Ibatis,Spring注解方式注入sqlMapClient

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

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; } }

你可能感兴趣的:(DAO,spring,bean,ibatis,user,Integer)