applicationContext.xml
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" xmlns:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <!-- 自动扫描Bean --> < context:component-scan base-package ="com.tq365" /> <!-- 强制使用CGLIB代理 --> < aop:aspectj-autoproxy proxy-target-class ="true" /> <!-- DataSource数据源 --> < bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" > < property name ="driverClassName" > < value > com.mysql.jdbc.Driver </ value > </ property > < property name ="url" > < value > jdbc:mysql://localhost:3306/ibatisorm </ value > </ property > < property name ="username" > < value > root </ value > </ property > < property name ="password" > < value > root </ value > </ property > </ bean > <!-- Spring iBatis SqlMapClient --> < bean id ="sqlMapClient" class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" > < property name ="configLocation" value ="WEB-INF/sqlmap-config.xml" /> < property name ="dataSource" ref ="dataSource" /> </ bean > <!-- 事务管理DataSourceTransactionManager --> < bean id ="txManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name ="dataSource" ref ="dataSource" /> </ bean > <!-- 启动spring注解功能 --> < tx:annotation-driven transaction-manager ="txManager" /> < aop:config > < aop:pointcut id ="baseServiceMethods" expression ="execution(* com.tq365.samples.ibatis.dao.*.*(..))" /> < aop:advisor advice-ref ="txAdvice" pointcut-ref ="baseServiceMethods" /> </ aop:config > < aop:aspectj-autoproxy /> < tx:advice id ="txAdvice" transaction-manager ="txManager" > < tx:attributes > < tx:method name ="select*" read-only ="true" propagation ="REQUIRED" /> < tx:method name ="find*" read-only ="true" propagation ="REQUIRED" /> < tx:method name ="save*" propagation ="REQUIRED" isolation ="REPEATABLE_READ" /> < tx:method name ="update*" propagation ="REQUIRED" isolation ="REPEATABLE_READ" /> <!-- <tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" /> --> < tx:method name ="delete*" propagation ="REQUIRED" isolation ="REPEATABLE_READ" /> </ tx:attributes > </ tx:advice > </ beans >
sqlmap-config.xml
<? 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 > < sqlMap resource ="com/tq365/sqlMap/User.xml" /> </ sqlMapConfig >
User.xml
<? 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" > < typeAlias alias ="User" type ="com.tq365.samples.ibatis.model.User" /> < resultMap id ="UserResult" class ="User" > < result property ="id" column ="id" /> < result property ="username" column ="username" /> < result property ="address" column ="address" /> < result property ="userpwd" column ="userpwd" /> </ resultMap > < insert id ="addUser" > insert user( username, userpwd, address ) values( #username#, #userpwd#, #address# ) </ insert > < select id ="findUser" resultMap ="UserResult" > select * from user WHERE username=#username# and userpwd=#userpwd# </ select > < select id ="findUserById" resultMap ="UserResult" > select * from user WHERE id=#id# </ select > <!-- User表内记录总条数 --> < select id ="getTotalCount" resultClass ="int" > select count(*) from user </ select > < select id ="findAllPageUser" resultMap ="UserResult" > select * from user limit #start#,5 </ select > </ sqlMap >
UserDAO.java接口
package com.tq365.samples.ibatis.dao; import org.springframework.stereotype.Repository; import com.tq365.samples.ibatis.model.User; import com.tq365.sqmples.util.Pages; /** * UserDAO 接口 */ @Repository public interface UserDAO { /** * 通过ID查询 * @param id * @return */ public User findUserById( int id); /** * 通过ID删除 * @param id */ public void deleteUserById( int id); public void addUser(User user); /** * 查询指定的User对象 * @param user * @return */ public User findUser(User user); /** * 通过用户名分页查询 * @param username * @return */ public Pages < User > findUsersByUsername(String username, int pageNo); /** * 所有User对象分页 * @param pageNo * @return */ public Pages < User > findAllPageUser( int pageNo); }
BaseDao.java
package com.tq365.samples.ibatis.dao; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.ibatis.sqlmap.client.SqlMapClient; /** * BaseDao,Dao需继承此Dao * @author archie2010 * since 2011-3-3 下午02:52:36 */ public class BaseDao extends SqlMapClientDaoSupport{ @Resource(name = " sqlMapClient " ) private SqlMapClient sqlMapClient; @PostConstruct public void initSqlMapClient(){ super .setSqlMapClient(sqlMapClient); } }
@PostConstruct注解
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并 装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的 setter方法时
UserDAOimpl.java
package com.tq365.samples.ibatis.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Component; import com.tq365.samples.ibatis.model.User; import com.tq365.sqmples.util.Pages; /** * UserDAO接口实现类 * @author archie2010 * since 2011-3-3 下午02:54:57 */ @Component( " userDAO " ) public class UserDAOimpl extends BaseDao implements UserDAO{ public User findUserById( int id){ return (User) getSqlMapClientTemplate().queryForObject( " findUserById " , id); } /** * 添加User */ public void addUser(User user) { getSqlMapClientTemplate().insert( " addUser " , user); } /** * 删除User */ public void deleteUserById( int id) { getSqlMapClientTemplate().delete( " deleteUserById " , id); } /** * 查找User */ public User findUser(User user) { return (User) getSqlMapClientTemplate().queryForObject( " findUser " , user); } public int getTotalCountByUsername(String username){ return (Integer) getSqlMapClientTemplate().queryForObject( " getTotalCountByUsername " , username); } /** * User分页 */ @SuppressWarnings( " unchecked " ) public Pages < User > findUsersByUsername(String username, int pageNo) { Pages < User > pages = new Pages < User > (getTotalCountByUsername(username)); pages.setPageSize( 5 ); List < User > userList = new ArrayList < User > (); Map < String, Object > map = new HashMap < String, Object > (); map.put( " username " , username); map.put( " start " , pages.getStart(pageNo)); userList = (List < User > ) getSqlMapClientTemplate().queryForObject( " findUsersByUsername " , map); pages.setPageList(userList); return pages; } /** * 查询所有记录 * @return */ public int getTotalCount(){ return (Integer) getSqlMapClientTemplate().queryForObject( " getTotalCount " ); } @SuppressWarnings( " unchecked " ) public Pages < User > findAllPageUser( int pageNo){ Pages < User > pages = new Pages < User > (getTotalCount()); pages.setPageSize( 5 ); List < User > userList = new ArrayList < User > (); userList = (List < User > ) getSqlMapClientTemplate().queryForList( " findAllPageUser " , pages.getStart(pageNo)); pages.setPageList(userList); return pages; } }