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的文章,方 ...
随机推荐
- 『动善时』JMeter基础 — 47、JMeter的HTTP代理服务器详细介绍
目录 1.HTTP代理服务器的添加 2.HTTP代理服务器界面详解 (1)State:状态 (2)Global Settings:全局设置 (3)Test Plan Creation:测试计划创建 ( ...
- 合宙Luat直播间即将开启,你揭开行业奥秘,让你快人一步。
嗨~刚陪你们过儿童节 和你们一起成长的合宙Luat 又有新计划 -- 合宙Luat官方直播即将开启 - 敬请关注 - - 官方直播什么内容 - 可能是合宙研发动态 可能是新品发布资讯 可能是行业大咖分 ...
- 【题解】codeforces 8c Looking for Order 状压dp
题目描述 Lena喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她想把所有的物品都放回她的手提包.但是,这里有一点问题:她一次最多只能拿两 ...
- C#调百度通用翻译API翻译HALCON的示例描述
目录 准备工作 参数简介 输入参数 输出参数 使用HttpClient 翻译工具类 应用:翻译HALCON的示例描述 准备工作 HALCON示例程序的描述部分一直是英文的,看起来很不方便.我决定汉化一 ...
- 一个线上 Maven 诡异问题排查过程
å. 前言 现在的大部分 Java 应用基本都是通过 Maven 进行组织的,不论是分布式应用还是单体集群应用往往都会通过一个 父 POM 加若干子 POM 完成项目的组织.然而这种多应用多模块的拆分 ...
- Android EditText输入框实现下拉且保存最近5个历史记录
文章结构: 一.需求阐述 技术部同事提出想要在APP上保存最近输入成功的5个密钥信息,同时支持可以下拉进行选择. 这也是为了方便客户在现在多次输入信息,帮助其快速进行输入. 二.实现思路: 目前想要实 ...
- 对比 Verilog 和 SystemVerilog 中的基本数据类型
作为引子,首先来看一段描述,该段介绍了SystemVerilog对比Verilog在RTL设计和建模时的新特性之一(logic数据类型),然后下文我再展开对比介绍Verilog和SystemVeril ...
- 让你发布的nuget包支持源代码调试
前情概要 在不久的从前(也还是要以年为单位哈), 我们如果需要调试第三方代码, 或者框架代码很麻烦. 需要配置symbols, 匹配原始代码路径等. 为此, MS推出了 Source Link 功能, ...
- Vue 全局组件
全局注册的组件可以在其他组件内直接使用,它在整个Vue实例中都是全局有效的. 非单文件组件中使用 Vue.component('student-list', { template: ` <div ...
- 通过css实现幻灯片效果
html: css: .box { border: 0px solid white; width: 1520px; height: 480px; margin: 0 auto; position: a ...