Ibatis is another o / r mapping solution, J2EE's O / R scheme is much more, compared with Hibernate, iBATIS is small, it is very fast. If you don't need too much complex feature, iBATIS is the easiest solution to meet your requirements and flexible solutions.
The biggest feature of Ibatis is simple, the latest version 2.0 (
Download), compared with 1.0, mainly modified on the XML configuration file, but as long as there is SQL foundation, I believe that you can understand without tutorial. Let's take a look at a simplest example.
We first build a table Account, including the field username, varchar (20), PK, and Password, Varchar (20), where some data is filled in. Then write IBATI must have the configuration file SQL-MAP-Config.xml, the file name is free, the content is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
http://www.ibatis.com/dtd/sql-map-config-2.dtd
">
Other parts You don't have to manage it, I am a sample configuration of direct copys, just pay attention to the red part, configure the data source. I use Access, so use JDBCODBC drivers. If you use mysql or other database, change the appropriate properties.
Then note that this profile also references an account.xml, it's right, iBatis associates each Java object that requires o / r mapping to an XML configuration file, we need to map an Account table to an Account class: package eXample ; public class Account {private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
Write an Account.xml file:
XML Version = "1.0" Encoding = "UTF-8"?>
http://www.ibatis.com/dtd/sql-map-2.dtd
">
We mainly care about the red part. Take
Finally, how to use IBATI to implement O / R mapping. First initialize iBatis to get a SQLMAPCLIENT object:
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null; try {java.io.Reader reader = om.ibatis.common.resources.Resources.getResourceAsReader ( "sql-map-config.xml"); sqlMap = SqlMapClientBuilder.buildSqlMapClient (});} catch (exception e) {E.PrintStackTrace ();} Then you can use O / R mapping anomally, such as UserName = admin on Account:
Try {Account Accord = (Account) SQLMap.QueryForObject ("getaccount", "admin"); ...}
Or create a new Account:
Try {Account Account = New Account (); Account.setUsername ("Micheal"); Account.SetPassword ("1234"); Sqlmap.insert ("CreateAccount", Account);
To put two configuration files and iBatis's 3 JAR packages in ClassPath, requiring JDK1.4.
Summary: Ibatis is indeed simple and flexible, easy to get off, the code is very small, the configuration is a bit complicated. Insufficient, one is that there is no convenient tool to automatically generate an XML configuration file. Second, whether it is query or INSERT, it can only be passed into a parameter, sometimes packaged in two parameters into a class pass. It is not as good as the common 1: 1, 1: N relationship than Hibernate. However, most of the iBATIS is fully able to meet our needs. Spring is well integrated with Ibatis, you can refer to Spring's JPETSTORE example. It should be noted that there is a big difference in IBATIS 2.0 and 1.0, mainly in the configuration file, IBATIS 2.0 adds a DAO framework, and does not require Spring to provide DAO mode.