Spring和Mybatis整合(原始dao)

整合思路
  1. 需要spring通过单例方式管理SqlSessionFactory
  2. spring和mabatis整合生成代理对象,使用SqlSessionFactory创建SqlSession(spring和Mybatis整合自动完成)
  3. 持久层的mapper都需要由spring进行管理

需要spring通过单例方式管理SqlSessionFactory
  • 在applicationContext.xml中配置SqlSessionFactory
  • SqlSessionFactory在mybaits和spring的整合包下
<!-- sqlSessionFactory -->
    <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
原始dao开发(整合后)
  • 加载mybatis的配置文件
<!-- 加载mybaits的配置文件 -->
        <property name="configLocation" value = "mybatis/SqlMapConfig.xml"/>
  • 连接数据库
<!-- 加载配置文件 -->
    <context:property-placeholder location = "classpath:db_properties"/>
    <!-- 数据源,使用dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
  • 数据源(sqlSessionFactory)
<!-- sqlSessionFactory -->
    <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybaits的配置文件 -->
        <property name="configLocation" value = "mybatis/SqlMapConfig.xml"/>
        <!-- 数据源 -->
        <property name="dataSource" ref = "dataSource"/>
    </bean>
mapper.xml
<select id="findUserById" parameterType ="int" resultType="com.shagou.ssm.po.User">
        SELECT * FROM USER WHERE id=#{id}
    </select>
在SqlMapConfig中加载xml
  <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="sqlmap/User.xml"/> 
    </mappers>
dao

dao接口实现类需要注入SqlSessionFactory,通过spring进行注入,这里使用Spring声明配置方式,配置dao的bean让UserDaoImpl实现类继承SqlSessionDaoSupport,配置dao

<!-- 原始dao接口 -->
    <bean id = "userDao" class = "com.shagou.ssm.dao.UserDaoImpl">
        <property name = "sqlSessionFactory" ref = "sqlSessionFactory"></property>
    </bean>
UserDaoImpl
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
        @Override
        public User findUserById(int id) throws Exception {
            //继承SqlSessionDaoSupport,通过this.getSqlSession()得到SqlSession
            SqlSession sqlSession = this.getSqlSession();
            User user = sqlSession.selectOne("test.findUserById",id);
            return user;
        }

}
dao接口
public interface UserDao {
    //根据id查询用户信息
    public User findUserById(int id) throws Exception;
}
测试
        private ApplicationContext applicationContext;
        @Before
        public void setUp()throws Exception{
            applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        }
        @Test
        public void testFindUserById() throws Exception {
            UserDao userDao = (UserDao)applicationContext.getBean("userDao");
            User user = userDao.findUserById(2);
            System.out.println(user);
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值