Ibatis,Spring注解方式注入sqlMapClien

pom.xml

<dependency>
    <groupId>org.apache.ibatis</groupId>
    <artifactId>ibatis-sqlmap</artifactId>
    <version>2.3.4.726</version>
</dependency>

Spring-config.xml

<!-- sqlMapCLient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:sqlmap-config.xml"/>
    <property name="dataSource" ref="dataSource" />
</bean>
<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <!-- BoneCP type -->
    <property name="driverClassName" value="${jdbc.driverClass}" />
    <property name="url" value="${jdbc.jdbcUrl}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

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


你可能感兴趣的:(学习笔记)