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</ ...
随机推荐
- TP5验证码上传阿里云万网虚拟主机后,验证码不显示的解决办法
TP5不显示验证码 清除缓冲区就应该可以了,今天我刚好也遇到了,解决的办法是在vendor/topthink/think-captcha/CaptchaController.php中加上这个ob_cl ...
- office2010安装不了提示已经安装32位的了怎么办
1.打开控制面板,查看是否有安装的程序没有拆卸,如果没有继续往下看,如果有直接拆卸掉,再进行下面的步骤. 2.首先打开注册列表.按下win+R键即可打开,输入regedit,也可以在开始菜单中搜索re ...
- iview 的事件绑定
iview 内的组件样式是不错,有时候我们想用它且绑定某个事件: 比如,我们使用了步骤条组件(Steps),然后绑定点击事件,实现每次点击某个步骤条内的step 就显示此step的具体信息, < ...
- Symfony之入门学习
最近因业务需要,主要针对Edusoho进行二次开发.但是对于Symfony,我并不熟悉,我所了解的是,它的那套与我在Java中常用的开发模式MVC,本质上并不多大差异,就是所使用的语言不一样而已.下面 ...
- FusionInsight,一个融合的大数据平台
随着物联网技术和应用的普及,以运营商.互联网以及实体经济行业为代表的企业产生了越来越多的数据,大数据的发展越来越蓬勃. 从2007年开始,大数据应用成为很多企业的需求,2012年兴起并产生了大数据平台 ...
- NIO Channel SocketChannel ServerSocketChannel
ServerSocketChannel: ServerSocketChannel是一个基于通道的socket监听器.它同我们所熟悉的java.net.ServerSocket执行相同的基本任务,不过它 ...
- redis-sentinel 高可用方案实践
近期公司的一块核心业务使用redis作为配置转发中心,存在单点问题,考虑服务的可靠性.针对业务需求,我们确定了我们的需求: 异地跨机房容灾 故障自动切换 尽可能高的保证数据不丢失 针对以上需求,我们分 ...
- Unity3D特效入门教学视频教程合集
目录 大小25GB,Mp4格式,语言:中文 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主
- python简单的游戏场景代码
模拟英雄联盟游戏场景的简单场景 最后计算出英雄的战斗力 class Hero: def __init__(self, na, gen, age, fig): self.name = na self.g ...
- WebGL学习笔记(七):输入和动画
目前为止,我们绘制出来的3D物体都是静止的,接下来我们需要让桌面上的小盒子可以根据我们按键(上下键)前进后退: 输入方面,监听按键和鼠标消息直接在document上添加对应的监听就行了: 动画这块,我 ...