一、maven配置
<mybatis-plus.version>3.2.0</mybatis-plus.version>
<mysql-connector.version>8.0.13</mysql-connector.version>
<druid-spring-boot.version>1.1.22</druid-spring-boot.version>
<lombok.version>1.16.18</lombok.version>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
二、修改配置文件
application.yml
# 打印Mysql查询日志
logging:
level:
com.baijia.umetric.dao.mapper: debug
# mybatis插件打印日志,打印查询结果
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
main:
allow-bean-definition-overriding: true
# 延迟加载
jpa:
open-in-view: false
jdbc.properties
db-metric.datasource.name=metric-db
db-metric.datasource.type=com.alibaba.druid.pool.DruidDataSource
db-metric.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
db-metric.datasource.url=jdbc:mysql://ip地址:3306/measures_dict?\
allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
db-metric.datasource.username=root
db-metric.datasource.password=root
#最大连接池数量
db-metric.datasource.max-active=200
#获取连接时最大等待时间,单位毫秒.配置之后,缺省启用公平锁,并发效率会有所下降
db-metric.datasource.max-wait=10000
#初始化时建立物理连接的个数
db-metric.datasource.initial-size=10
# 用来检测连接是否有效的sql,要求是一个查询语句
db-metric.datasource.validation-query=SELECT 1
#申请连接时执行validationQuery检测连接是否有效,配置会降低性能
db-metric.datasource.test-on-borrow=false
#归还连接时执行validationQuery检测连接是否有效,配置会降低性能
db-metric.datasource.test-while-idle=true
#Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接
db-metric.datasource.time-between-eviction-runs-millis=18800
java的config目录新增MetricDbConfig.java
package com.baijia.umetric.config;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import lombok.extern.slf4j.Slf4j;
@Configuration
@Slf4j
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.baijia.umetric.dao.mapper")
public class MetricDbConfig {
@Primary
@Bean
@ConfigurationProperties(prefix = "db-metric.datasource")
public DataSource dataSource() {
log.info("-------------------- metric DataSource init ---------------------");
return DruidDataSourceBuilder.create().build();
}
}
新增分页插件配置 MybatisPlusConfig.java
package com.baijia.umetric.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
三、使用
MetricDTO.java
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
@TableName("metrices")
@Data
public class MetricDTO {
//主键
@TableId(type = IdType.AUTO)
private Integer id;
//精度,保留小数点位数
@TableField("`precision`")
private Integer precision;
}
MetricMapper.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@Mapper
public interface MetricMapper extends BaseMapper<MetricDTO> {
}
Service层使用
LambdaQueryWrapper<MetricDTO> userQueryWrapper = Wrappers.lambdaQuery();
userQueryWrapper.like(MetricDTO::getNameCn,"test");
Page<MetricDTO> page= new Page<>(1, 1);
metricMapper.selectPage(page, userQueryWrapper);