目录
2.5定义 StudentDao 接口和对应的mapper映射文件
2.8.4 定义Mapper扫描配置器MapperScannerConfigurer
1.spring集成mybatis相关说明
将 MyBatis 与 Spring 进行整合,主要解决的问题就是将SqlSessionFactory 对象交由 Spring 来管理。所以,该整合,只需要将SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。
实现 Spring 与 MyBatis 的整合常用的方式:扫描的 Mapper 动态代理Spring 像插线板一样,mybatis 框架是插头,可以容易的组合到一起。插线板 spring 插上 mybatis,两个框架就是一个整体。
使用mybatis,需要创建mybatis框架中的某些对象,使用这些对象,就可以使用mybatis提供的功能了。
对于mybatis执行sql语句,需要用到的对象有:
1. SqlSessionFactory对象,只有创建了SqlSessionFactory对象,才能调用openSession()方法得到SqlSession对象。
2. dao接口的代理对象,例如StudentDao接口,需要的代理对象为:SqlSeesion.getMapper(StudentDao.class)。
3. 数据源DataSource对象,使用一个更强大、功能更多的连接池对象代替mybatis自己的PooledDataSource。
![]()
2.实现步骤
2.1实现步骤说明
Spring集成mybatis 实现步骤: 1.使用mysql库,创建表。 2.创建maven项目。 3.添加gav (依赖级) Spring依赖、mybatis依赖、mysql驱动、junit依赖 mybatis-spring依赖(mybatis网站上提供的,用来在spring项目中,创建mybatis对象) spring有关事务的依赖。 mybatis和spring整合的时候,事务自动提交的。 4.创建实体Student 5.创建Dao接口和mapper文件写sql语句 6.写mybatis主配置文件 7.创建service接口和他的实现类 8.创建spring的配置文件 1)声明数据源DataSource,使用的是阿里的Druid连接池 2)声明SqlSessionFactoryBean类,在这个类内部创建的是SqlSessionFactory对象。 3)声明MapperScannerConfiguration类,在内部创建dao代理对象,创建的对象放都到Spring容器中。 4)声明Service对象,把3)中的dao赋值给service属性 9.测试dao访问数据库
2.2准备数据库,MySQL 创建新建表 Student
2.3 maven 依赖 pom.xml
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring事务依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis和spring集成依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- 阿里的连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<build>
<resources>
<resource>
<directory>src/main/java</directory> <!--所在的目录-->
<includes> <!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.4 实体类(Student)
package com.liuhaiyang.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
//set和get方法以及tostring、构造方法等
}
2.5定义 StudentDao 接口和对应的mapper映射文件
package com.liuhaiyang.dao;
import com.liuhaiyang.domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudent();
}
<?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">
<mapper namespace="com.liuhaiyang.dao.StudentDao">
<!--使用insert,uodate,delete,select标签写sql-->
<insert id="insertStudent" >
insert into student2(name,age) values (#{name},#{age})
</insert>
<select id="selectStudent" resultType="com.liuhaiyang.domain.Student">
select * from student2
</select>
</mapper>
2.6 编写MyBatis主配置文件
<?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>
<!-- 设置日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 别名-->
<!-- <typeAliases>-->
<!-- <package name="com.liuhaiyang.domain"/>-->
<!-- </typeAliases>-->
<!--指定其他mapper文件的位置 才能找到其他文件sql语句-->
<mappers>
<!-- <mapper resource="com/lhy/dao/StudentDao.xml"/>-->
<!-- 使用要求:
1.mapper文件和dao接口在同一个目录
2.mapper文件和dao接口名称一样
-->
<package name="com.liuhaiyang.dao"/>
</mappers>
</configuration>
2.7定义 Service 接口和实现类
package com.liuhaiyang.service;
import com.liuhaiyang.domain.Student;
import java.util.List;
public interface StudentService {
int addSttudent(Student student);
List<Student> selectStudent();
}
package com.liuhaiyang.service.impl;
import com.liuhaiyang.dao.StudentDao;
import com.liuhaiyang.domain.Student;
import com.liuhaiyang.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
// @Autowired(required = false)
// @Qualifier("studentDao")
private StudentDao studentDao=null;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addSttudent(Student student) {
int a=studentDao.insertStudent(student);
System.out.println("本次执行结果影响了"+a+"行");
return a;
}
@Override
public List<Student> selectStudent() {
List<Student> stu=studentDao.selectStudent();
return stu;
}
}
2.8 编写Spring配置文件
jdbc.properties配置文件(数据库的相关消息)
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456(数据库密码)
2.8.1 加载外部属性配置文件
<!-- 加载外部属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
2.8.2声明数据源
<!-- 声明数据源DataSource -->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
2.8.3 注册SqlSessionFactoryBean
<!-- 声明SqlSessionFactoryBean,在这个类的内部,创建SqlSessionFactory对象,之后就可以获取SqlSession对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 指定mybatis主配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
2.8.4 定义Mapper扫描配置器MapperScannerConfigurer
<!-- 声明MapperScannerConfigurer -->
<!--
MapperScannerConfigurer作用:
循环basePackage所表示的包,把包中的每个接口都找到,调用SqlSession.getMapper(XXXDao.class)
把每个dao接口都创建出对应的dao代理对象,将dao代理对象放在容器中
对于StudentDao接口,其代理对象为 studentDao
相当于:
ApplicationContext ctx=...
SqlSessionFactory sqlSessionFactory=ctx.getBean("factory");
for(接口: com.liuhaiyang.dao){
接口 对象=session.getMapper(接口)
springMap.put(对象名, 对象)
}
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定SqlSessionFactory对象的名称 -->
<property name="sqlSessionFactoryBeanName" value="factory"/>
<!-- 指定基本包,dao接口所在的包名 -->
<property name="basePackage" value="com.liuhaiyang.dao"/>
</bean>
2.8.5 向Service中注入相关的接口名
<!-- 声明service-->
<bean id="studentService" class="com.liuhaiyang.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
2.9 编写测试方法
2.9.1 测试方法1
@Test
public void tset02(){
//StudentDao操作,和属于mybatis内容
String config="application.xml";
ApplicationContext app=new ClassPathXmlApplicationContext(config);
StudentDao student=(StudentDao) app.getBean("studentDao"); //这个是由容器创建的。主要是容器的第三个模块
Student stu=new Student();
stu.setName("张三");
stu.setAge(26);
int a=student.insertStudent(stu);
System.out.println(a);
}
结果截图:
2.9.2 测试方法2
@Test
public void test03(){
//StudentService操作的,是将mybatis和Spring结合起来
String config="application.xml";
ApplicationContext app=new ClassPathXmlApplicationContext(config);
StudentService service=(StudentService) app.getBean("studentService");
List<Student> students=service.selectStudent();
for (Student student:students) System.out.println(student);
}
结果截图: