SpringBoot 整合 Mybatis 和 Mysql (详细版)
结构如下

1、引入相关依赖
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2、配置application.yml文件
server:
port: 80
servlet:
context-path: spring:
# 配置文件选择(dev,pro)
profiles:
active: dev # 国际化(消息源自动配置,springboot默认找出messages)
messages:
basename: i18n.messages
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/fallenfairy?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: admin
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
# mapper文件
mapper-locations: mapper/*/*Mapper.xml
# 实体类
type-aliases-package: com.fallenfairy.*.pojo
3、启动类添加@MapperScan注解
@SpringBootApplication
@MapperScan("com.fallenfairy.*.dao")
public class FallenfairyApplication {
public static void main(String[] args) {
SpringApplication.run(FallenfairyApplication.class, args);
}
}

6、编写mapper.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.example.demo.dao.UserDAO">
<!--id对应UserDAO接口方法名-->
<!--parameterType对应参数类型-->
<!--resultType对应返回值类型-->
<!--注意sql语句字段和数据库对应-->
<select id="getUserById" parameterType="int" resultType="com.example.demo.pojo.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>

7、编写controller测试
/**
* Copyright (C), 2017-2018, XXX有限公司
* FileName: UserController
* Author: 丶Zh1Guo
* Date: 2018/11/21 16:51
* Description: user控制器
* History:
* <author> <time> <version> <desc>
* 作者姓名
* 〈一句话功能简述〉<br>
* 〈user控制器〉
*
* @author 丶Zh1Guo
* @create 2018/11/21
* @since 1.0.0
*/
@RestController
public class UserController {
@Autowired
private UserDAO userDAO;
// url: xxx/user?id=1
@RequestMapping("/user")
public User getUser(@RequestParam("id") int id){
return userDAO.getUserById(id);
}
// url: xxx/user/1
@RequestMapping("/user/{id}")
public User getUser2(@PathVariable int id){
return userDAO.getUserById(id);
}
}
8、启动服务测试
测试第一种@RequestParam("id") int id:

测试第二种@PathVariable int id:

若访问报错如下:
mysql运行报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
设置数据库时区为东八区即可
设置数据库连接:(增加:&serverTimezone=GMT%2B8)
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/flexo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: admin
或修改数据时区:

ok
SpringBoot 整合 Mybatis 和 Mysql (详细版)的更多相关文章
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- springboot整合mybatis连接mysql数据库出现SQLException异常
在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...
- kotlin + springboot整合mybatis操作mysql数据库及单元测试
项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...
- SpringBoot 整合 Mybatis + Mysql——XML配置方式
一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...
- 学习springboot整合mybatis并编写测试类
报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...
- SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置
接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...
- SpringBoot整合Mybatis完整详细版
记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...
随机推荐
- 【Android】资源系列(二) -- 文件原样保留的资源assets和res/raw文件夹
这两个文件夹都能够存放文件.而在打包的时候被原样保留. 那用这两个文件夹可以做什么事呢? 1.放一个apk,要用的时候调出来.免得去下载server下载. 2.放一个sql,当app数据库非常大的时候 ...
- legend---八、php对象如何转换成js对象
legend---八.php对象如何转换成js对象 一.总结 一句话总结:a.直接转换:b.通过json对象做中间桥梁 1.为什么传递给父亲构造函数的参数不能写默认值? 这里的第三行的比如$type不 ...
- [Codeforces 757E] Bash Plays with Functions (数论)
题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...
- UI Framework-1: Aura Multi-desktop
Multi-desktop Aura now makes it possible for the same browser process to render to multiple desktops ...
- Linux-CentOS5/6启动流程
Linux-CentOS5/6启动流程
- 高手过愚人节 Manancher模板题_双倍经验
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- 关于 nginx 的配置的 location
精准匹配和普通匹配: server{ location =/index.htm{ ////精准匹 ...
- 关于thinkphp 命令行
很多人做多年开发只懂得PHP能在浏览器下运行或者只能结合APACHE等WEB服务器运行,却不晓得,PHP也能用命令行执行,或许是由于大多人在WINDOWS平台做开发部署运行,比较少接触LINUX. T ...
- 20180929 北京大学 人工智能实践:Tensorflow笔记06
入戏 需要修改成如下: (完)
- glEnable(GL_DEPTH_TEST)作用
glEnable(GL_DEPTH_TEST): 用来开启更新深度缓冲区的功能,也就是,如果通过比较后深度值发生变化了,会进行更新深度缓冲区的操作.启动它,OpenGL就可以跟踪再Z轴上的像素,这样, ...