思路:
单独mybatis开发步骤:
SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD
可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库,
Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
SM整合步骤:
1、 jar
spring的jar
spring-tx.jar spring-jdbc.jar spring-expression.jar
spring-core.jar spring-beans.jar spring-aop.jar
spring-context-support.jar
spring-context.jar //spring 事务
mybatis-spring.jar //整合jar
mybatis.jar
commons-dbcp.jar commons-pool.jar
log4j.jar commons-logging.jar
mysql-connector-java.jar
2 、类-表
Student.java
package org.zq.entity;
import lombok.Data;
@Data
public class Student {
private int stuId;
private String stuName;
private int age;
}
表:student
3 、MyBatis配置文件conf.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 数据库信息 -->
<!-- 加载映射文件 studentMapper.xml -->
<mappers>
<mapper resource="org/zq/mapper/stuMapper.xml"></mapper>
</mappers>
</configuration>
4.通过mapper.xml将 类、表建立映射关系
studentMapper
package org.zq.mapper;
import org.zq.entity.Student;
public interface StudentMapper {
int addStudent(Student student);
}
studentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.zq.mapper.StudentMapper">
<insert id="addStudent" parameterType="org.zq.entity.Student" >
insert into student(stuid,stuname,age) values(#{stuId},#{stuName},#{age})
</insert>
</mapper>
5、将conf.xml 交由spring托管
之前使用MyBatis: conf.xml ->SqlSessionFacotry
现在整合的时候,需要通过Spring管理SqlSessionFacotry ,
因此 产生qlSessionFacotry 所需要的数据库信息 不在放入conf.xml 而需要放入spring配置文件中
配置Spring配置文件(applicationContext.xml)
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
uname=root
password=password
maxIdle=1000
maxActive=500
applicationContext.xml
<!--加载配置文件 db.properties 方便修改 PreferencesPlaceholderConfigurer 加载类 -->
<bean id="configurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 数据源 dbcp数据源 -->
<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="${uname}"></property>
<property name="password" value="${password}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="maxActive" value="${maxActive}"></property>
</bean>
<!--在spring中产生sqlsessionfactory 需要数据源-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 将mybatis配置文件交由spring -->
<property name="configLocation" value="classpath:conf.xml"></property>
</bean>
6、 使用Spring-MyBatis整合产物开发程序
目标:通过spring产生mybatis最终操作需要的 动态mapper对象(StudentMapper对象)
Spring产生 动态mapper对象 有3种方法:
a.第一种方式
DAO层实现类 继承 SqlSessionDaoSupport类 实现StudentMapper接口
studentDao.java
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public int addStudent(Student student) {
SqlSession sqlSession = super.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.addStudent(student);
}
}
通过xml的形式 将dao、service层加入ioc容器
<bean id="studentDao" class="org.zq.dao.impl.StudentDaoImpl">
//dao 层必须配置的属性
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="studentService" class="org.zq.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
下面的配置可以省略 mybatis的配置文件 直接配置mapper
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="org/zq/mapper/*.xml"/> //加载目录下的所有配置文件
</bean>
以下方式 mybatis的配置文件都不存在
b.第二种方式
就是省略掉 第一种方式的 实现类
直接MyBatis提供的 Mapper实现类:org.mybatis.spring.mapper.MapperFactoryBean
缺点:每个mapper都需要一个配置一次
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="org/zq/mapper/*.xml"/>
</bean>
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="org.zq.mapper.StudentMapper"></property>
</bean>
<bean id="studentService" class="org.zq.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>
Dao层不用再写
c.第三种方式
批量配置 实现类
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="org/zq/mapper/*.xml"/>
</bean>
<!--批量产生-->
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="org.zq.mapper"/>
</bean>
<!-- 批量引入(有多个) 在IOC中的值 默认是接口名 = ID值 并首字母小写-->
<bean id="studentService" class="org.zq.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>
d、通过注解实现
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="org/zq/mapper/*.xml"/>
</bean>
<!--批量产生-->
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="org.zq.mapper"/>
</bean>
<context:component-scan base-package="org.zq.service"/>
使用的到注解
@Component @Service @Autowired
- 提醒: 如果使用maven构建项目, 如果*.xml文件找不到 ,请将文件方法resource目录中。
因为maven打包回忽略除 .java以外的文件