spring-3-spring整合mybatis
版本和依赖
MyBatis-Spring 需要以下版本:

maven依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
第一种方式
spring核心配置文件(IOC)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bean="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<bean:scan base-package="com.wang"/>
<!--类似于mybatis的事务处理部分板块-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--在这个sqlSessionFactoryBean中可以配置几乎全部的mybatis-config.xml的配置项-->
<!--也可以把这个和mybatis-config.xml文件绑定起来
示例:
<property name="configuration" value="classpath:mybatis-config.xml"/>-->
<!--但是:
我们一般只在这个地方子需要配置数据源和连接mybatis-config.xml配置文件就可以了-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--sqlSession:mybatis-spring写了一个模板专门用来帮助我们造sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--构造时必须要有参数-->
<!--且:只能用构造函数加参数,因为无set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--把我们做好的service层构造放入IOC容器中-->
<!--我们要用的时候直接取这个EmployeeMapperImpl,就可以直接获取我们要的数据了-->
<bean id="EmployeeMapperImpl" class="com.wang.service.EmployeeMapperImpl">
<!--帮助我们把sqlSessionTemplate注入-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
mybatis-config.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>
<settings>
<!--日志开关,此处为默认-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<!--开启懒加载,在需要的时候才会读取数据 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭强制加载,即关闭在加载实例的时候就读取全部数据-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<!--package是为这个包下的类都创建一个开头小写的别名-->
<package name="com.wang"/>
</typeAliases>
<mappers>
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
多了一个service的层(用来直接获取Mapper接口实例化好的对象,直接调用对应的方法)
//使用这个类之前需要先传入一个从spring配置文件中得到的sqlSessionTemplate对象
//我们这里一般在springIOC容器中帮助注入
@Component
public class EmployeeMapperImpl implements EmployeeMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<Employee> queryEmployees() {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
return mapper.queryEmployees();
}
}
测试:
@org.junit.Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");
EmployeeMapperImpl employeeMapperImpl = context.getBean("EmployeeMapperImpl", EmployeeMapperImpl.class);
List<Employee> employees = employeeMapperImpl.queryEmployees();
Iterator<Employee> iterator = employees.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
第二种方式
主要在service层发生了修改
@Component
public class EmployeeMapperImpl extends SqlSessionDaoSupport implements EmployeeMapper {
//我们这里直接继承sqlSessionDaoSupport,使用里面的getSqlSession方法直接获取sqlSession
//而不是用DI的方式取注入sqlSession
private SqlSession sqlSession = getSqlSession();
// public void setSqlSession(SqlSessionTemplate sqlSession) {
// this.sqlSession = sqlSession;
// }
public List<Employee> queryEmployees() {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
return mapper.queryEmployees();
}
不需要DI之后
我们就不需要再IOC容器中管理这些Service层了
- 便于管理
- 便于事务操作
在Application.xml核心配置文件中:
- 我们不需要再把sqlsessiontemplate也放入到IOC容器中了
- 但是在我们的service层类中仍需要放到IOC容器中,不过只需要再注入sqlSessionFactory类就可以了
<?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">
<!-- driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
username = root
password = 123456-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg index="0" ref="sqlSessionFactory"/>-->
<!-- </bean>-->
<!-- <bean id="EmployeeMapper" class="com.wang.service.EmployeeMapperImpl">-->
<!-- <property name="sqlSession" ref="sqlSession"/>-->
<!-- </bean>-->
<bean id="userEmployeeMapper" class="com.wang.service.EmployeeMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
spring-3-spring整合mybatis的更多相关文章
- Spring Boot:整合MyBatis框架
综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...
- MyBatis原理,Spring、SpringBoot整合MyBatis
1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- 10、Spring教程之整合MyBatis
1.步骤 1.导入相关jar包 junit <dependency> <groupId>junit</groupId> <artifactId>juni ...
- spring boot 2整合mybatis
mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...
- Spring Boot 应用系列 3 -- Spring Boot 2 整合MyBatis和Druid,多数据源
本文演示多数据源(MySQL+SQL Server)的配置,并且我引入了分页插件pagehelper. 1. 项目结构 (1)db.properties存储数据源和连接池配置. (2)两个数据源的ma ...
- Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid
本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置. 1. pom.xml文件配置 需要在dependencies节点添加: <!-- MySQL --> ...
- SSM(Spring MVC +Spring+Mybatis)整合——maven工程
所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需 ...
- Spring Boot 整合MyBatis(1)
这篇文章介绍如何在Spring boot中整合Mybatis,其中sql语句采用注解的方式插入.后续文章将会介绍,如何使用xml方式. SSM SSH框架已经满足轻量级这个需求了,但是对于开发人员而言 ...
- Spring Boot2 系列教程(二十一)整合 MyBatis
前面两篇文章和读者聊了 Spring Boot 中最简单的数据持久化方案 JdbcTemplate,JdbcTemplate 虽然简单,但是用的并不多,因为它没有 MyBatis 方便,在 Sprin ...
- Spring Boot教程(三十七)整合MyBatis
Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方 ...
随机推荐
- python2向python3移植问题
问题: payload = "A"*140 # padding ropchain = p32(puts_plt) ropchain += p32(entry_point) ropc ...
- windows10下JDK9的环境配置
JDK版本:jdk-9.0.4_windows-x64_bin.exe windows版本:windows10 专业版 64位 需要在系统变量中新建如下3个变量: JAVA_HOME=jdk安装路径J ...
- 【题解】SOFTWARE 二分+搜索/dp
题目描述 一个软件开发公司同时要开发两个软件,并且要同时交付给用户,现在公司为了尽快完成这一任务,将每个软件划分成m个模块,由公司里的技术人员分工完成,每个技术人员完成同一软件的不同模块的所用的天数是 ...
- NOIP模拟测试2「排列 (搜索)·APIO划艇」
排序 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目描述 输入格式 数据范围与提示 对于30%的数据,1<=N<=4: 对于全部的数据,1<=N< ...
- React-Antd4的Form表单校验
之前很少用react做项目,最近入职新公司,用的react,在自己的摸索过程中,慢慢会记录一些使用方法.今天简单记录一下使用antd 4.0版本的Form表单校验,直接代码. 需要购买阿里云产品和服务 ...
- Django-Auth模块之auth_user表
一.Auth模块之auth_user表 在创建Django项目之后直接执行数据迁移命令会自动生成许多表. Django在启动之后就可以直接访问admin路由,需要输入用户名和密码,数据参考的就是aut ...
- 20204107 孙嘉临 《PYTHON程序设计》实验四报告
课程:<Python程序设计>班级: 2041姓名: 孙嘉临学号: 20204107实验教师:王志强实验日期:2020年6月29日必修/选修: 公选课 ##作为一个轻度游戏玩家,当然是要写 ...
- Redis的数据安全与性能保障
1.持久化选项 Redis提供了2种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里.另一种方法叫只追加文件(appen ...
- 乘风破浪,Windows11设计和开发指导,全新图标字体和云母材质
Windows11全新的布局设计 Windows 11全新的布局设计已设计为支持现代应用体验.渐进的圆角.嵌套元素和一致的排水沟相结合,营造出柔和.平静.平易近人的效果,强调目的的统一和易用性. ht ...
- centos 8.3安装 OPENJDK
centos 8.3安装 OPENJDK 查找可安装的OPENJDK [root@xamppr10 ~]# yum search java | grep -i --color openjdk 命令执行 ...