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.Drivertype: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://${spring.datasource.ip}:${spring.datasource.port}/${spring.datasource.database}?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=trueip: 127.0.0.1port: 3306database: demousername: 123456password: 123456# 连接池的配置信息druid:# 初始化大小,最小,最大initial-size: 2min-idle: 1maxActive: 5# 配置获取连接等待超时的时间maxWait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒minEvictableIdleTimeMillis: 600000# 配置一个连接在池中最大生存的时间,单位是毫秒maxEvictableIdleTimeMillis: 900000connectionProperties: 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/*.xmlconfiguration:#开启自动驼峰命名规则(camel case)映射map-underscore-to-camel-case: true#延迟加载,需要和lazy-loading-enabled一起使用aggressive-lazy-loading: truelazy-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 {@Overridepublic 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