pom.xml
org.apache.ibatis
ibatis-sqlmap
2.3.4.726
Spring-config.xml
BaseDao.javapackage 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 findUsersByUsername(String username,int pageNo) {
Pages pages=new Pages(getTotalCountByUsername(username));
pages.setPageSize(5);
List userList=new ArrayList();
Map map=new HashMap();
map.put("username", username);
map.put("start", pages.getStart(pageNo));
userList=(List) getSqlMapClientTemplate().queryForObject("findUsersByUsername", map);
pages.setPageList(userList);
return pages;
}
/**
* 查询所有记录
* @return
*/
public int getTotalCount(){
return (Integer) getSqlMapClientTemplate().queryForObject("getTotalCount");
}
@SuppressWarnings("unchecked")
public Pages findAllPageUser(int pageNo){
Pages pages=new Pages(getTotalCount());
pages.setPageSize(5);
List userList=new ArrayList();
userList=(List) getSqlMapClientTemplate().queryForList("findAllPageUser", pages.getStart(pageNo));
pages.setPageList(userList);
return pages;
}
}