<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.propertiesvalue>
array>
property>
bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}">property>
<property name="url" value="${url}">property>
<property name="username" value="${username}">property>
<property name="password" value="${password}">property>
<property name="maxIdle" value="${maxIdle}">property>
<property name="maxActive" value="${maxActive}">property>
bean>
<bean id="studentMapper" class="org.dsl.dao.impl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>
<bean id="studentService" class="org.dsl.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="classpath:conf.xml">property>
bean>
beans>
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORACLEDSL
username=scott
password=tiger
maxIdle=1000
maxActive=500
package org.dsl.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.dsl.entity.Student;
import org.dsl.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public void addStudent(Student student) {
// TODO Auto-generated method stub
SqlSession session = super.getSqlSession();
//获取动态mapper对象
StudentMapper stuDao = (StudentMapper) session.getMapper(StudentMapper.class);
stuDao.addStudent(student);
}
}
注:DAO的接口三层时的类名可以叫StudentDao,但是为了mapper接口和映射文件对应,所以建议将IStudentDao.java接口的名字改为StudentMapper与接口映射文件同名,接口映射文件的首字母小写。并且建议放在同一个类路径下,为了减少不必要配置。位置和文件如下:
1.Mapper 接口方法名和 Mapper.xml 中定义的每个 statement 的 id 同名。
2.Mapper 接口方法的输入参数类型和 rMapper.xml 中定义的 statement 的parameterType 类型相同。
3.Mapper 接口的返回类型和Mapper.xml 中定义的 statement 的 resultType 类型相同。
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!- namespace为接口全限定类名 -->
<mapper namespace = "org.dsl.mapper.StudentMapper">
<insert id="addStudent" parameterType="org.dsl.entity.Student">
insert into student(stuNo,stuName,stuAge) values(#{stuNo},#{stuName},#{stuAge})
insert>
mapper>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<mappers>
<mapper resource="org/dsl/mapper/studentMapper.xml"/>
mappers>
configuration>
最后将Dao层与sqlSessionFactory对象建立联系,将已经注入的sqlSessionFactory对象以属性的方式配置给Dao层。
配置完成调用测试
package org.dsl.test;
import org.dsl.entity.Student;
import org.dsl.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService)context.getBean("studentService");
Student student = new Student();
student.setStuNo(2);
student.setStuName("zds");
student.setStuAge(20);
studentService.addStudent(student);
}
}
可省略提醒:src路径下面的mybatis配置文件conf.xml在spring整合mybatis时,是可以不用写的,因为在配置SqlSessionFactory时org.mybatis.spring.SqlSessionFactoryBean类中,有获取mapper映射文件的属性,mapperLocations该属性是数组,可以配置多个,可以使用通配符进行全包检索即可。当没有conf.xml文件时,就不能配置configLocation属性查找conf.xml文件,否则会报错。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations" value="org/dsl/mapper/*.xml">property>
bean>
主要步骤:在springIOC容器中配置数据库信息DataSource—>创建SqlSessionFactory对象(读取数据库信息、加载mybatis配置文件)—>将SqlSessionFactory对象与Dao层(StudentMapper)关联。
第二种,利用mybatis-spring.jar中提供的org.mybatis.spring.mapper.MapperFactoryBean类实现动态mapper获取
在第一种方法基础上删除StudentDaoImpl类和springIOC容器中的StudentDaoImpl的相关配置,利用mybatis-spring.jar中提供的org.mybatis.spring.mapper.MapperFactoryBean类中提供的属性指定mapper接口文件StudentMapper(即StudentDao的接口类)。
第二种方式缺点:如果存在多个类似于StudentMapper需要使用时,就要定义配置多个bean,代码较为冗余。
第三种,利用mybatis-spring.jar中提供的Mapper扫描器(org.mybatis.spring.mapper.MapperScannerConfigurer),扫描指定的包路径下的所有mapper.xml配置文件。其中配置sqlSessionFactory对象的属性与前两种不同,需要使用sqlSessionFactoryBeanName属性才可以,并且需要指定要扫描的包,在利用相应的mapper属性时,在中ref需要配置mapper.java的文件名称首字母小写。
三种配置方法applicationContext.xml文件配置,需要时保留一种方式,否则会报错
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.propertiesvalue>
array>
property>
bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}">property>
<property name="url" value="${url}">property>
<property name="username" value="${username}">property>
<property name="password" value="${password}">property>
<property name="maxIdle" value="${maxIdle}">property>
<property name="maxActive" value="${maxActive}">property>
bean>
<bean id="studentMapper" class="org.dsl.dao.impl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.dsl.mapper.StudentMapper">property>
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.dsl.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="studentService" class="org.dsl.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations" value="org/dsl/mapper/*.xml">property>
bean>
beans>