Mendix practice and move: Mendix Studio Pro connects to the peripheral database (SQL Server) to implement additions, deletions, and changes

1. Environmental preparation

1: Connecting to peripheral databases is a built-in function of Mendix, and the connection methods of different databases are similar. This article uses SQL Server as an example to explain;

2: This article assumes that the reader is a Mendix junior scholar, so the authors try to describe each step as detailed as possible, and skilled scholars can read it at 1.5 times the speed.

3: Installation checklist:

 

2. Download Database Connector and related configuration

Assuming you have installed Mendix Studio Pro and built your project, click the icon (App Store) in the upper right corner of Studio Pro to enter the App Store interface (embedded in the Mendix Studio Pro workspace).

Select Connectors->Data in the directory on the left or directly enter Database in the search box, find "Database Connector", click to open:

First click on the "Download" button to download, and select "Add as a new module" by default to add it to the project as a new component.

After importing, you can find it under the project path App Store modules.

Then, click the Database Connector link under the Document tab in the App Store to open the reference document on the web page to view specific usage information and precautions. It is worth noting the part of the prerequisites:

You need to download the relevant JDBC driver/driver file, unzip it and place it in the userlib directory of the Mendix project, and then when you define the database connection constants in the Mendix project, you need to maintain the corresponding path information (described below), if you The selected database is PostgreSQL, just refer to the example given here. Our database is SQL Server, so we click on the link Common JDBC Drivers to jump to the JDBC Drivers section below to download the drivers we need.

