spring和mybatis的整合开发(传统Dao开发方式)

spring和mybatis整合开发有三种整合方式1.传统DAO方式的开发整合(现在基本上不会用这种方式了,不推荐使用这种方式),2.mapper接口方式的开发整合(基于MapperFactoryBean的整合和基于MapperScannerConfigurer的整合)

mybatis和spring的开发整合环境:

1.mybatis核心jar包和解压过后lib目录下的所有jar包

2.spring核心jar包,以及aop开发要用到的jar包,关于事务的jar包,jdbc jar包,这些jar包都在下载的spring框架包中能找到

3.日志jar包,数据库驱动jar包,DBCP数据源jar包,以及连接池jar包,junit测试jar包

4.spring和mybatis整合的中间件,mybatis-spring jar包

整合所需要的配置文件

1.mybatis-config.xml 2.applicationContext.xml 3.db.properties 4.log4j.properties 5.xxxMapper.xml 

传统DAO方式的开发整合

采用传统DAO方式的开发整合需要编写DAO接口以及接口的实现类 ,并且需要向DAO实现类中注入SqlSessionFactory,然后在方法体内通过SqlSessionFactory创建sqlsession。所以可以使用mybatis-spring包中的SqlSessionTemplate类和SqlSessionDaoSupport类来实现。

SqlSessionTemplate类是mybatis-spring的核心类,他负责管理mybatis的sqlsession,调用mybatis的sql方法。当调用sql方法时,SqlSessionTemplate将会保证使用的SqlSession和当前的spring的事务是相关的。并且他还管理bean的生命周期,包含必要的关闭,提交和回滚等。

SqlSessionDaoSupport类:是一个抽象支持类,他继承了DaoSupport类,主要是作为DAO的基类来使用。可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取需要的SqlSession。

例如

/*客户持久化类*/

public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
  setter/getter......
   @override
    toString()
}

CustomerMapper映射文件
根据id查找客户
<mapper namespace="com.itheima.po.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
记得在mybatis-config.xml中配置mapper的路径

接口CustomerDao
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}

接口实现类CustomerDaoImpl
CustomerDaoImpl需要先继承SqlSessionDaoSupport类,然后实现CustomerDao接口
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
@Override
public Customer findCustomerById(Integer id) {
return this.getSqlSession().selectOne("com.itheima.po.CustomerMapper.findCustomerById", id);
}

}
在applicationContext.xml中实例化customerDao
<!--实例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--注入sqlsessionFactory对象实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
测试
public class FindCustomerByIdTest {
@Test
public void findCustomerById(){
ApplicationContext applicationContex = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDao=applicationContex.getBean(CustomerDao.class);
Customer customer=customerDao.findCustomerById(2);
System.out.println(customer);
}
}
测试结果
Customer{id=2, username='chen', jobs='java', phone='456789'}
application.xml中完整配置
<!--读取db.properties配置信息-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--连接数据库的url地址-->
<property name="url" value="${jdbc.url}"/>
<!--连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!--连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!--最大连接数-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!--最大空闲连接-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始化连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<!--事务管理器,依赖于数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置Mybatis工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--指定核心配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--实例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--注入sqlsessionFactory对象实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

猜你喜欢

转载自www.cnblogs.com/jasonboren/p/10594586.html