springboot笔记07——整合MyBatis
前言
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的更多相关文章
- SpringBoot当中如何整合mybatis和注入
[学习笔记] 6.整合mybatis和注入: 马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子.pom.xml只需要加入mybatis和mysql的部分 ...
- SpringBoot 2.X整合Mybatis
1.创建工程环境 勾选Web.Mybatis.MySQL,如下 依赖如下 <dependency> <groupId>org.springframework.boot</ ...
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- SpringBoot | 3.2 整合MyBatis
目录 前言 1. 导入MyBatis场景 1.1 初始化导向 1.2 手动导入 2. *MyBatis自动配置原理 3. 全局配置文件 @Mapper @MapperScan 3.1 配置模式 3.2 ...
- springboot学习2 整合mybatis
springboot整合mybatis 一.添加mybatis和数据库连接的依赖 <!--整合mybatis--> <dependency> <groupId>or ...
- SpringBoot学习之整合Mybatis
本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...
- 利用IDEA搭建SpringBoot项目,整合mybatis
一.配置文件.启动项目 生成之后这几个文件可以删掉的 配置application spring.datasource.url=jdbc:mysql://localhost:3306/test?serv ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源
一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...
随机推荐
- <英狼>--团队作业3 王者光耀--终极版
队员 陶俊宇_031702113 卞永亨_031702229 唐怡_031702109 Github 吉哈---King-Shines 队员输出百分比,数据为估值仅供参考 MVP:队长:陶俊宇 60% ...
- PHP 根据php传的值修改 select 中动态生成的 option 组的默认选中值
有一个情况今天遇到了:通过后台传过来的一组下拉框的option值,需要默认选中其中某一项. html 部分是这样的: <select class="form-control" ...
- Java 12 骚操作, 文件比对居然还能这样玩!
Java 13 都快要来了,12必须跟栈长学起! Java 13 即将发布,新特性必须抢先看! 之前分享了一些 Java 12 的骚操作,今天继续,今天要分享的是 Java 12 中的文件比对骚操作. ...
- linux服务器磁盘挂载
1.先查看当前服务器挂载的磁盘个数 fdisk -l 2.将vdb磁盘挂载到/data目录下 mount /dev/vdb /data 3.df -h 检查磁盘挂载的情况
- (转载)基于Linux C的socket抓包程序和Package分析
转载自 https://blog.csdn.net/kleguan/article/details/27538031 1. Linux抓包源程序 在OSI七层模型中,网卡工作在物理层和数据链路层的MA ...
- deepin深度学习环境配置
deepin是一个精致优美的系统.最近因为工作需要在deepin上配置深度学习环境,话不多说,接下来记录下整个的配置过程. ×××本篇文章适合对深度学习环境配置有一定了解且对deepin系统感兴趣的同 ...
- coredump之栈溢出
1.栈溢出引发的core往往出现出现在递归调用中. gdb时看到的特征是: 栈缺失,当前栈地址不可读. 根据栈是逆向生长的特点(栈逆向生长,所以很容易出现类似数组溢出覆盖率函数返回地址,导致函数退出地 ...
- 带空格的 jquery ID 选择器
当 DOM ID有一个空格时,如何使用 jquery 的 ID 选择器? 例如,我的 DOM ID 是 <div id="content Module">Stuff&l ...
- postgresql 中文排序
select c_wsxx from fjfl.t_case_anyou order by convert_to(c_wsxx,'GBK') asc;
- C++运行出现"what(): std::bad_alloc"的解决办法
注:这里只是我的代码出现这种情况及对应的解决办法,你的代码不一定出现和我一样的情况.左移这篇随笔仅供参考. 运行程序出现如下结果: terminate called after throwing an ...