版本和依赖

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&amp;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&amp;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的更多相关文章

  1. Spring Boot:整合MyBatis框架

    综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  2. MyBatis原理,Spring、SpringBoot整合MyBatis

    1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  3. 10、Spring教程之整合MyBatis

    1.步骤 1.导入相关jar包 junit <dependency> <groupId>junit</groupId> <artifactId>juni ...

  4. spring boot 2整合mybatis

    mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...

  5. Spring Boot 应用系列 3 -- Spring Boot 2 整合MyBatis和Druid,多数据源

    本文演示多数据源(MySQL+SQL Server)的配置,并且我引入了分页插件pagehelper. 1. 项目结构 (1)db.properties存储数据源和连接池配置. (2)两个数据源的ma ...

  6. Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid

    本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置. 1. pom.xml文件配置 需要在dependencies节点添加: <!-- MySQL --> ...

  7. SSM(Spring MVC +Spring+Mybatis)整合——maven工程

    所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需 ...

  8. Spring Boot 整合MyBatis(1)

    这篇文章介绍如何在Spring boot中整合Mybatis,其中sql语句采用注解的方式插入.后续文章将会介绍,如何使用xml方式. SSM SSH框架已经满足轻量级这个需求了,但是对于开发人员而言 ...

  9. Spring Boot2 系列教程(二十一)整合 MyBatis

    前面两篇文章和读者聊了 Spring Boot 中最简单的数据持久化方案 JdbcTemplate,JdbcTemplate 虽然简单,但是用的并不多,因为它没有 MyBatis 方便,在 Sprin ...

  10. Spring Boot教程(三十七)整合MyBatis

    Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方 ...

随机推荐

  1. MapReduce——客户端提交任务源码分析

    计算向数据移动 MR程序并不会在客户端执行任何的计算操作,它是为计算工作做好准备,例如计算出切片信息,直接影响到Map任务的并行度. 在Driver中提交任务时,会写到这样的语句: boolean r ...

  2. EasyExcel 框架使用-读

    EasyExcel 框架使用 官方介绍:JAVA解析Excel工具EasyExcel Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内 ...

  3. Git操作_本地仓库第一次推送到远程仓库

    实现目的: 本地已经安装好Git,pycham已经有一个项目,打算放到Git远程仓库 前提条件:本地配置好了公钥,且GIT 上关联好公钥,步骤如下: git本地仓库连接github操作步骤:windo ...

  4. 教你三种jQuery框架实现元素显示及隐藏动画方式

    摘要:在jQuery框架中对元素对象进行显示和隐藏有三种方式,分别是"默认方式显示和隐藏"."滑动方式显示和隐藏"."淡入淡出显示和隐藏". ...

  5. 详细了解 Linkerd 2.10 基础功能,一起步入 Service Mesh 微服务架构时代

    Linkerd 提供了许多功能,如:自动 mTLS.自动代理注入.分布式追踪.故障注入.高可用性.HTTP/2 和 gRPC 代理.负载均衡.多集群通信.重试和超时.遥测和监控.流量拆分(金丝雀.蓝/ ...

  6. java笔试题(二)

    1.写出一维数组初始化的两种方式 int[] arr={1,2,3}; String[] str=new String[2]; str[1]="23"; 2.写出二维数组初始化的两 ...

  7. 【复习】Listening and Reading Comprehension

    短对话 M: Why do you declare the news that you're pregnant on your blog directly? W: I'm so excited tha ...

  8. Linux shell是什么

    shell概念: shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell启动,挂起,停止甚至编写一些程序. shell还是一个功能强 ...

  9. JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇

    JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇 作为一个使用Java语言开发的程序员,我们都知道,要想运行Java程序至少需要安装JRE(安装JDK也没问题).我们也知道我们Java程序 ...

  10. external-attacher源码分析(1)-main方法与启动参数分析

    更多 ceph-csi 其他源码分析,请查看下面这篇博文:kubernetes ceph-csi分析目录导航 摘要 ceph-csi分析-external-attacher源码分析.external- ...