Spring学习(五)Spring和Mybatis的整合
作者:互联网
1、前言
在整合之前、要搞清楚是谁整合谁、后续会学到很多整合的例子。在这里、是Spring整合Mybatis、Spring中集成了很多关于Mybatis中一些关键类的jar包、通过这些、可以更加方便的联系Mybatis和数据库之间的关系。以前、Mybatis操作数据库的思路是这样的SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD。可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库。Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
2、项目骨架
3、步骤
1、下载相关的jar包
2、创建实体类和表
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>
<!-- 数据库信息 -->
<!-- 加载映射文件 -->
<!-- <mappers>-->
<!-- <mapper resource="com/feng/mapper/studentMapper.xml"></mapper>-->
<!-- </mappers>-->
</configuration>
4、通过mapper.xml将 类、表建立映射关系
studentMapper
package com.feng.mapper;
import com.feng.enetity.Student;
public interface StudentMapper {
public void 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="com.feng.mapper.StudentMapper">
<select id="queryStudentByStuNo" parameterType="int" resultType="com.feng.enetity.Student">
select * from student where stuno = #{stuNo}
</select>
<insert id="addStudent" parameterType="com.feng.enetity.Student">
insert into student(stuno, stuname, stuage) values(#{stuNo},#{stuName},#{stuAge})
</insert>
</mapper>
5、Spring管理SqlSessionFactory
之前使用MyBatis: conf.xml ->SqlSessionFacotry .现在整合的时候,需要通过Spring管理SqlSessionFacotry ,因此 产生qlSessionFacotry 所需要的数据库信息 不在放入conf.xml,而需要放入spring配置文件中配置Spring配置文件(applicationContext.xml).
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的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>
<!-- 在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.xml路径 -->
<property name="mapperLocations" value="com/feng/mapper/*.xml"></property>
</bean>
<bean id="studentMapper" class="com.feng.dao.impl.StudentDaoImpl">
<!--将Spring配置的sqlSessionFactory 对象交给mapper(dao)-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="com.feng.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
</beans>
6、使用Spring-Mybatis整合产物开发程序
目标:通过spring产生mybatis最终操作需要的 动态mapper对象(StudentMapper对象)、Spring产生 动态mapper对象 有3种方法:
a.第一种方式
DAO层实现类继承 SqlSessionDaoSupport类SqlSessionDaoSupport类提供了一个属性 SqlSession
b.第二种方式
就是省略掉 第一种方式的 实现类、直接MyBatis提供的 Mapper实现类:org.mybatis.spring.mapper.MapperFactoryBean 。缺点:每个mapper都需要一个配置一次
c.第三种方式
批量配置 实现类
注:本文用的是第一种方式。
7、三层架构实现
1、Dao层
StudentDaoImpl
package com.feng.dao.impl;
import com.feng.mapper.StudentMapper;
import com.feng.enetity.Student;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public void addStudent(Student student) {
SqlSession session = super.getSqlSession();
StudentMapper stuDao = session.getMapper(StudentMapper.class);
stuDao.addStudent(student);
}
}
2、Service层
1、接口IStudentService
package com.feng.service;
import com.feng.enetity.Student;
public interface IStudentService {
public void addStudent(Student student);
}
2、实现类StudentServiceImpl
package com.feng.service.impl;
import com.feng.enetity.Student;
import com.feng.mapper.StudentMapper;
import com.feng.service.IStudentService;
public class StudentServiceImpl implements IStudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public void addStudent(Student student) {
// 调用Dao
studentMapper.addStudent(student);
}
}
3、数据库配置db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xiao?serverTimezone=GMT%2B8
username=root
password=123456
4、测试
1、Test
package com.feng.test;
import com.feng.enetity.Student;
import com.feng.service.IStudentService;
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");
IStudentService studentService = (IStudentService)context.getBean("studentService");
Student student = new Student();
student.setStuNo(4);
student.setStuAge(81);
student.setStuName("xiaofeng1");
studentService.addStudent(student);
}
}
5、总结
借助Spring相关的jar包、极大的简化了Mybatis的操作、为后续的开发简便了很多步骤。
标签:feng,student,Spring,整合,Mybatis,import,com,public 来源: https://www.cnblogs.com/xiaofrank/p/14454947.html