1.dao组件继承org.springframework.jdbc.core.support.JdbcDaoSupport
applicationContext.xml文件中配置
id = "jdbcProperties" location = "classpath:db.properties">
id = "myDataSource2" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close">
<property name="driverClassName" value ="#{jdbcProperties.driver}">property>
<property name="url" value="#{jdbcProperties.url}">property>
<property name="username" value="#{jdbcProperties.user}">property>
<property name="password" value="#{jdbcProperties.pwd}">property>
id = "empDao01" class = "hateapple.dao.EmpDao01">
<property name="dataSource" ref = "myDataSource2">property>
empDao01通过setter注入dataSource,set方法继承自JdbcDaoSupport,且不能被覆盖重写
//set方法签名
public final void setDataSource(DataSource dataSource)
其实就是注入的dataSource被父类JdbcDaoSupport拿去初始化自己的成员变量jdbcTemplate了,empDao01想要使用jdbcTemplate只能通过getJdbcTemplate。
2.不继承
org.springframework.jdbc.core.support.JdbcDaoSupport,为empDao02注入jdbcTemplate,empDao02通过jdbcTemplate操作数据库;
applicationContext.xml文件配置
id = "myDataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver">property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">property>
<property name="username" value="em">property>
<property name="password" value="em">property>
id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "myDataSource">property>
id = "empDao02" class = "hateapple.dao.EmpDao02">
<property name="jdbcTemplate" ref = "jdbcTemplate">property>
对比两种方式,第一种是把钥匙交给门卫用的时候向门卫拿,第二种自己带身上。
1.spring整合mybatis 的核心是 SqlSessionFactoryBean、MapperFactoryBean(单一接口)
org.mybatis.spring.SqlSessionFactoryBean包含了dataSource(数据源)、mapperLocations(接口的mapper映射文件路径);
org.mybatis.spring.mapper.MapperFactoryBean包含了sqlSessionFactory(上面的bean),mapperInterface(接口的完整名称);
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "myDataSource">property>
<property name="mapperLocations" value = "classpath:hateapple/mapper/StudentMapperMyBatis.xml">property>
bean>
<bean id="studentMapper" class= "org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="hateapple.dao.BaseDao">property>
<property name="sqlSessionFactory" ref ="sqlSessionFactory">property>
bean>
测试代码
@Test
public void testMybatis(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
BaseDao baseDao = (BaseDao)ac.getBean("studentMapper");
List studentList = baseDao.findAll();
for (Student student : studentList) {
System.out.println(student.getName());
}
如果需要多个org.mybatis.spring.mapper.MapperFactoryBean,一个一个配置肯定不现实
2. MapperScannerConfigurer批量扫描接口,并为每个接口生成一个 MapperFactoryBean的实例
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value ="hateapple.dao">property>
<property name="sqlSessionFactory" ref ="sqlSessionFactory">property>
<property name="annotationClass" value="hateapple.annotation.MyMapperAnnotation">property>
bean>
MyMapperAnnotation .java
public @interface MyMapperAnnotation {
}
BaseDao.java
@MyMapperAnnotation
public interface BaseDao {
public List findAll();
}
测试代码:
@Test
public void testMybatis(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
BaseDao baseDao = (BaseDao)ac.getBean("baseDao");
List studentList = baseDao.findAll();
for (Student student : studentList) {
System.out.println(student.getName());
}
其实org.mybatis.spring.mapper.MapperFactoryBean就是封装了一个SqlSessionTemplate操作数据库,我们调用baseDao.findAll()最终的操作还是sqlSessionTemplate.selectList(“findAll”)
1.直接为操作数据库类注入sqlSessionTemplate
id ="sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate">
"0" ref="sqlSessionFactory">
//sqlSessionTemplate是被注入进来的
@Override
public List findAll() {
List studentList = sqlSessionTemplate.selectList("findAll");
for (Student student : studentList) {
System.out.println(student.getName());
}
return studentList;
}