MyBatis初级实战之五:⼀对⼀关联查询,linux操作系统实⽤教程⽂东⼽课后答
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⼯程
1. 在⽗⼯程mybatis下新建⼦⼯程relatedoperation,l如下:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
com.bolingcavalry
mybatis
1.0-SNAPSHOT
…/l
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
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
2. 基本配置⽂件l:
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: sql.cj.jdbc.Driver
#2.连接池配置
druid:
#初始化连接池的连接数量 ⼤⼩,最⼩,最⼤
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
配置⼀个连接在池中最⼤⽣存的时间,单位是毫秒
max-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM user
test-while-idle: true
test-on-borrow: true
test-on-return: false
是否缓存preparedStatement,也就是PSCache 官⽅建议MySQL下建议关闭个⼈建议如果想⽤SQL防⽕墙建议打开
pool-prepared-statements: true
max-pool-prepared-statement
《⼀线⼤⼚Java⾯试题解析+后端开发学习笔记+最新架构讲解视频+实战项⽬源码讲义》
【docs.qq/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
-per-connection-size: 20
配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall’⽤于防⽕墙
filters: stat,wall,slf4j
filter:
stat:
linux mysql教程视频merge-sql: true
slow-sql-millis: 5000
#3.基础监控配置
web-stat-filter:
enabled: true
url-pattern: /*
#设置不统计哪些URL
exclusions: “.js,.gif,.jpg,.png,.css,.ico,/druid/*”session-stat-enable: true
session-stat-max-count: 100
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
#设置监控页⾯的登录名和密码
login-username: admin
login-password: admin
allow: 127.0.0.1
#deny: 192.168.1.100
mybatis配置
mybatis:
配置⽂件所在位置
config-location: l
映射⽂件所在位置
mapper-locations: classpath:mappers/*l ⽇志配置
logging:
level:
root: INFO
com:
bolingcavalry:
relatedoperation:
mapper: debug
3. 再准备名为l的配置⽂件,这是执⾏单元测试时⽤到的,和l的不同之处是
spring.datasource.abled配置设置成false;
4. mybatis的配置⽂件l如下:
5. 数据源配置类DruidConfig.java:
package latedoperation;
import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
public class DruidConfig {
private static final Logger logger = Logger(DruidConfig.class);
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.druid.initial-size}")
private int initialSize;
@Value("${spring.datasource.druid.max-active}")
private int maxActive;
@Value("${spring.datasource.druid.min-idle}")
private int minIdle;
@Value("${spring.datasource.druid.max-wait}")
private int maxWait;
@Value("${spring.datasource.druid.pool-prepared-statements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。