1. ibatis introduction in the ibatis Tutorial:
What is ibatis? Compared with "one-stop" orm solutions such as hibernate and apache ojb, ibatis is a "semi-automated" orm implementation.
This framework will enable you to better design and implement real-world layers in java applications. This framework has two main components: SQL maps and data access objects. Some useful tools are also included.
SQL maps:
SQL maps is the most exciting part of the framework. It is the core value of the entire ibatis database layer. By using SQL maps, You can significantly reduce the amount of code used for database operations. SQL maps uses a simple xml file to map javabean to SQL statements. Compared with other frameworks or object Ing tools, SQL maps has the greatest advantage of being simple. It has very few things to learn, and does not need complicated scheme when connecting tables or complex queries (how to translate complex scheme ?), With SQL maps, you can use SQL statements freely.
Data access objects (dao)
When developing flexible java applications, a good idea is to encapsulate the details of entity operations through a common api layer. Data access objects allows you to operate data through a simple interface, thus hiding the implementation details. With dao, You can dynamically configure your applications to access different object storage mechanisms. If you have a complex application that requires several different databases, dao allows you to build a consistent api and call other parts of the system.
Utilities
Ibatis database layer contains a series of useful tools, such as simpledatasource and a lightweight Implementation of jdbc datasource 2.0 (including 3.0. Scriptrunner also provides database preparation from unit testing to automatic release.
Workflow:
SQL maps provides a simple framework to map JavaBeans, map implementations, and even original type packages (string, integer, etc.) to JDBC preparedstatement through XML descriptions. The idea is simple. The basic steps are as follows:
1) provide an object as a parameter (either a JavaBean, map or primitive wrapper), the parameter object will be used setting input values in an update statement, or query values in a where clause (etc .). (I think it's better not to translate it. What do you say ?)
2) execute the Statement of the ing. This step is where magic occurs. The SQL maps framework creates a preparedstatement instance, uses the parameters of the previously provided object, executes statement, and creates a return object from the resultset.
3) For update, the number of rows modified is returned. For query, a set of objects or objects is returned. Like parameters, the returned object can also be a JavaBean, map, or primitive type wrapper.
Ii. Use the configuration file of the ibatis Tutorial:
SQL map configuration file. This file is the root configuration of SQL map implementation, which is not described in this summary.
The configuration file is an XML file. We use it to configure attributes, JDBC datasources and SQL maps. It gives us a convenient place to centrally configure different datasource. This framework supports ibatis simpledatasource, Jakarta DBCP (Commons), and any other datasource that can be accessed through the JNDI context. We will discuss this issue in detail later. Now we use Jakarta DBCP, which has a simple structure. In the example above, its configuration file is as follows.
<Properties> element:
The <Properties> element in SQL map supports the SQL map xml configuration file. The elements in the configuration file comply with the Data Map rules.
<Properties> the elements in the applied file must correspond to the elements in <datasource>. The content of the sqlmapconfigexample. properties file is as follows. The elements in it correspond to the elements in <datasource>.
<Settings> element
Maxrequests: The number of times SQL statements can be executed at the same time
Maxsession: Number of sessions that can work at the same time
Maxtransaction: transactions that can be executed at the same time.
Statementcachesize: buffer size
Iii. Operation Methods in the ibatis Tutorial:
Note: This summary is suitable for reference by developers who use ibatis. Due to my limited capacity, please point out the shortcomings.
1. Statement supports the following elements and attributes:
1.1 ID
The ID attribute is required. It indicates the name of the statement, which is unique within the entire <sqlmap> element. Reference related statements by ID in the code.
1.2 parametermap
The parametermap attribute defines an ordered Value List, which corresponds to? Placeholder.
1.3 parameterclass
If the parametermap attribute is not specified, you can specify the parameterclass attribute method to replace the parametermap attribute. The value of the parameterclass attribute can be an alias of the type or the full name of the type (class ).
1.4 resultmap
Resultmap allows you to control how to extract data from the query result (resultmap) and store it in a java object. You can also define how to map fields to java bean attributes. For example:
1.5 resultclass
When the resultmap attribute is not specified, the resultclass method can be used. The value of the resultclass attribute can be the type alias or the full name of the type (class. The specified class is automatically mapped to the field value of the query result based on the metadata of the database.
2. parametermap element attributes
2.1 id
The id attribute is required. It provides a unique identifier for the <parametermap> element data map.
2.2 class
The class attribute is optional. The class attribute must be a javabean or map real column.
Although this attribute is optional, we recommend that you specify it. The class attribute can be used to verify the validity of the input parameter and optimize the query.
3. query statement
3.1 SHIELD special characters <! [Cdata []>
3.2 statement import <include>, which can be statement Optimization
Using the <include> method, the preceding statement can be written as follows:
4. Auto-increment method: <selectkey resultclass = "int">. This auto-increment method is executed no matter where <selectkey resultclass = "int"> is in the statement. Note: The methods for mysql and oracle are different.
5. <insert> method:
The insert method uses the parametermap attribute. The field name must match the elements in the <parametermap> method one by one.
6. <resultclass> attributes:
Using the resultclass method, attributes such as id, firstname, lastname, birthdate, werghtkilograms, and heightmeters are required in the class person.
7. Use xmlresultname: xml ing File
8. Use <parametermap> to set the attributes of an element. nullvalue indicates the default value.
9. If only one input parameter exists, use # value # to replace the input parameter name. If the field name is still sent, ibatis does not support
10. Determine whether the input parameter exists: isnotnull and whether the input parameter value is greater than the set value: isgreaterthan
11. Determine whether the input parameters are null: isempty and isnotemply
12. Use the like method for fuzzy search.