这里介绍两种整合SpringBoot和Mybatis的模式,分别是“全注解版” 和 “注解xml合并版”。

前期准备
开发环境

开发工具:IDEA
JDK:1.8
技术:SpringBoot、Maven、Mybatis
创建项目

项目结构

Maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
全注解版
SpringBoot配置文件

这里使用yml格式的配置文件,将application.properties改名为application.yml。

#配置数据源
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dianping?useUnicode=true&characterEncoding=utf8
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
1
2
3
4
5
6
7
SpringBoot会自动加载application.yml相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中。

实体类

public class Happiness {
private Long id;
private String city;
private Integer num;

//getters、setters、toString
}
1
2
3
4
5
6
7
映射类

@Mapper
public interface HappinessDao {
@Select("SELECT * FROM happiness WHERE city = #{city}")
Happiness findHappinessByCity(@Param("city") String city);

@Insert("INSERT INTO happiness(city, num) VALUES(#{city}, #{num})")
int insertHappiness(@Param("city") String city, @Param("num") Integer num);
}
1
2
3
4
5
6
7
8
Service类

事务管理只需要在方法上加个注解:@Transactional

@Service
public class HappinessService {
@Autowired
private HappinessDao happinessDao;

public Happiness selectService(String city){
return happinessDao.findHappinessByCity(city);
}

@Transactional
public void insertService(){
happinessDao.insertHappiness("西安", 9421);
int a = 1 / 0; //模拟故障
happinessDao.insertHappiness("长安", 1294);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Controller类

@RestController
@RequestMapping("/demo")
public class HappinessController {
@Autowired
private HappinessService happinessService;

@RequestMapping("/query")
public Happiness testQuery(){
return happinessService.selectService("北京");
}

@RequestMapping("/insert")
public Happiness testInsert(){
happinessService.insertService();
return happinessService.selectService("西安");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
测试

http://localhost:8080/demo/query
http://localhost:8080/demo/insert
1
2
注解xml合并版
项目结构

SpringBoot配置文件

#配置数据源
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dianping
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver

#指定mybatis映射文件的地址
mybatis:
mapper-locations: classpath:mapper/*.xml
1
2
3
4
5
6
7
8
9
10
11
映射类

@Mapper
public interface HappinessDao {
Happiness findHappinessByCity(String city);
int insertHappiness(HashMap<String, Object> map);
}
1
2
3
4
5
映射文件

<mapper namespace="com.example.demo.dao.HappinessDao">
<select id="findHappinessByCity" parameterType="String" resultType="com.example.demo.domain.Happiness">
SELECT * FROM happiness WHERE city = #{city}
</select>

<insert id="insertHappiness" parameterType="HashMap" useGeneratedKeys="true" keyProperty="id">
INSERT INTO happiness(city, num) VALUES(#{city}, #{num})
</insert>
</mapper>
1
2
3
4
5
6
7
8
9
Service类

事务管理只需要在方法上加个注解:@Transactional

@Service
public class HappinessService {
@Autowired
private HappinessDao happinessDao;

public Happiness selectService(String city){
return happinessDao.findHappinessByCity(city);
}

@Transactional
public void insertService(){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("city", "西安");
map.put("num", 9421);

happinessDao.insertHappiness(map);
int a = 1 / 0; //模拟故障
happinessDao.insertHappiness(map);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Controller类

@RestController
@RequestMapping("/demo")
public class HappinessController {
@Autowired
private HappinessService happinessService;

@RequestMapping("/query")
public Happiness testQuery(){
return happinessService.selectService("北京");
}

@RequestMapping("/insert")
public Happiness testInsert(){
happinessService.insertService();
return happinessService.selectService("西安");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
测试

http://localhost:8080/demo/query
http://localhost:8080/demo/insert

SpringBoot和Mybatis的整合的更多相关文章

  1. SpringBoot+SpringMVC+MyBatis快速整合搭建

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  2. springboot+springmvc+mybatis项目整合

    介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生, ...

  3. Springboot与MyBatis简单整合

    之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来 ...

  4. springboot对mybatis的整合

    1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver pom.xml配置文件导入依赖 <!--mybatis-sp ...

  5. IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合

    一.编译器建立项目 参考:http://www.cnblogs.com/liuyangfirst/p/8372291.html 二.代码编辑 1.建立数据库 /* Navicat MySQL Data ...

  6. SpringBoot系列——MyBatis整合

    前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...

  7. 30分钟带你了解Springboot与Mybatis整合最佳实践

    前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...

  8. SpringBoot与SpringDateJPA和Mybatis的整合

    一.SpringBoot与SpringDateJPA的整合 1-1.需求 查询数据库---->得到数据------>展示到页面上 1-2.整合步骤 1-2-1.创建SpringBoot工程 ...

  9. 微服务学习一:idea中springboot集成mybatis

    一直都想学习微服务,这段时间在琢磨这块的内容,个人之前使用eclipse,现在用intellij idea来进行微服务的开发,个人感觉intellij idea比eclipse更简洁更方便,因为int ...

随机推荐

  1. Struts2 概述

    1. struts2应用在javaee三层结构中web层框架 2. struts2框架在struts1和webwork基础之上的发展全新的框架 3.struts2 解决的问题: 用户管理的crud操作 ...

  2. MySQL PXC集群部署

    安装 Percona-XtraDB-Cluster 架构: 三个节点: pxc_node_0 30.0.0.196 pxc_node_1 30.0.0.198 pxc_node_2 30.0.0.19 ...

  3. redis-3.2 集群

    目录 简介 集群简介 Redis 集群的数据分片 Redis 集群的主从复制模型 Redis 一致性保证 redis 集群间的通信 环境 安装Ruby 部署 安装Redis略 创建集群 集群节点信息 ...

  4. Leetcode480-Sliding Window Median

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  5. PHP多进程非阻塞模式下结合原生Mysql与单进程效率测试对比

    公司在做游戏服务器合并的时候,对大批量数据表做了合并操作,难免会出现数据格式不一致问题.根据玩家反映BUG排查,是因为某个模块下日志表出现了数据格式问题导致. 目前想到的是有两种方案解决,第一种就是把 ...

  6. bzoj2152 / P2634 [国家集训队]聪聪可可(点分治)

    P2634 [国家集训队]聪聪可可 淀粉质点分治板子 边权直接 mod 3 直接点分治统计出所有的符合条件的点对再和总方案数约分 至于约分.....gcd搞搞就好辣 #include<iostr ...

  7. 【题解】Luogu P3901 数列找不同

    我博客中对莫队的详细介绍 原题传送门 不错的莫队练手题 块数就直接取sqrt(n) 对所有询问进行排序 排序第一关键词:l所在第几块,第二关键词:r的位置 考虑Ai不大,暴力开数组 add时如果加之后 ...

  8. LVM基本应用,扩展及缩减实现

    一.基本概念 如上图所示:底层PV(物理卷可能是硬盘设备,分区或RAID等),一个或多个PV组织成一个VG(卷组),卷组是不能直接格式化使用的,所以在VG之上,还需要创建LV进行格式化使用.VG在逻辑 ...

  9. FJUT 毒瘤3(二分 + 最大匹配)题解

    毒瘤3 TimeLimit:1000MS  MemoryLimit:256MB 64-bit integer IO format:%lld   Problem Description 字节跳动有n款产 ...

  10. oracle 之 伪列 rownum 和 rowid的用法与区别

    rownum的用法 select  rownum,empno,ename,job from emp where rownum<6 可以得到小于6的值数据 select rownum,empno, ...