Ibatis SQL MAPS (1)

xiaoxiao2021-03-06  39

I wrote some articles in Hibernate, and netizens were not bad. In the next time, I will introduce another O / R Mapping Solution - Ibatis, which will take the style of the Hibernate series.

What is iBATIS?

Like numerous SourceForge open source projects, Ibatis has been one of them. Successfully became a subproject under Apache Incubator on 3 November 2004. IBATIS includes two versions of For Java and For .NET, the For Java version provides SQL MAPS and DAO frameworks, for .NET only provides a SQL Maps framework. From now on, we only discuss the SQL MAPS of the For Java version.

You can download Ibatis for Java at http://www.ibatis.com. The latest version is 2.0.9, and the compressed package already contains SQL Maps (Ibatis-Sqlmap-2.jar) and DAO Framework (Ibatis-Dao- 2.jar).

Why choose Ibatis?

Perhaps you have seen the officially understanding of its advantages in a variety of different technical information. But for me, there is only one reason to choose its - "Use the original resources". Hibernate is excellent, but it is difficult to integrate the original system. It is difficult for it. For example, the composite primary key used in the data modeling, across the multi-tables generated inquiry, using the original stored procedure ... When facing this series of questions, hibernate is not your heart, Want to use Hibernate to only transform the original system!

When I face the system integration problem (integrated requirements are very simple: just retain the original system query part), Ibatis enters my sight. In addition to the SQL statement in the original system, the data table and query results do not need to change! Nor does it use to map a lot of profiles like Hibernate, Pojo, a lot of refreshing. BTW: hibernate is not wrong! Just I only need to query the function, just take me well, avoid killing chicken with a cattle knife.

At present, the system integration has ended and spent a month. If you use Hibernate, I am afraid I am still in designing the data sheet, how to fight with my colleagues.

Why choose Ibatis?

Perhaps you have seen the officially understanding of its advantages in a variety of different technical information. But for me, there is only one reason to choose its - "Use the original resources". Hibernate is excellent, but it is difficult to integrate the original system. It is difficult for it. For example, the composite primary key used in the data modeling, across the multi-tables generated inquiry, using the original stored procedure ... When facing this series of questions, hibernate is not your heart, Want to use Hibernate to only transform the original system!

When I face the system integration problem (integrated requirements are very simple: just retain the original system query part), Ibatis enters my sight. In addition to the SQL statement in the original system, the data table and query results do not need to change! Nor does it use to map a lot of profiles like Hibernate, Pojo, a lot of refreshing. BTW: hibernate is not wrong! Just I only need to query the function, just take me well, avoid killing chicken with a cattle knife.

At present, the system integration has ended and spent a month. If you use Hibernate, I am afraid I am still in designing the data sheet, how to fight with my colleagues.

Start another O / R mapping Bo, the BO of the O / R Mapping trip, the data sheet can also be reused, just migrating the database to MySQL. Open Eclipse, create a new RESIN project. Copy Ibatis-Sqlmap-2.jar and Ibatis-Common-2.jar to the lib directory, and then import projects. It is hand-made by Hibernate automatic configuration, automatic mapping.

Configure configuration files under SRC SQLMAPCONFIG.XML, database links, connect pools, and SQLMAP mapping files ... These must rely on it. The official reference manual has a very detailed description of how to set up, I only have a rough explanation of the places you want to use.

TransactionManager

Element is defined

Ibatis

Transaction manager.

Type

Options include:

JDBC: Manage transactions through traditional JDBC.

JTA: Using a JTA global transaction, allowing IBATIs' transactions into larger transaction (cross database session), this larger transaction range may include other databases and transaction resources. This configuration requires a UserTransaction property to get a useertransaction from JNDI.

EXTERNAL: Call other transaction managers other than IBATIS to manage transactions.

The DataSource element is part of TransactionManager. The Type options include: Simple: Simple is the IBATIS built-in DataSource implementation, which implements a simple database connection pool that can be used when the container provides DataSource services, and the corresponding IBATIS. Sqlmap.Engine. DataSource.SIMPLEDASourceFactory.

 DBCP: DataSource based on Apache DBCP Connection Pool API, when the container provides DataSource service, you can use this option, corresponding to the IBATIS.sqlmap.Engine.DataSource.dbcpdataSourceFactory.

JNDI: Using the DataSource implementation provided by the J2EE container, DataSource will be obtained from the container via the specified JNDI Name. Corresponding to the IBATIS implementation class com.ibatis.sqlmap.Engine.DataSource.jndidataSourceFactory.

note! Each DataSource element has a different place, can't just change the Type name.

The SQLMAP element defines the storage location of the mapping file. You can include multiple SQLMAP elements in the configuration file, such as:

... ...

You may have discovered that I only define a single mapping file. Nice, and a mapping file with Hibernate, the number of IBATIS's mapping files can be controlled, and the granularity is mastered.

Light has a BO and configuration files, but also create test class Automag.java for this test. The complete layout is as follows: The following is the Ibatis SQL MAPS workflow, which is very helpful for understanding concepts. Master is 1, you can use the JavaBean, Map type, original variable (or their wrapper class), XML data as an incoming object; 2, load mapping files by configuration file; 3. Using framework to translate into JDBC to access the database; 4. The execution result can be a Wrapper Class, XML data of JavaBean, Map type, and raw variable.

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

The following is the Ibatis SQL MAPS workflow, which is very helpful for understanding concepts. Master is 1, you can use the JavaBean, Map type, original variable (or their wrapper class), XML data as an incoming object; 2, load mapping files by configuration file; 3. Using framework to translate into JDBC to access the database; 4. The execution result can be a Wrapper Class, XML data of JavaBean, Map type, and raw variable.

(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-71160.html

New Post(0)