Spring整合MyBatis
Spring - MyBatis
思路:
SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD
可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库,
Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
SM整合步骤:
java项目
jar
mybatis-spring.jar
spring-tx.jar
spring-jdbc.jar
spring-expression.jar
spring-context-support.jar
spring-core.jar
spring-context.jar
spring-beans.jar
spring-aop.jar
spring-web.jar
commons-logging.jar
commons-dbcp.jar
ojdbc.jar
mybatis.jar
log4j.jar
commons-pool.jar
类-表
package org.ycit.entity;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
public Student() {
}
public Student(int stuNo, String stuName, int stuAge) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
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>
<!-- 数据库信息 -->
<!-- 在spring容器中配置 -->
<!-- 加载映射文件studentmapper.xml
<mappers>
<mapper resource="org/ycit/mapper/StudentMapper.xml"></mapper>
</mappers>
-->
</configuration>
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:MyOracleDB
username=scott
password=135451
maxIdle=1000
maxActive=500
通过mapper.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.ycit.mapper.StudentMapper">
<select id="queryStudentbyStuno" parameterType="int" resultType="org.ycit.entity.Student">
select * from student where stuno = #{stuNo}
</select>
<insert id="addStudent" parameterType="org.ycit.entity.Student">
insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})
</insert>
</mapper>
public interface StudentMapper {
public void addStudent(Student student);
}
整合
之前使用MyBatis:conf.xml ->SqlSessionFacotry
现在整合的时候,需要通过Spring管理SqlSessionFacotry ,因此 产生的sqlSessionFacotry 所需要的数据库信息 不在放入conf.xml 而需要放入spring配置文件中
配置Spring配置文件(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 加载db.properties文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations" >
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!--配置数据库的信息(代替mybatis的配置i文件conf.xml) -->
<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="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
</bean>
<!-- 在SpringIoc容器中创建MyBatis的核心类SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis的配置文件
<property name="configLocation" value="classpath:conf.xml"></property>
-->
<!-- 加载,mapper的文件路径 -->
<property name="mapperLocations" value="org/ycit/mapper/*.xml"></property>
</bean>
<bean id="studentService" class= "org.ycit.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
<!--第一种方式生成mapper对象
<bean id="studentMapper" class= "org.ycit.dao.impl.StudentDaoImpl">
dao层注入sqlSessionFactory
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
-->
<!-- 第二种方式生成mapper对象
<bean id="studentMapper" class= "org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.ycit.mapper.StudentMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
-->
<!-- 第三种方式生成mapper对象(批量产生了 多个mapper)
批量产生的mapper对象在springioc中的id值默认为 (首字母小写) 接口名
-->
<bean id="mappers" class= "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定批量产生 那个包中的mapper对象 多个包使用,隔开 -->
<property name="basePackage" value="org.ycit.mapper"></property>
</bean>
</beans>
使用Spring-MyBatis整合产物开发程序
目标:通过spring产生mybatis最终操作需要的 动态mapper对象(StudentMapper对象)
Spring产生 动态mapper对象 有3种方法:
第一种方式
DAO层实现类 继承 SqlSessionDaoSupport类
SqlSessionDaoSupport类提供了一个属性 SqlSession
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{ public void addStudent(Student student) { SqlSession session = super.getSqlSession(); StudentMapper stuDao = session.getMapper(StudentMapper.class); stuDao.addStudent(student); } }
<insert id="addStudent" parameterType="org.ycit.entity.Student"> insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge}) </insert>
<!--第一种方式生成mapper对象 要在dao层注入sqlSessionFactory --> <bean id="studentMapper" class= "org.ycit.dao.impl.StudentDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
<!-- 在SpringIoc容器中创建MyBatis的核心类SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加载mybatis的配置文件 <property name="configLocation" value="classpath:conf.xml"></property> --> <!-- 加载,mapper的文件路径 --> <property name="mapperLocations" value="org/ycit/mapper/*.xml"></property> </bean>
这时可以在spring中配置mapper文件的路径,以前是在mybatis的配置文件conf.xml文件中配置的,此时我们可以在spring中配置,发现mybatis配置文件形同虚设,里面没有配置的信息,因此,spring也不需要加载mybatis的配置文件,所以把加载mybatis的配置文件配置信息注释掉。
此时不在需要我们手动提交,交给spring后会自动提交,以前采用mybatis,采用jdbc手动提交的方式。
第二种方式
就是省略掉 第一种方式的 实现类 直接MyBatis提供的 Mapper实现类:org.mybatis.spring.mapper.MapperFactoryBean
不需要自己再编写实现类
缺点:每个mapper都需要一个配置一次
<bean id="studentMapper" class= "org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.ycit.mapper.StudentMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
第三种方式
批量配置 实现类
<!-- 第三种方式生成mapper对象(批量产生了 多个mapper) 批量产生的mapper对象在springioc中的id值默认为 (首字母小写) 接口名 --> <bean id="mappers" class= "org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 指定批量产生 那个包中的mapper对象 多个包使用,隔开 --> <property name="basePackage" value="org.ycit.mapper"></property> </bean>
批量产生的mapper对象在springioc中的id值默认为 (首字母小写) 接口名
SSM整合:
Spring - SpringMVC - MyBatis
SM
Spring - MyBatis : 需要整合:将MyBatis的SqlSessionFactory 交给Spring
SSM
Spring - SpringMVC : 就是将Spring - SpringMVC 各自配置一遍
思路: SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD 可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库, Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
SM整合步骤:
jar
mybatis-spring.jar spring-tx.jar spring-jdbc.jar spring-expression.jar spring-context-support.jar spring-core.jar spring-context.jar spring-beans.jar spring-aop.jar spring-web.jar commons-logging.jar commons-dbcp.jar ojdbc.jar mybatis.jar log4j.jar commons-pool.jar
类-表
Student类 -student表
配置
(与Spring整合时,conf.xml可省)–MyBatis配置文件conf.xml(数据源、mapper.xml) –可省,将该文件中的配置 全部交由spring管理
spring配置文件 applicationContext.xml
通过mapper.xml将 类、表建立映射关系
配置spring配置文件
配置Spring配置文件(applicationContext.xml) (Web项目):
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
之前使用MyBatis: conf.xml ->SqlSessionFacotry
现在整合的时候,需要通过Spring管理SqlSessionFacotry ,因此 产生SqlSessionFacotry 所需要的数据库信息 不在放入conf.xml 而需要放入spring配置文件中
在applicationContext中配置mybatis信息
配置数据库信息
<!-- 加载db.properties文件 --> <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:db.properties</value> </array> </property> </bean> <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) --> <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> </bean>
使用Spring整合MyBatis :
在SpringIoc容器中 创建MyBatis的核心类
<!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加载mapper.xml路径 --> <property name="mapperLocations" value="classpath:org/lanqiao/mapper/*.xml"></property> </bean>
将MyBatis的SqlSessionFactory 交给Spring
<!-- 将MyBatis的SqlSessionFactory 交给Spring --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="org.lanqiao.mapper"></property> <!--上面basePackage所在的property的作用: 将org.lanqiao.mapper包中,所有的接口 产生与之对应的 动态代理对象 (对象名 就是 首字母小写的接口名) --> </bean>
继续整合SpringMVC
将springmvc加入项目即可
加入SpringMVC需要的jar
spring-webmvc.jar
给项目加入SpringMVC支持
web.xml: dispatcherServlet
<!-- 整合SPringMVC --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-controller.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
编写springmvc配置文件:
applicationContext-controller.xml:视图解析器、基础配置
<!-- 将控制器所在包 加入IOC容器 --> <context:component-scan base-package="org.lanqiao.controller"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- SPringMVC基础配置、标配 --> <mvc:annotation-driven></mvc:annotation-driven>