- 引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
- yml配置
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印在控制台,在开发或测试的时候可以用。生产环境需要注释掉,否则debug日志将打印不出来
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 分页插件
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
- model
@TableName("tag_class")
public class TagClass {
@TableId(type = IdType.AUTO)
private Long id;
private String className;
private String pid;
// get set 方法省略
...
}
- dao
public interface TagClassMapper extends BaseMapper<TagClass> {
}
- service
public interface TagClassService extends IService<TagClass> {
}
- serviceImpl
@Service
public class TagClassServiceImpl extends ServiceImpl<TagClassMapper, TagClass> implements TagClassService {
}
- 自定义sql方法
dao
public interface GroupInfoMapper extends BaseMapper<GroupInfo> {
@Update("update group_info\n" +
"set group_status = \n" +
"case \n" +
"when end_date < now() and group_status != 3 then 3 -- 过期\n" +
"when start_date < now() and end_date > now() then 2 -- 计算中\n" +
"else group_status\n"+
"end ")
boolean updateGroupStatus();
}
serviceImpl
@Service
public class GroupInfoServiceImpl extends ServiceImpl<GroupInfoMapper, GroupInfo> implements GroupInfoService {
@Override
public boolean updateGroupStatus(){
return this.baseMapper.updateGroupStatus();
}
}
- 自定义批量提交
dao
public interface GroupUserDetailMapper extends BaseMapper<GroupUserDetail> {
@Insert("insert ignore into group_user_detail (group_id, user_id, stat_date)" +
" values(#{groupId}, #{userId}, #{statDate})")
boolean ignoreInset(GroupUserDetail groupUserDetail);
}
serviceImpl
@Service
public class GroupUserDetailServiceImpl extends ServiceImpl<GroupUserDetailMapper, GroupUserDetail> implements GroupUserDetailService {
@Override
public boolean ignoreBatch(List<GroupUserDetail> groupUserDetails, int batchSize) throws Exception{
String className = GroupUserDetailMapper.class.getName();
String methodName = GroupUserDetailMapper.class.getMethod("ignoreInset",GroupUserDetail.class).getName();
String sqlStatement = className + "." + methodName;
SqlSession batchSqlSession = this.sqlSessionBatch();
int i = 0;
for (GroupUserDetail groupUserDetail : groupUserDetails) {
batchSqlSession.insert(sqlStatement, groupUserDetail);
i += 1;
if (i % batchSize == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.flushStatements();
return true;
}
}