Mybatis、MybatisPlus 简单介绍
MybatisPlus 集成
一、pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
一、yml配置
spring.datasource p配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://${spring.datasource.ip}:${spring.datasource.port}/${spring.datasource.database}?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true
ip: 127.0.0.1
port: 3306
database: demo
username: 123456
password: 123456
# 连接池的配置信息
druid:
# 初始化大小,最小,最大
initial-size: 2
min-idle: 1
maxActive: 5
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 600000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500;druid.mysql.usePingMethod=false
mybatis-plus 配置
mybatis-plus:
#扫mapper包路径类似做到 @MapperScan("com.hxl.starter.mapper")
base-package: com.hxl.starter.mapper.**
mapper-locations: classpath:mapping/*.xml
configuration:
#开启自动驼峰命名规则(camel case)映射
map-underscore-to-camel-case: true
#延迟加载,需要和lazy-loading-enabled一起使用
aggressive-lazy-loading: true
lazy-loading-enabled: true
#关闭一级缓存
local-cache-scope: statement
#关闭二级级缓存
cache-enabled: false
二、mapper、service、po 写法示例
UserMapper
public interface UserMapper extends BaseMapper<UserPo> {
}
service
public interface UserService extends IService<UserPo> {
Long getUserId();
}
ServiceImpl
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {
@Override
public Long getUserId() {
LambdaQueryWrapper<UserPo> wrapper = Wrappers.lambdaQuery();
List<UserPo> list = list(wrapper);
return CollectionUtil.isNotEmpty(list) ? list.get(0).getId() : 0L;
}
}
po 支持注解
@Getter
@Setter
@ToString(callSuper = true)
@TableName("user")
public class UserPo {
/**
* id
*/
@TableId("id")
private Integer id;
/**
* 账号
*/
@TableField("user_name")
private String userName;
/**
* 密码
*/
@TableField("pass_word")
private String passWord;
}
github 地址:
https://github.com/sanxiaoshitou/tower-boot