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设计模式, ...
随机推荐
- 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转)
通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?(转) 一.总结 一句话总结:.NET是一个平台,包含多种语言,比如(C#.Visual Basic.C++/C ...
- js中如何取精度
js中如何取精度 一.总结 一句话总结:其实round()函数去经度会有误差,直接用num.toFixed(2)简单方便. toFixed()方法会按照指定的小数返回数值的字符串表示.var num ...
- python判断一个单词是否为有效的英文单词?——三种方法
For (much) more power and flexibility, use a dedicated spellchecking library like PyEnchant. There's ...
- R语言写简单线性回归
library(MASS) library(ISLR) lm.fit <- lm(medv~lstat,data=Boston) attach(Boston) lm.fit = lm(medv~ ...
- 86.express里面的app.configure作用
以下摘自 express 3.0 的 文档 app.configure([env], callback) Conditionally invoke callback when env matches ...
- rest_framework 权限功能
权限: 问题:不用视图不用权限可以访问 基本使用 写上一个权限类 创建utils 中 permission.py文件 class SvipPermisson(object): message = &q ...
- React ----- 路由懒加载的几种实现方案
传统的两种方式 1.import() 符合ECMAScript提议的import()语法,该提案与普通 import 语句或 require 函数的类似,但返回一个 Promise 对象.这意味着模块 ...
- 如何知道 CPU 是否支持虚拟化技术(VT)
作者: Sk 译者: LCTT geekpi 我们已经知道如何检查你的 Linux 操作系统是 32 位还是 64 位以及如何知道你的 Linux 系统是物理机还是虚拟机.今天,我们将学习另一个有用的 ...
- SSD-tensorflow-1 demo
一.简易识别 用最简单的已训练好的模型对20类目标做检测. 你电脑的tensorflow + CUDA + CUDNN环境都是OK的, 同时python需要安装cv2库 { 'aeropla ...
- element-ui table 行内编辑
EditRow.ts vue+element-ui+slot-scope原生实现可编辑表格 interface NoParamConstructor<T> { new(): T; } ex ...