mybatis-plus基本使用

  1. 引入依赖
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.2.0</version>
</dependency>
  1. 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
  1. 分页插件
@Configuration
public class MybatisPlusConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
  1. model
@TableName("tag_class")
public class TagClass {

    @TableId(type = IdType.AUTO)
    private Long id;

    private String className;

    private String pid;

	// get set 方法省略
	... 
}
  1. dao
public interface TagClassMapper extends BaseMapper<TagClass> {
}
  1. service
public interface TagClassService extends IService<TagClass> {
}
  1. serviceImpl
@Service
public class TagClassServiceImpl extends ServiceImpl<TagClassMapper, TagClass> implements TagClassService {
}
  1. 自定义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();
    }
}
  1. 自定义批量提交

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值