SpringBoot和Mybatis的整合

这里介绍两种整合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

原文地址:https://www.cnblogs.com/jxldjsn/p/10198094.html

时间: 2024-08-06 23:17:03

SpringBoot和Mybatis的整合的相关文章

SpringBoot+SpringMVC+MyBatis快速整合搭建

使用过SpringBoot的同学都知道,SpringBoot的pom.xml中的坐标都是按功能导入的,jar包之间的依赖SpringBoot底层已经帮我们做好了,例如我们要整合SprngMVC,只需要导入SpringMVC的起步依赖就可以了,SpringBoot会帮我们导入Spring和SpringMVC整合需要的jar包. SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Sp

springboot+springmvc+mybatis项目整合

介绍: Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. 特点: 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标,健康检查和外部配置 6. 绝对没有代码生成和对XML没有要求配置 搭建springboot项目我推荐大家用idea,我现在用的是idea,所以接下来我是用idea搭建项目的  一.

springboot同mybatis整合

springboot和mybatis整合有两种开发模式,首先要做的是配置好开发环境, 实现步骤: 在maven文件pom中配置: 1)SpringBoot同Mybatis整合的依赖. <dependency> <groupId>com.ruijc</groupId> <artifactId>spring-boot-starter-mybatis</artifactId> <version>3.2.2</version> &

SpringBoot与Mybatis整合实例详解

介绍 从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于Spring框架的应用.SpringBoot会选择最适合的Spring子项目和第三方开源库进行整合.大部分Spring Boot应用只需要非常少的配置就可以快速运行起来. SpringBoot包含的特性 创建可以独立运行的Spring应用 直接嵌入Tomcat或Jetty服务器,不需要部署WAR文件

springboot+mybatis+springmvc整合实例

以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是"零配置","零配置"不等于什么也不配置,只是说相对于传统的ssm框架的xml配置或是注解配置,要少的多.作为常规的来说,一个ssm框架整合,拿maven来说,首先在src/main/resource下加入jdbc.properties,spring-mvc.xml,spring-myba

Springboot 2.0.4 整合Mybatis出现异常Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required

在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required,然后各种找日志百度,网上给了一种解决方法: 版本太高,使用手动注入sqlSessionFactory,然后用dao的实习类继承,因为我的项目没有dao 的实现类,直接是interface+mapper文件,所以直接忽略了,没有试过,想试一下可以试一下 阅读博客点这里(随手百度的):这里是传送门

springboot + mybatis + mycat整合

1.mycat服务 搭建mycat服务并启动,windows安装参照. 系列文章: [Mycat 简介] [Mycat 配置文件server.xml] [Mycat 配置文件schema.xml] [Mycat 配置文件rule.xml] 2.相关配置文件 此处我的配置为: schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <myca

SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作

SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作 1> 数据准备 -- 创建测试表 CREATE TABLE `tb_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_name` varchar(20) NOT NULL COMMENT '用户名', `password` varchar(20) NOT NULL COMMENT '密码', `name` varchar

springboot集成mybatis环境搭建以及实现快速开发微服务商品模块基本的增删改查!

之前学习了springboot和mybatis3的一些新特性,初步体会了springboot的强大(真的好快,,,,,),最近趁着复习,参考着以前学习的教程,动手写了一个springboot实战的小例子! 一 创建表以及实体 使用简单的五个字段商品表,主键采用UUID字符串,价格使用BigDecimal,本来是想在linux数据库中建立表的,实在是懒不想启动虚拟机(这么简单也觉得没必要),,sql语句如下: create table products( pid varchar(32) not n