Mybatis集成到spring boot
1, Mybatis简介
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
2, Mybatis安装
将下面的 dependency 代码置于 pom.xml 文件中:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
3, Mybatis配置
/**
* 构建Mybatis所需要的数据源,事务,SqlSessionFactory,sqlSessionTemplate
*
* @author
*/
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer
{
@Override
public PlatformTransactionManager annotationDrivenTransactionManager()
{
return new DataSourceTransactionManager(dataSource());
}
/**
* 设置数据库的数据源
*
* @return DataSource
*/
@Bean(name = "dataSource")
public DataSource dataSource()
{
DruidDataSource DruidDataSource = new DruidDataSource();
DruidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
DruidDataSource.setUrl(dataSourceProperties.getUrl());
DruidDataSource.setUsername(dataSourceProperties.getUsername());
DruidDataSource.setPassword(dataSourceProperties.getPassword());
return DruidDataSource;
}
/**
* 用于构建SqlSessionFactory
*
* @return SqlSessionFactory
* @throws Exception
*/
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() throws Exception
{
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
bean.setMapperLocations(resolver.getResources("classpath*:com/eversec/**/*.xml"));
return bean.getObject();
}
/**
* 通过 sqlSessionFactory 构建 SqlSessionTemplate
*
* @param sqlSessionFactory
* @return SqlSessionTemplate
*/
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory)
{
return new SqlSessionTemplate(sqlSessionFactory);
}
@Resource(name = "dataSourceProperties")
private DataSourceProperties dataSourceProperties;
}
/**
* 描述:配置Mapper
*
* @author
*
*/
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig
{
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()
{
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.eversec.**.sqlmapper");
return mapperScannerConfigurer;
}
}
4, Mybatis 反向生成代码
4.1 将下面的 plugin 代码置于 pom.xml 文件中:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
4.2 将generatorConfig.xml置于src/main/resource文件夹中
generatorConfig.xml 的内容大致如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<classPathEntry location="D:\mysql-connector-java-5.1.39.jar" />
<context id="context1" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://IP:3306/database?useUnicode=true&characterEncoding=UTF-8"
userId="root" password="123456" />
<javaModelGenerator targetPackage="com.entity"
targetProject="D:\workspace\xxxx\src\main\java" />
<sqlMapGenerator targetPackage="com.sqlmapper"
targetProject="D:\workspace\xxxx\src\main\java" />
<javaClientGenerator targetPackage="com.sqlmapper"
targetProject="D:\workspace\xxxx\src\main\java" type="XMLMAPPER" />
<table tableName="table" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
4.3 执行
右键pom.xml, 依次选择Run As >> Maven build... ,然后输入Goals中输入mybatis-generator:generate,最好点击Run按钮。
5, MyBatis的框架图

看到Mybatis的框架图,可以清晰的看到Mybatis的整体核心对象,我更喜欢用自己的图来表达Mybatis的整个的执行流程。如下图所示:

原理详解:
MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。
官网地址:http://www.mybatis.org/mybatis-3/zh/index.html
Mybatis集成到spring boot的更多相关文章
- Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置
Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- 持续集成之 Spring Boot 实战篇
本文作者: CODING 用户 - 何健 这次实战篇,我们借助「CODING 持续集成」,实现一个简单的 Spring Boot 项目从编码到最后部署的完整过程.本教程还有 B 站视频版,帮助读者更好 ...
- MyBatis集成到Spring时配置MapperScannerConfigurer出错
问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...
- 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】
swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...
- docker搭建elasticsearch、kibana,并集成至spring boot
步骤如下: 一.基于docker搭建elasticsearch环境 1.拉取镜像 docker pull elasticsearch5.6.8 2.制作elasticsearch的配置文件 maste ...
- mybatis集成到spring理解
- Spring Boot (八)MyBatis + Docker + MongoDB 4.x
一.MongoDB简介 1.1 MongoDB介绍 MongoDB是一个强大.灵活,且易于扩展的通用型数据库.MongoDB是C++编写的文档型数据库,有着丰富的关系型数据库的功能,并在4.0之后添加 ...
- spring boot集成mybatis(1)
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
随机推荐
- 高效开发技巧:为什么你下载Git项目这么慢?
文章首发于[博客园-陈树义],点击跳转到原文<高效开发技巧:为什么你下载Git项目这么慢?>. 笔者所在公司采用的是 GitLab 进行版本管理,但许多同事下载 Git 项目的路径是这样的 ...
- zz:linux下rz,sz的安装方法
zz:http://xukaizijian.blog.163.com/blog/static/1704331192011611104631875/ wget http://ohse.de/uwe/re ...
- react 开发知识准备
react react使用教程 babel babel 可用于ES6转换为ES5,jsx转换为原生js. ES6 ES6 语法 webpack webpack打包工具,它把不同的.相互依赖的静态资源都 ...
- 从Unity中的Attribute到AOP(六)
本文将重点对Unity剩下常用的Attribute进行讲解,其他不常用的Attribute各位可以自行去官方文档查阅. 首先是UnityEngine命名空间下的. ColorUsage,这个主要作用于 ...
- java.io与网络通信
文件IO java.io.File是用于操作文件或目录的类: File file = new File("hello.txt"); 实例化File时不关心路径的目标并不会去读取文件 ...
- 从Unity中的Attribute到AOP(八)
本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...
- [].slice.call(arguments,1)
[转] 前言 今天偶然翻资料看到一个叫做软绑定的函数,用来确定this的; 原代码 if(!Function.prototype.softBind){ Function.prototype.softB ...
- java_web学习(十) 显示mysql中的数据
一.建立数据库 create database animal; create table animal( sno int, name varchar(20), weight varcahr(20), ...
- meta标签有何作用?一起来学习一下
平日里总是沉迷于写页面写组件思考业务逻辑,解决冲突找出bug,猛的发现躲在head标签里的一大串标签时什么?这么多meta标签好多居然都不知其存在的意义.所以决定记录一下学习到的知识点. 先搞明白以上 ...
- BZOJ 3670: [Noi2014]动物园【KMP变形 】
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2738 Solved: 1475[Submit][Status ...