版本和依赖

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. 用Taro写一个微信小程序(三)—— 配置dva

    一.关于dva dva 首先是一个基于 redux 和 redux-saga 的数据流方案,然后为了简化开发体验,dva 还额外内置了 react-router 和 fetch,所以也可以理解为一个轻 ...

  2. swagger 注解使用

    @Api() 用于类:表示标识这个类是swagger的资源 tags–表示说明 value–也是说明,可以使用tags替代 但是tags如果有多个值,会生成多个list @ApiOperation() ...

  3. HashMap源码解析和设计解读

    HashMap源码解析 ​ 想要理解HashMap底层数据的存储形式,底层原理,最好的形式就是读它的源码,但是说实话,源码的注释说明全是英文,英文不是非常好的朋友读起来真的非常吃力,我基本上看了差不多 ...

  4. 【NX二次开发】通过两点创建单位向量

    源码1: //生成从起点到终点的单位向量 double douPoint_Start[3] = { 10,10,10 }; double douPoint_End[3] = { 15,16,13 }; ...

  5. 从 Nginx 优秀的核心架构设计,揭秘其为何能支持高并发?

    目录: 1. Nginx的整体架构 2. Nginx的模块化设计 3. Nginx的请求方式处理 4. Nginx事件驱动模型 5. Nginx进程处理模型 写在前面 Nginx 是一个 免费的,开源 ...

  6. 「模拟8.18」字符串(卡特兰数)·乌鸦喝水(树状数组,二分)·所驼门王的宝藏(tarjan,拓扑)

    最近好颓啊,所以啥都做不出来 简单说一下这次考试,分机房了,还分不同考卷,果然我还是留在二机房的蒟蒻, 大概也只有这样的简单题,才能勉强水个rank 3吧........ 其实不必管在哪个机房,努力便 ...

  7. redis不完整的事务实现Transaction

    使用场景 redis一个命令执行是单线程的,不用担心并发冲突,如果你想有几个命令想像一个命令一样,在这几个命令执行过程中不会执行别的客户端发来的命令 ,也就是原子性,就可以用 redis Transa ...

  8. 性能工具之linux常见日志统计分析命令

    引言 我前几天写过的性能工具之linux三剑客awk.grep.sed详解,我们已经详细介绍 linux 三剑客的基本使用,接下来我们看看具体在性能测试领域的运用,本文主要介绍的是在 Tomcat 和 ...

  9. Terraform入门教程,示例展示管理Docker和Kubernetes资源

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 最近工作中用到了Terraform,权当学习记录一下,希望能帮助到其它人. Terraform系列文章如下: T ...

  10. 《手把手教你》系列基础篇(五)-java+ selenium自动化测试- 创建首个自动化脚本(详细教程)

    1.简介 前面几篇宏哥介绍了两种(java和maven)环境搭建和三大浏览器的启动方法,这篇文章宏哥将要介绍第一个自动化测试脚本.前边环境都搭建成功了,浏览器也驱动成功了,那么我们不着急学习其他内容, ...