Spring 整合My Batis 3种方式
前言:
spring 整个 My Batis 的核心就是把My Batis 的sqlSessionFactory 交给spring 管理
开始整合基本的配置:
整合思路 SqlsessionFactoty >Sqlsession>Mapper>CRUD
1,步骤 导入jar
2,基本的类-表,MyBatis 配置conf.xml, mapper.xml 将 类,表建立映射关系
原来的mybatis conf.xml 用来配置 1,加载映射文件 2,数据库配置信息现在将conf.xml 配置交给Spring 的 applicationContext.xml 管理
在applicationContext.xml配置dataSource:
<!-- 配置 连接 db.properties -->
<context:property-placeholder
location="classpath:db.properties" />
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="${datasource.connection.driver_class}" />
<property name="jdbcUrl" value="${datasource.connection.url}" />
<property name="user"
value="${datasource.connection.username}" />
<property name="password"
value="${datasource.connection.password}" />
</bean>
第一种方式 Dao 实现类 继承 sqlSessionDaoSupport
Dao 程实现类 继承 sql sqlSessionDaoSupport类 得到sqlsession 后就和MyBatis 中的使用方法一样, SqlSessionDaoSupport类提供了一个属性 SqlSession
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.dao.support.DaoSupport;
import sm.test.domain.Student;
import sm.test.mapperdao.StudentMapperDao;
/**
* Dao实现类
* @author 翁艳敏
* extends SqlSessionDaoSupport 可以得到session 对象 动态代理
*/
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapperDao {
@Override
public List<Student> findAll() {
SqlSession session = super.getSqlSession();//获得sqlsession
StudentMapperDao mapper = session.getMapper(StudentMapperDao.class);
return mapper.findAll();
}
}
applicationContext.xml 的配置:
sqlSessionFacotry:
<!--在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
<bean id="sqlSessionFacotry"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:conf.xml"> </property>
</bean>
注入bean:
<!-- studentService -->
<bean id="studentService"
class="sm.test.serviceimpl.StudentServiceImpl">
<property name="mapperDao" ref="mapperDao"></property>
</bean>
<!-- mapperDao -->
<bean id="mapperDao" class="sm.test.Daoimpl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFacotry"> </property>
</bean>
就可以测试了:
package sm.test.test;
/**
* 测试类
* @author 翁艳敏
*
*/
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sm.test.domain.Student;
import sm.test.service.StudentService;
public class test1 {
public static void main(String[] args) {
ApplicationContext context1 =
new ClassPathXmlApplicationContext("appliactionContext.xml");
StudentService bean = (StudentService) context1.getBean("studentService");
List<Student> list = bean.findAll();//查询所有
System.out.println(list);
}
}
结果:
第2种方式 省略 实现类 spring -mybatis 整个包动态代理的方式
org.mybatis.spring.mapper.MapperFactoryBean
<!-- studentService -->
<bean id="studentService"
class="sm.test.serviceimpl.StudentServiceImpl">
<property name="mapperDao" ref="mapperDao"></property>
</bean>
<!--第二种 取消 实现类-->
<bean id="mapperDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
<property name="mapperInterface" value="sm.test.mapperdao.StudentMapperDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFacotry"></property>
</bean>
结果:
这种虽然方便了不用实现类,但还是要每个mapper都需要一个配置一次,这里就要用第三种方法了
第三种方法 批量配置 实现类
<!--第三种 批量 动态代理 -->
<!-- 第三种方式生成mapper对象(批量产生多个mapper)
批量产生Mapper对在SpringIOC中的 id值 默认就是 首字母小写接口名 (首字母小写的接口名=id值)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注意这里是value 不是ref -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
<!-- value=扫描的包 -->
<property name="basePackage" value="sm.test.mapperdao"></property>
</bean>
<!-- studentService -->
<bean id="studentService"
class="sm.test.serviceimpl.StudentServiceImpl">
<!--首字母小写接口名 (首字母小写的接口名=id值) -->
<property name="mapperDao" ref="studentMapperDao"></property>
</bean>
注意:首字母小写接口名 (首字母小写的接口名=id值)
不然会:
修改完结果:
总结 :以后使用第三种方式跟方便,感觉Spirng NB!!!