前言

Springboot 整合 MyBatis 有两种方式,分别是:“全注解版” 和 “注解、xml混合版”。

创建项目

创建Springboot项目,选择依赖

Maven依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>2.1.0</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

配置文件

#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver

全注解版

项目结构

实体类

public class User {
private int id;
private String name; //getter、setter、toString

Dao

@Mapper
public interface UserDao { @Select("select * from user where id = #{id}")
public User getUserById(@Param("id") int id); @Select("select * from user")
public List<User> getAllUser();
}

Service

@Service
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao; @Override
public User getUserById(int id) {
return this.userDao.getUserById(id);
} @Override
public List<User> getAllUser() {
return this.userDao.getAllUser();
}
}

控制类

@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceImpl userService; @RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable("id")int id) {
return this.userService.getUserById(id);
} }

测试

http://localhost:8080/user/getUserById/1


注解、xml混合版

项目结构

(比之前的多了一个映射文件)

配置文件

添加了mybatis的配置,包括Mapper文件的位置、alias的配置

spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver #mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.jotal.springboot05mybatis.entities

实体类

public class User {
private int id;
private String name;
//getter、setter、toString

Dao

/*
* 在主启动类使用了@MapperScan就不用每一个添加@Mapper了
* */
public interface UserDao { // @Select("select * from user where id = #{id}") // 若使用全注解需要添加@Param("id")到形式参数之前
User getUserById(int id); // @Select("select * from user")
List<User> getAllUser();
}

映射xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jotal.springboot05mybatis.dao.UserDao">
<select id="getUserById" parameterType="Integer" resultType="user">
select * from user where id = #{id}
</select>
<select id="getAllUser" resultType="user">
select * from user
</select>
</mapper>

Service、控制类和之前的一样

可以在主启动类使用@MapperScan注解扫描Mapper类,就不需要在每个Mapper类添加@Mapper

@MapperScan("com.jotal.springboot05mybatis.dao")
@SpringBootApplication
public class Springboot05MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05MybatisApplication.class, args);
} }

到此,Springboot整合Mybatis的两种方式就介绍到这里了。

springboot笔记07——整合MyBatis的更多相关文章

  1. SpringBoot当中如何整合mybatis和注入

    [学习笔记] 6.整合mybatis和注入: 马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子.pom.xml只需要加入mybatis和mysql的部分 ...

  2. SpringBoot 2.X整合Mybatis

    1.创建工程环境 勾选Web.Mybatis.MySQL,如下 依赖如下 <dependency> <groupId>org.springframework.boot</ ...

  3. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  4. SpringBoot | 3.2 整合MyBatis

    目录 前言 1. 导入MyBatis场景 1.1 初始化导向 1.2 手动导入 2. *MyBatis自动配置原理 3. 全局配置文件 @Mapper @MapperScan 3.1 配置模式 3.2 ...

  5. springboot学习2 整合mybatis

    springboot整合mybatis 一.添加mybatis和数据库连接的依赖 <!--整合mybatis--> <dependency> <groupId>or ...

  6. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  7. 利用IDEA搭建SpringBoot项目,整合mybatis

    一.配置文件.启动项目 生成之后这几个文件可以删掉的 配置application spring.datasource.url=jdbc:mysql://localhost:3306/test?serv ...

  8. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  9. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

随机推荐

  1. Eclipse R语言开发环境搭建 StatET插件

    StatET 官网 http://www.walware.de/goto/statet Installation 点击菜单栏 help --> Install New Software 配置R语 ...

  2. OpenFOAM——平行平板间具有相对运动(库埃特流)

    本算例翻译整理自:http://the-foam-house5.webnode.es/products/chapter-1-plane-parallel-plates-case/ 这个算例研究了一个距 ...

  3. hdu5438 Ponds[DFS,STL vector二维数组]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...

  4. 运维笔记--postgresql占用CPU问题定位

    运维笔记--postgresql占用CPU问题定位 场景描述: 业务系统访问变慢,登陆服务器查看系统负载并不高,然后查看占用CPU较高的进程,发现是连接数据库的几个进程占用系统资源较多. 处理方式: ...

  5. WMS常用命令

  6. 查询、下载GWAS目录数据的R包(gwasrapidd)

    目前GWAS方向发了很多文献,但是并没有一个很完善的R包对这些文献的数据进行汇总. 接下来推荐的这个是最新发表的GWAS数据汇总R包​.看了一下功能齐全,但是数据不是收录的很齐全​. 下面具体讲一下. ...

  7. mysql8忘记秘密-重置密码步骤

    mysql8修改密码的方式有些许不同 1.配置无密码登录 修改/etc/my.cnf文件,在mysqld模块下添加 skip-grant-tables 2.重启mysql 3.mysql -uroot ...

  8. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. 用浏览器控制台抓取shodan、搜索引擎、zone-h的结果

    0x00 前言 大部分内容来自参考连接的内容,只是一种爬取内容的思路. 在很久以前自己会有爬取zone-h做目标测试的需求,但是总是有各种反爬限制.而且个别网址还有前端自动生成内容的功能,使用Java ...

  10. LeetCode:复原IP地址【93】

    LeetCode:复原IP地址[93] 题目描述 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: [&qu ...