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

  1. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  2. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  3. 持续集成之 Spring Boot 实战篇

    本文作者: CODING 用户 - 何健 这次实战篇,我们借助「CODING 持续集成」,实现一个简单的 Spring Boot 项目从编码到最后部署的完整过程.本教程还有 B 站视频版,帮助读者更好 ...

  4. MyBatis集成到Spring时配置MapperScannerConfigurer出错

    问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...

  5. 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】

    swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...

  6. docker搭建elasticsearch、kibana,并集成至spring boot

    步骤如下: 一.基于docker搭建elasticsearch环境 1.拉取镜像 docker pull elasticsearch5.6.8 2.制作elasticsearch的配置文件 maste ...

  7. mybatis集成到spring理解

  8. Spring Boot (八)MyBatis + Docker + MongoDB 4.x

    一.MongoDB简介 1.1 MongoDB介绍 MongoDB是一个强大.灵活,且易于扩展的通用型数据库.MongoDB是C++编写的文档型数据库,有着丰富的关系型数据库的功能,并在4.0之后添加 ...

  9. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

随机推荐

  1. mongodb 的进库操作

    mongo show dbs use  xxx show collections db.xxx.find() db.users.update({ "xxx" : ObjectId( ...

  2. Windows下安装Selenium

    安装python,建议在官网下载python3以上的版本 安装easy_install,找度娘 安装selenium,在命令行窗口下输入:pip install -U selenium 下载chrom ...

  3. phantomjs的使用

    PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引擎.它原生支持多种web 标准:DOM 操作,CSS选择器,JSON,Canvas 以及SVG,同时也提供了处理文件I/O的操作,从而 ...

  4. 在Java Web项目中添加定时任务

    在Java Web程序中加入定时任务,这里介绍两种方式:1.使用监听器注入:2.使用Spring注解@Scheduled注入. 推荐使用第二种形式. 一.使用监听器注入 ①:创建监听器类: impor ...

  5. Codeforces 834E The Bakery【枚举+数位dp】

    E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...

  6. hihoCoder #1038 : 01背包(板子题)

    #1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...

  7. Codeforces Round #336 (Div. 2)-608A.水题 608B.前缀和

    A题和B题...   A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabyte ...

  8. BZOJ2282: [Sdoi2011]消防

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2282 答案一定是在直径上的一段,然后答案一定不会小于不在直径上的点到直径的距离(要是可以的话那 ...

  9. hdu_1011(Starship Troopers) 树形dp

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:打洞洞收集脑子,你带领一个军队,洞洞互联成一棵树,每个洞中有一些bug,要全部杀死这些虫子 ...

  10. C#的LINQ

    在过去如果我们如果需要去查询某些集合或者数组里面的某些元素,我们需要写出大量的带有筛选的遍历集合的代码,但是有了Linq之后,我们就不用写出那些冗余麻烦的遍历代码,只需要关注其中的筛选,排列的函数就可 ...