iBATIS与Spring集成及环境搭建
环境搭建依赖包:
Spring 2.5.6依赖包:
spring.jar
aspectjrt.jar
aspectjweaver.jar
commons-dbcp.jar
commons-pool.jar
commons-beanutils.jar
iBATIS依赖包:
ibatis-2.3.0.677.jar
mysql-connector-java-3.1.13-bin.jar
将spring的配置文件bean.xml中关于hibernate的配置删除使用DataSourceTransactionManager来管理事务。
iBATIS配置文件sqlMapConfig.xml
<sqlMapConfig><!-- 启用懒加载策略,使用Namespaces --> <settings lazyLoadingEnabled="true" useStatementNamespaces="true"/> <!-- 使用spring之后,数据源的配置移植到了spring上,所以iBATIS本身的配置可以取消 --><sqlMap resource="com/lzh/crm/model/User.xml"/></sqlMapConfig>
<!-- 配置事务特性 --><tx:advice id="txAdvice" transaction-manager="事务管理器名称"><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><!-- 配置哪些类的方法需要进行事务管理 --><aop:config><aop:pointcut id="allManagerMethod" expression="execution(* com.lzh.ibatis.manager.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/></aop:config>
<bean id="txManager" ref="dataSource"/> </bean> <bean id="dataSource" 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="123"/> </bean>
<bean id="sqlMapClient" ref="dataSource"></property></bean>
public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {public void delete(User user) {getSqlMapClientTemplate().delete("delete",user.getId());}public List findAll() {return getSqlMapClientTemplate().queryForList("findAll");}public User findById(int id) {return (User)getSqlMapClientTemplate().queryForObject("findById", id);}public void save(User user) {getSqlMapClientTemplate().insert("save",user);}public void update(User user) {getSqlMapClientTemplate().update("update",user);}}
<bean id="userDAO" />
User实体类:public class User {private int id;private String username;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}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;}}
User.xml配置文件:<sqlMap namespace="User"> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias="User" type="com.lzh.crm.model.User"/> <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllUsers" result> select * from t_user </select> <select id="selectUser" resultparameterresultparameterproperty="username">(username like '%$username$%')</isNotEmpty><isNotEmpty prepend="and" property="password">(password like '%$password$%')</isNotEmpty></dynamic> </select> <insert id="insertUser" parameterparameterparametername="code">public void testAddUser() {UserManager um = (UserManager)factory.getBean("userManager");User user = new User();user.setUsername("User"+new Random().nextInt(9999));user.setPassword("Pass"+new Random().nextInt(9999));um.addUser(user);}public void testAddUsers() {UserManager um = (UserManager)factory.getBean("userManager");for(int i=0; i<100; i++){User user = new User();user.setUsername("User"+new Random().nextInt(9999));user.setPassword("Pass"+new Random().nextInt(9999));um.addUser(user);}}public void testSearchUsers(){UserManager um = (UserManager)factory.getBean("userManager");User user = new User();user.setUsername("1");user.setPassword("1");List users = um.searchUsers(user);for (Iterator iterator = users.iterator(); iterator.hasNext();) {User u = (User) iterator.next();System.out.println(u.getUsername()+","+u.getPassword());}}
最后在贴一下sql建表语句:create table t_user(ID integer not null auto_increment,USERNAME varchar(255),PASSWORD varchar(255),primary key(ID));