Integration of ibatis and spring

Source: Internet
Author: User

Spring provides good support for iBATIS through the DAO mode. SqlMapClient objects are the main objects in iBATIS. We can configure spring to manage the creation of SqlMapClient objects.
Similar to hibernate, Spring provides the SqlMapClientDaoSupport object. Our DAO can inherit this class and manipulate the database through the SqlMapClientTemplate object provided by it. It seems that these concepts are similar to those of hibernate.

Using SqlMapClientTemplate to manipulate database CRUD is no problem. The key issue here is transaction processing. Spring provides powerful declarative transaction processing functions. We know how to configure declarative transactions in hibernate. How can we obtain declarative transaction capabilities in iBATIS?

First, we need to know that spring uses AOP to intercept method calls and add declarative transaction processing capabilities to these methods. Typical configuration: applicationContext-common.xml
[Html]
<! -- Configure Transaction Features -->
 
<Tx: advice id = "txAdvice" transaction-manager = "transaction manager name">
 
<Tx: attributes>
 
<Tx: method name = "add *" propagation = "REQUIRED"/>
 
<Tx: method name = "del *" propagation = "REQUIRED"/>
 
<Tx: method name = "update *" propagation = "REQUIRED"/>
 
<Tx: method name = "*" read-only = "true"/>
 
</Tx: attributes>
 
</Tx: advice>
 

 
<! -- Configure the methods of which classes require transaction management -->
 
<Aop: config>
 
<Aop: pointcut id = "allManagerMethod" expression = "execution (* com. ibatis. manager. *. * (..)"/>
 
<Aop: advisor advice-ref = "txAdvice" pointcut-ref = "allManagerMethod"/>
 
</Aop: config>
These transactions are declared on the objects at the business logic layer.

Second, we need a Transaction Manager to manage transactions.

[Html]
<Bean id = "txManager" class = "org. springframework. jdbc. datasource. cetcetransactionmanager">
 
<Property name = "dataSource" ref = "dataSource"/>
 
</Bean>
 
<Bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource">
 
<Property name = "driverClassName" value = "com. mysql. jdbc. Driver"/>
 
<Property name = "url" value = "jdbc: mysql: // 127.0.0.1/ibatis"/>
 
<Property name = "username" value = "root"/>
 
<Property name = "password" value = "mysql"/>
 
</Bean>

Later, we need spring to manage SqlMapClient objects:

[Html]
<Bean id = "sqlMapClient" class = "org. springframework. orm. ibatis. SqlMapClientFactoryBean">
 
<Property name = "configLocation"> <value> classpath: sqlMapConfig. xml </value> </property>
 
</Bean>
 

Our sqlMapConfig. xml can be abbreviated:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
 
<! DOCTYPE sqlMapConfig
 
PUBLIC "-// ibatis.apache.org//DTD SQL Map Config 2.0 // EN"
 
Http://ibatis.apache.org/dtd/sql-map-config-2.dtd>
 
<SqlMapConfig>
 
<Settings
 
LazyLoadingEnabled = "true"
 
UseStatementNamespaces = "true"/>
 
<! -- After spring is used, the configuration of the data source is transplanted to spring, so the configuration of iBATIS can be canceled -->
 
<SqlMap resource = "com/ibatis/dao/impl/ibatis/User. xml"/>
 
</SqlMapConfig>
User. xml:

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
 
<! DOCTYPE sqlMap
 
PUBLIC "-// ibatis.apache.org//DTD SQL Map 2.0 // EN"
 
Http://ibatis.apache.org/dtd/sql-map-2.dtd>
 
<SqlMap namespace = "User">
 
<! -- Use type aliases to avoid typing the full classname every time. -->
 
<TypeAlias alias = "User" type = "com. ibatis. User"/>
 
<! -- Select with no parameters using the result map for Account class. -->
 
<Select id = "selectAllUsers" resultClass = "User">
 
Select * from t_user
 
</Select>
 

 
<Select id = "selectUser" resultClass = "User" parameterClass = "int">
 
Select * from t_user where id = # id #
 
</Select>
 

 
<Insert id = "insertUser" parameterClass = "User">
 
Insert into t_user values (
 
Null, # username #, # password #
 
)
 
</Insert>
 

 
<Update id = "updateUser" parameterClass = "User">
 
Update t_user set username = # username #, password = # password #
 
Where id = # id #
 
</Update>
 

 
<Delete id = "deleteUser" parameterClass = "int">
 
Delete from t_user where id = # id #
 
</Delete>
 
</SqlMap>

Write our DAO:

[Java]
Package com. iabtis. dao. impl. ibatis;
 
Import java. util. List;
 
Import org. springframework. orm. ibatis. support. SqlMapClientDaoSupport;
 
Import com. ibatis. dao. UserDAO;
 
Import com. ibatis. crm. model. User;
 
Public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {
 
Public void select (User user ){
 
GetSqlMapClientTemplate (). delete ("selectUser", user. getId ());
 
}
 
Public List findAll (){
 
Return getSqlMapClientTemplate (). queryForList ("selectAllUsers ");
 
}
 
Public void delete (User user ){
 
GetSqlMapClientTemplate (). delete ("deleteUser", user. getId ());
 
}
 
Public void save (User user ){
 
GetSqlMapClientTemplate (). insert ("insertUser", user );
 
}
 
Public void update (User user ){
 
GetSqlMapClientTemplate (). update ("updateUser", user );
 
}
 
}
Inherit from SqlMapClientDaoSupport and require the SqlMapClient object to be injected. Therefore, the following DAO configuration is required:

[Html]
<Bean id = "userDAO" class = "com. ibatils. dao. impl. ibatis. UserDAOImpl">
 
<Property name = "sqlMapClient" ref = "sqlMapClient"/>
 
</Bean>

This is all the issues you need to pay attention to. Now you can call DAO objects at the business logic layer!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.