Select the SQL Server database we want, and we will arrive at the address where we need to download the Driver ( https://www.microsoft.com/en-us/download/details.aspx?id=11774 ), just download it.

You can choose to save it in a temporary directory, unzip it, and copy it to the directory jre8 (such as ~\Microsoft JDBC Driver 6.0 for SQL Server\sqljdbc_6.0\enu\jre8) and copy the .jar (I am sqljdbc42.jar here) Go to the userlib (C:\Users\xxx\Documents\Mendix\MyLaunchPad\userlib in my case) directory of the project.

At this point, the download and installation of the database connector in Mendix Studio Pro is complete.

 

3. Preparations in the database

Next, we enter the database and do some data preparation work, including: building a database, building a table, and building a user.

Of course, first you have to conceive the data model you need to operate. For example, my design here is to maintain a service cost rate (Service Cost Rate).

Open your database manager, I am SQL Server Management Studio here.

1.  Create a new database

Name it ExternalDB for the time being and save it.

2. Build a data table

Create fields and attributes according to the designed table structure.

My reference information:

Table: CostRates

CostRateID (主 键)

int

CostDescription

nvarchar(500)

CompanyID

nvarchar(50)

ValidTo

nvarchar(50)

CostRate

decimal(18, 2)

3.  Build users

Define database access users. In SSMS, access the path Security->Login, right-click -> New Login (the location of different databases may be slightly different, in short, define a database access user).

  • Named: MXadmin
  • Password: mendix

Set the user as the database Owner: Double-click the User Mapping on the left, select your database "ExternalDB", check db_owner in the database role in the lower right corner, and save.

At this time, you can see that the user has been built:

 

Four, Mendix Studio Pro built data model and database connection constants

1. The  data model / the Data the Model

We built the same data model in Mendix again, and the data types are consistent.

 

2.  Create database access constants

  • URL:  右键MyFirstModule -> Add other -> Constant:

  • Naming: DB_URL
  • Type: String
  • Default value: jdbc:sqlserver://localhost;database=ExternalDB;

Back to Mendix Studio Pro, continue to build two constants:

DB_USER:

DB_PASS:

At this point, the preparations for accessing the local database have been in place, and now you will begin to develop your application to realize the addition, deletion, and modification of the database.

 

Fifth, realize the addition, deletion, modification and investigation

1.  Application framework design

Briefly describe the main design ideas of the application page demonstrated in this article. The first application uses the SAP template, and the entrance is My Launchpad (My Work Panel). Clicking on the first application "Cost Maintenance" in the My Panel will jump to the overview interface of cost maintenance, where we complete the addition, deletion, modification, and inspection actions. . The link part of the page will not be repeated here. Configure the response of On Click to "Show a Page" in the corresponding link properties, and then select the default List control. See screenshot:

 

2.  Initialization and filling of overview page data

The newly created page is named CostRateOverview, which is used to display the overview information of the cost and provide an entry for addition, deletion, and modification.

 

2.1 Adjust the CostRateOverview page layout

Adjust the CostRateOverview page layout and delete unnecessary controls.

Modify the page header and description;

Modify the existing fields and attributes, and arrange the information that needs to be displayed on the page in order: CostDescription, CompanyID, ValidTo, CostRate.

In the position of the parameter, click "New" and select CostDescription.

Add the parameter {1} to the field description.

Modify other attribute fields in the same way.

At the same time, ensure that the button that has no event response for the time being, the value of On Click response in its property is selected as Do Nothing.

 

2.2 Data filling micro-flow design

Select the List control, and drag the defined Data Entity "CostRates" from the Connector tab of the tool box on the right to the List control.

The pop-up window chooses to fill the data by Microflow/Microflow, check "Automatically fill the content of the list view", we temporarily choose to manually add the attribute field and fill the data through the microflow.

Double-click the micro-flow name "Show_CostOverview" in the left navigation bar to enter the micro-flow editing workspace. Now we come to realize the query CostRates table information from the database and fill in the ListView component of the page just defined.

Just as we need to conceive the function architecture before writing code, we must first think about what it will do for us before designing the microflow:

Read the CostRates table information from the specified database ExternalDB and fill it in the ListView control of the CostRateOverview page just defined;

<- And whether the action of "fill in xx control" has been configured/associated through properties, then can my task description be simplified to:

<-Read the CostRates table information from the specified database ExternalDB, which also contains a built-in logic (the output structure must be consistent with the structure received in the Listview, is it not difficult to imagine, here is DataEntity:  CostRates ), so I have My function design:

<-

enter:

Database access URL (the constant DB_URL I have defined);

Database access user (DB_USER);

Database access password (DB_PASS);

Output: a DataEntity:  CostRates type structure/table (I don’t seem to define this parameter yet, it’s okay, look down);

Okay, let's implement the microflow:

<- 

1. Define a DataEntity: CostRates type parameter and pass it into the micro-flow function to receive the queried data:

The micro-flow editing area provides a very convenient shortcut Action Activity, click, click again at the destination to be placed, it is fine, here we define one;

Double-click Activity, select Action type, here we want to define an Object parameter, so select Create Object.

You can directly select the defined Entity to associate with it, which is very convenient (Select->Select your Entity in the pop-up box).

Give a name to make it easier to distinguish, for example: CostRate_Obj.

In this way, the output parameters are defined and the query function can be executed.

2. Define the database query

The Toolbox of Mendix Studio Pro provides us with many Actions that can be directly dragged and used, such as the "Execute Query" we will use, and select it and drag it directly to the workspace.

Double-click the Execute Query Activity, and fill in the parameters in the pop-up box according to the previous design. It looks like we usually write functions, right?

Click the button "Edit" behind each parameter input area, and select the parameter according to this syntax format.

When selecting a constant, just enter an @ character to associate a defined constant, which supports fuzzy search.

In this way, define the input parameters "Jdbc url", "User name", "User password".

For the Sql part, we need to edit the SQL query statement to query the data table information, the format is as follows:

'Select CostRateID CostDescription, CompanyID, ValidTo, CostRate from CostRates'

[Remarks] Cost RateID does not need to be displayed on the interface for the time being, but it needs to be taken out and used in subsequent database operations;

Result Object selects the newly defined parameter $CostRate_Obj.

Output terminal:

List name: ListofCostRate (used at the end of the micro-stream, as the output of the entire micro-stream)

3. Set the output format

At the end of the microflow, double-click, select the output type as List, and the value is the List Name $ListofCostRate just defined.

This completes the definition of microflow.

 

2.3 Trial run

Click Run Locally to compile and test run.

Click View to enter the application interface.

Click Cost Maintenance to view the Service Cost Overview interface, because the data table is currently empty, so there is no data display.

Add information to the database table:

Run again after adding information:

 

3.  Add

Page design :

-The added button is in the upper right corner of the main page

-To create a new cost record requires filling in a new form, so a new page is required

-The information entered when creating a new project includes: Cost Description/CostDescription, Company ID/ CompanyID, Deadline/ValidTo, Price/CostRate.

-There should be a save button after input

-There should be a pop-up prompt when saving is successful

-There should be a Cancel button to abandon the new creation and return to the previous page

Decompose and realize according to the above design:

 

 

New page CostRateNew

 

The Save button responds to the event Call a microflow, creating a new microflow

 

 Realize the micro-flow CostRate_InsertUpdate, insert the information entered on the page CostRateNew into the database:

Define the parameters needed to write the database, take CostRateID as an example:

Define other parameters in the same way. After completion, they are as follows:

Adding a record corresponds to inserting a piece of data into the database, so the next step we need to execute a SQL statement to insert a record into the database, the inserted value is the input value on the current page (obtained by the parameters defined above). Find the Execute Statement action in the Toolbox, and drag it to the workspace.

Close the current page after completion, so we have to add an Action: Close Page.

At the same time, for user-friendliness, we pop up a prompt that the message has been saved. The parameter CostRateDescription can be quoted in the message to indicate that the record has been saved. This Activity is called "Show Message".

 

Run the program, verify the add button

Verify that the record is added successfully in the database:

After a detailed introduction to the above process, especially the "Add" button, we basically cover the basic operations of the rapid development of Mendix Studio Pro, so we will introduce the basic logic overview interface for the subsequent function implementation, and will not repeat it. If there is any problem Can be discussed later.

 

4.  Edit/Modify

Edit/modified interface design:

-The "Edit" button is located in the lower left corner of each cost record

-Click the "Edit" button to enter the new editing interface. The interface should be similar to the interface for creating a new record. The difference is that each attribute field in the new interface is empty, and each attribute field is the edited value when editing.

-Editable information includes: Cost Description/CostDescription, Company ID/ CompanyID, Deadline/ValidTo, Price/CostRate.

-There should be a save button after input

-There should be a pop-up prompt for successful saving: a record has been modified

-There should be a Cancel button to give up the modification and return to the previous page

Decompose and realize according to the above design:

After clicking the Edit button, a micro-flow is implemented to call up the edit (new) page and fill the current value on the page.

The microflow in response to the Edit event mainly does two things. The first is to read the current data from the database according to CostRateID, and the second is to call up the page CostRateNew and put the data read in the previous step into the page for editing.

'Select * from CostRates where CostRateID =' + $ CostRates / CostRateID + ';'

When the Save button is clicked, the micro-flow CostRate_InsertUpdate will be called. We have implemented this micro-flow before adding it. Here we need to make some changes to the implementation logic to make it also meet the needs of Edit:

-Edit if Cost already exists, otherwise create/add

-On the Edit line: execute the SQL Update statement to update

- Close Page

-A pop-up prompts that the modification is successful

-Back to the main process

Therefore, the realized microflow (core part) is as follows, and is accompanied by detailed information reference:

Analyzing conditions:

SQL statement used by Update:

Save and run:

  

 

5.  Delete

The delete operation has no interface design, and the logic is relatively simple:

-Click the delete button to call the micro flow

-Corresponding to the deletion of database entries

- refresh page

Button properties:

Definition of microfluidics:

 

6.  Query

Query interface design:

-The query button is set at the top right of the page, next to the "Add" button

-Click the query button to pop up the query input interface, which contains an input box, a query button, and a cancel button

-There are format requirements for the input information (time relationship, here only search by ID)

-The information found should be displayed on the new pop-up page, read-only, with a confirm close button

-If you can't find this item, there should be a pop-up prompt

Data Model

The input text of the query input box needs to have a corresponding Data Model design.

 

Query button definition:

Query the definition of the main interface:

 

Definition of microfluidics:

 

 

 

 

 

 

S earch Result interface:

 

Run and test:

Query failed:

 

 search successful:

 

 

 

Six, some empirical tips

If you encounter a connection error, you will generally be prompted to check the firewall settings, whether the TCP/IP port is available, etc., and check as required:

  • Check firewall settings
  • Check SQL Server configuration 


For more information, please visit the following link:

Mendix official website: https://www.mendix.com/zh/

Mendix industry solutions: https://solutions.mendix.com/

Mendix platform guide: https://www.mendix.com/evaluation-guide/

Mendix animation display: https://www.mendix.com/demos/

Mendix public account

 

thanks for reading!

Guess you like

Origin blog.csdn.net/Mendix/article/details/115253527