title: 玩转spring-boot-mybatis
date: 2019-03-11 19:36:57
type: "mybatis"
categories: mybatis #分类名
tags: mybatis
---
作为持久层的ORM框架,目前在国内主流之一就是MyBatis,学会用它,用好它肯定是必备的功课
我会主要从下面几个方面入整理本篇博客
快速搭建快发环境
常见的注解
怎么玩?
一. 快速搭建开发环境#
小插曲,添加测试模块的时候,引入junit模块和spring-boot-text-starter模块有先顺序,不然ide会报错...
坐标
Copy
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
tk.mybatis
mapper-spring-boot-starter
2.0.3
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
mysql
mysql-connector-java
8.0.11
junit
junit
text
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
另外插一嘴---mysql连接的版本适配
我现在用的云主机docker上的官方版mysql,版本比较新,因此我的调整版本到 8以上,不然会报错说什么
Copy
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could
配置文件:
主要是配置数据库的连接,如果我们使用的是通用mapper,Mybatis可以做到零配置
Copy
server:
port: 8089
spring:
application:
name: text-mybatis
datasource:
url: jdbc:mysql://211.159.XXX.XXX:8888/changwu?serverTimezone=UTC&useUnijava=true&characterEncoding=utf-8&useSSL=false
username: root
password: 2424zcw..
driver-class-name: com.mysql.jdbc.Driver
#输出sql
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类
在这里告诉通用mapper我们的mapper包路径
Copy
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.changwu.mapper")
public class MybatisApp {
public static void main(String[] args) {
SpringApplicat