id
int(32) NOT NULL AUTO_INCREMENT,
name
varchar(32) NOT NULL,
age
int(32) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS log
;
CREATE TABLE log
(
id
int(32) NOT NULL AUTO_INCREMENT,
user_id
int(32),
action
varchar(255) NOT NULL,
create_time
datetime not null,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO mybatis.user (id, name, age) VALUES (3, ‘tom’, 11);
INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (3, 3, ‘read book’, ‘2020-08-07 08:18:16’);
INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (4, 3, ‘go to the cinema’, ‘2020-09-02 20:00:00’);
INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (5, 3, ‘have a meal’, ‘2020-10-05 12:03:36’);
INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (6, 3, ‘have a sleep’, ‘2020-10-06 13:00:12’);
INSERT INTO mybatis.log (id, user_id, action, create_time) VALUES (7, 3, ‘write’, ‘2020-10-08 09:21:11’);
本次实战的java工程
- 在父工程mybatis下新建子工程relatedoperation,pom.xml如下:
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.bolingcavalry
mybatis
1.0-SNAPSHOT
…/pom.xml
com.bolingcavalry
relatedoperation
0.0.1-SNAPSHOT
relatedoperation
Demo project for Mybatis related operation in Spring Boot
<java.version>1.8</java.version>
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
com.alibaba
druid-spring-boot-starter
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
- 基本配置文件application.yml:
server:
port: 8080
spring:
#1.JDBC数据源
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.50.43:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
#2.连接池配置
druid:
#初始化连接池的连接数量 大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
t