Ibatis SQL MAPS (2)

xiaoxiao2021-03-06  38

Let us return to the story of the vehicle management system and Zhang San.

There is also one-to-man, man-to-one relationship in the world of Ibatis SQL Maps, and you have to drive these concepts. it is good! Still each person corresponds to multiple autoinfo information.

The first part of this series mentioned that the number of mapped files for Ibatis SQL MAPS can be set, but a group of co-functions is placed together is the preferred policy. Let's take a look at what maps generated by the first time for Zhang San's first time:

>

> SQLMAP

SQLMAP elements have attributes Namespace = "..." defined the XML file namespace. If you specify the properties of the Settings element in the configuration file SQLMAPConfig.xml, you can access mapped statement in the way named space, such as Namespace = "Automag", corresponding Java code: SQLMap.insert ("Automap .insertpeople ", people). This is to prevent conflicts in different mapping files from appearing in different mapping files. What is Mapped Statement?

Mapped Statement

Ibatis SQL MAPS core concept is Mapped Statement! Mapped Statement can use any SQL statement, using Pojo, raw variable, and its Wrapper Class as an input (Result Class).

Mapped statement contains the following types:

INSERT corresponds to the INSERT operation of the database, which returns the primary key value inserted in this operation.

SELECT corresponds to the SELECT operation of the database, which returns a specific POJO or object.

Update corresponds to the UPDATE operation of the database, which returns the number of updated records.

Delete corresponds to the DELETE operation of the database, which returns the number of records being deleted.

Procedure corresponds to the data set.

The STATEMENT type is most common and can replace all the above types. However, it is not recommended because of the lack of operation intuitiveness. INSERT ID = "Insertpeople" parameterclass = "bo.people"

Define the mapped statement of the Insert type. Property ID = "Insertpeople" Defines the operation name, parameterclass = "bo.people" Defines the incoming parameter to the PEOPLE object instance, the framework ensures that its property persisted into the database corresponding field. Since the SQL statement will contain "<>" symbols, it is easy to conflict with XML, and it will be avoided in the area. INSERT INTO People (Name, Address) VALUES (# name #, # address #) is a normal SQL statement, "# name #, # address #" to access the corresponding attribute of the PEOPLE object instance using the Java reflex mechanism.

SelectKey Resultclass = "java.lang.integer" keyproperty = "id"

IBATIS SQL MAPS supports automatic generation of primary keys through the child elements of the element . Resultclass = "java.lang.integer" defines the Wrapper Class for the object to INT. KeyProperty = "id" defines the primary key name. This example is the MYSQL primary key generation method, refer to the official document, Mysql's primary key generation does not need to control, that is, it is automatically handled by the database without using . But I tested found that after executing the Insert operation, the program did not return the primary key value inserted in this operation. There are also many users in the official forum to make such doubts, the author's reply is: this is related to JDBC Driver. Can't you test one by one? The way one is always for all is to use the element. The following is the Oracle and SQL Server primary key generation method:

<-! Oracle -> SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL INSERT INTO PRODUCT (PrD_ID, PrD_DESCRIPTION) VALUES (# ID #, # description #) INSERT INTO PRODUCT VALUES (# description #) VALUES (# description #) select @@ identity as id SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values ​​( #ID #, # description #) Insert Into Product (product_description) Values ​​(# Description #) select @@ identity as id

INSERT INTO AUTO_INFO (License_Plate, Owner_no) Values ​​(# licenseplate #, # Ownerno.id #)

After inserting the People record, you have to insert records for Auto_INFO. The basic principle is the same as before, just "owner_no" field value is obtained by the AutoInfo object property "OwnerNo", which is people. This is because I used the Pojo generated in Hibernate, if you like, you can replace "OwnerNo" to the Integer type.

Several key objects

The com.ibatis.common.Resources.Resources object is responsible for getting an instance of the Java.io.Reader abstraction class from XML for factory methods. com.ibatis.sqlmap.client.sqlmapclientBuilder Constructs a SQLMAPCLIENT instance.

com.ibatis.sqlmap.client.sqlmapclient is the Ibatis SQL MAPS core component, which can be said that our programming work is around it.

The one-to-many formed is as follows:

package test; import java.io.Reader; import com.ibatis.sqlmap.client *;. import com.ibatis.common.resources *;. import bo *;. public class AutoMag {private Reader reader; private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public void insertPeople () throws Exception {try {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); sqlMap.startTransaction (); people people = new people () People.setName ("Zhang Third"); People.Setaddress ("China"); Sqlmap.insert ("Insertpeople"; AutoInfo AutoInfo = New AutoInfo (); AutoInfo.setLicenseplate ("A00001"); AutoInfo. Setownerno; sqlmap.insert ("INSERTAUTOINFO", AutoInfo; sqlmap.committransaction ();} finally {sqlmap.endtransaction ();}}}

package test; import java.io.Reader; import com.ibatis.sqlmap.client *;. import com.ibatis.common.resources *;. import bo *;. public class AutoMag {private Reader reader; private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public void insertPeople () throws Exception {try {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); sqlMap.startTransaction (); people people = new people () People.setName ("Zhang Third"); People.Setaddress ("China"); Sqlmap.insert ("Insertpeople"; AutoInfo AutoInfo = New AutoInfo (); AutoInfo.setLicenseplate ("A00001"); AutoInfo. Setownerno; Sqlmap.insert ("INSERTAUTOINFO", AutoInfo; sqlmap.committransaction ();} finally {sqlmap.endtransaction ();}}} The program and Hibernate are almost simply simpler than Hibernate. I can display the INSERT operation in line with traditional JDBC programming habits. IBATIS SQL MAPS supports automatic transaction processing, and you can write StartTransaction, CommitTransAction. However, if the people of the People Insert is successful, the AutoInfo Insert operation fails, and the atomicity of the two INSERT operations is destroyed. The endTransaction contains two functions of rollback transactions and closing the connection pool.

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-68275.html

New Post(0)