With the rise of big data technology and rapid development, people are more concerned about HDFs, HBase, hive and other technologies. But when it comes to strong consistency, we have to go back to reality and continue to use RDBMS to store strong consistency data. The vast majority of applications that we are exposed to daily (from apps to games, from Internet applications to SAP) are data-based, and the process of application execution is the process of ongoing data processing, while the application design is object-oriented, the database is more data-oriented, and the two different kinds of thinking cause system comprehension and design complexity. At the same time, with the increasing complexity of the business, the dependence on the database is increasing, which leads to the coupling of the system and the database too tightly.
With the requirements of object-oriented and system decoupling, we hope to have a technology to transform the database structure into easy-to-understand program data structures. Through the processing of the program data into the operation of the database. This is a theoretical basis for the ORM (object-relational-map-relational Mapping).
The ORM methodology is based on three core principles:
Simplicity: Modeling data in the most basic form.
Communication: The database structure is documented in a language that anyone can understand.
Accuracy: Create a properly standardized structure based on the data model.
First, ORM and Ibatis
Earlier in the Java field there was a very well-known framework for SSH (Spring + Struts +hibernate). Each of them takes on this different responsibility, realizes the separation of the presentation layer, the business layer and the data persistence layer, and reduces the coupling of each layer. Hibernate is the main realization of the business layer and the data of the persistence layer decoupling. Hibernate is built on the theoretical basis of ORM. It is a technique for mapping object-oriented data transformations and operations to data persistence operations.
There are many other frameworks in the ORM field, in addition to hibernate. such as Entityframeword, Ibatis and so on. What we are going to introduce today is Ibatis, an ORM framework called "semi-automated".
To introduce Ibatis first of all to say "semi-automatic" this attribute. The Ibatis is "semi-automatic" and is relative to hibernate. Hibernate can be implemented without writing SQL statements to translate business data operations into database operations. Ibatis does not have a comprehensive encapsulation like hibernate, it does not abandon SQL statements, and even it does not reduce the amount of SQL statements written, it simply provides a way to separate the SQL statements from the system, which gives us enough space to play the powerful syntax of SQL.
Second, the principle of ibatis
The understanding of the fundamentals of Ibatis can be started by understanding this figure in the official documentation
Ibatis with Sqlmap as the core, the input parameters and output results are persisted and converted to CLR type. The input parameter class (Parameter object) is generated by the data processing method set in the Sqlmap and combined with the persisted data processing method (ADO), resulting in the output class (Result object). For. NET application business processing, implements the method processing of the class, thus realizes the application logic and the data persistence isolation.
As you can see, the input parameter parameter object and the output result object supports the collection type, the base type, and the value type. One of the core data conversion processes. The data conversion process is constrained by the XML configuration. This article is a technical practice article, so focus on the use of Ibatis related profiles.
Iii.. Ibatis configuration file
1, Sqlmap.config
Ibatis the core configuration file for the basic operating environment is the Sqlmap.config file. It is similar to App App. Config and Web. config. Other files of the Ibatis are loaded into the system through this configuration file. The basic sqlmap.config is as follows:
Sqlmap.config mainly includes several parts: Settings, providers, database, Sqlmaps
Settings : main Ibatis Global base configuration, which includes three configuration items
Node name |
Description |
Default value |
Usestatementnamespaces |
Whether to use a statement namespace, where the namespace refers to the namespace property of the Sqlmap node in the mapping file. That is, if the argument is set to "true", the statement is called with a namespace appended. |
FALSE |
Cachemodelsenabled |
Configure whether the Ibatis configuration item caching mechanism is enabled |
TRUE |
Validatesqlmap |
Configure whether the Sqlmapconfig.xsd schema validation mapping file is enabled |
FALSE |
Where Usestatementnamespaces is likely to be applied in a system environment. A value of "true" calls the statement when it needs to add the namespace name in its configuration file header. For example
When usestatementnamespaces= "true", it needs to be called by the name "Complex.complexmap" and, if False, by the name "Complexmap".
Providers : configures the path and file name of the data-driven provider class configuration file.
Various data-driven methods are described in the corresponding files in this configuration, and can be configured to ensure that Ibatis supports mainstream data (such as oracle\mssql\mysql\sqlite\access, etc.) on the market. To replace a database, you need to set the enabled initiator in the configuration file to "True" and modify the configuration in the database section.
Database : used primarily for database drive configuration and database connection string configuration.
The value of provider should correspond to name in the providers configuration file, and you need to set the enabled in the providers configuration file to "true".
The value of datasource is the database connection string. In order to better match the connection character, reduce the complexity of modifying the connection string, the properties can be used for pre-defined processing.
Sqlmaps : You can add the location of the XML map file in the configuration section to specify all the mapping files contained within the project. There are three ways of referencing an XML file
Parameters |
Description |
Resource |
Specifies that the data-driven configuration file is loaded from the project's root directory, such as resource= "Providers.config" |
wr. |
Specifies that the data-driven configuration file is loaded from the absolute path of the file, such as url= "C:\IBatis\Resources\providers.config" Or Url= "File://c:\ibatis\resources\providers.config" |
Embedded |
Specifies that the data-driven configuration file can be loaded as a resource file for the assembly, such as embedded= "Resources.providers.config, Myapp.data" |
There are other configurations in the configuration file (such as properties, Alias, Typehandlers), which are not described here.
2, Sqlmap.xml
Sqlmap mainly consists of three parts, the entry configuration (Parameter), the return result configuration (result), and the Data manipulation SQL configuration (statements).
Parameter
Used to configure the incoming parameter format, which is passed through the configuration to statements, parameter can be passed in Parameterclass or Parametermap two different ways.
Parameterclass can be configured as an application class name, passing the corresponding class instance into the Ibatis framework, configured with its property name as a sqlmap statement matching element, and generating the corresponding SQL statement.
Parametermap can be extended to Parameterclass to achieve null conversion, field matching and other functions, through the configuration of the way to the null value, name matching and other processing.
Result
Resul also consists of two forms, ResultClass and Resultmap. The ResultClass configuration can map the return value to an instance of the set class, and can map the query results to the appropriate class.
Resultmap the ResultClass extension, you can set the correspondence between the data query field and the Class data field.
Property |
Description |
Parametermap |
Parameter mapping, the mapping relationship needs to be defined in conjunction with PARAMETERMAP node, for statement outside the stored procedure, it is recommended to use Parameterclass as the parameter configuration method, on the one hand, the parameter mapping configuration work is avoided, on the other hand, the performance is better. |
Parameterclass |
The parameter class. The full class name (including namespace) of the parameter type is specified, and you can avoid writing lengthy class names each time by using aliases |
Resultmap |
The mapping relationship is defined with the Resultmap node in the result mapping. |
ResultClass |
The result class. The full class name (including namespace) of the result type is specified, and you can avoid writing lengthy class names each time by using aliases |
The Parametermap and Resultmap formats are similar, and mainly include the following properties:
Property |
Description |
Property |
Specify a property in the class |
Column |
Name of the parameter defined |
Direction |
Parameter direction for declaring a stored procedure (input,output,inputoutput) |
DbType |
Used to specify the type of data that the property maps to the database |
Type |
Specifies the CLR type for the object for the parameter |
Nullvalue |
When specifying the value of the property, it will be replaced with NULL when storing the data, which is often used |
Size |
Used to specify the maximum value |
Note:parameter and result are all related to the properties of the class, which are case-sensitive by C # language attributes, and therefore require special attention when making XML configurations.
Statements
Ibatis the core of the transformation of classes and SQL. He is mainly composed of ID, parameter, result, dynamic SQL, first introduced the ID, parameter, result, the following terminal describes the dynamic SQL statement.
To be more flexible with SQL statement matching, Ibatis has defined multiple operators.
Operator |
Conditions |
Description |
Routines |
IsEqual |
Duality conditions |
Equal |
<isequal prepend= "and" property= "StartDate" comparevalue= "25/12/2004" > |
Isnotequal |
Duality conditions |
Not equal |
<isnotequal prepend= "and" property= "year" comparevalue= "0" > |
Isgreaterequal |
Duality conditions |
Greater than or equal to static value |
<isgreaterequal comparevalue= "3" > |
Isgreaterthan |
Duality conditions |
Greater than |
<isgreaterthan prepend= "and" property= "Id" comparevalue= "0" > |
Islessequal |
Duality conditions |
Less than or equal |
<islessequal comparevalue= "3" > |
Islessthan |
Duality conditions |
Less than |
<islessthan comparevalue= "3" > |
Isnotempty |
One-dimensional conditions |
is not null or is not empty |
<isnotempty prepend= "and" property= "LastName" > |
IsEmpty |
One-dimensional conditions |
Is null or empty |
|
Isnotnull |
One-dimensional conditions |
Checks whether the property is not null. |
<isnotnull prepend= "and" property= "FirstName" > |
Isnotparameterpresent |
|
Check if no Parameter object exists |
<isnotparameterpresent prepend= "and" > |
Isnotpropertyavailable |
One-dimensional conditions |
Check if the property does not exist |
|
IsNull |
One-dimensional conditions |
Checks whether the property is null. |
|
Isparameterpresent |
|
Checks whether a parameter object exists (not null). |
Ispropertyavailable |
One-dimensional conditions |
Check if the property exists |
|
Iterate |
|
|
|
The use of operators is primarily for input parameters.
Binary operators
<Property= "Name" comparevalue= "1" prepend= "and "> a1= ' abc ' </isequal>
Input parameter The value of the Name property equals 1 o'clock, a1= ' abc ' is combined in SQL, otherwise no SQL is combined.
Similar usages are similar to isnotequal, Isgreaterequal, Isgreaterthan, Islessequal, and Islessthan.
Unary operators
<property= "name" prepend= "and"> a1= ' ABC ' </isequal>
When the value of the input parameter name property is NULL, a1= ' ABC ' is combined in SQL, otherwise SQL is not combined.
Array operators
One way to convert an array to an SQL statement, for example
<property= "Ids" open= "(" close= ")" conjunction ="," ="" > #Ids []#</iterate>
Converts an array to an SQL statement similar to "(1,2,3,4,5)".
Through a series of configuration we will be a method of the program or a database operation into a sqlmap.xml configuration, the method into the parameter into the parameter, the method is converted to result, and the method name into the ID, so that the object-oriented business method into the database operation.
Ibatis In addition to the above syntax points, also includes some other syntax such as extends, caching and so on. Full syntax support ensures Ibatis support for most scenarios. This article in a lot of knowledge is just a simple explanation, far from showing the full picture of Ibatis. Only through continuous learning and accumulation, and flexible application to play its powerful configuration function, in order to better play the function of Ibatis, to achieve real business and data separation.
Ibatis.net Technology Practice