使用Mybatis-generator生成映射文件(源码下载),deleteByExample,batchInsert,updateBySelective,updateByExample的使用(三)

该篇博客详细介绍了如何使用Mybatis-generator生成映射文件,结合Mysql数据库进行DAO层的CRUD操作,包括单条删除、批量插入、按条件更新等常见功能的实现和测试用例。通过实例代码展示了updateByExample与updateByExampleSelective的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题背景

前几篇文章介绍了insert,selectByPrimaryKey,updateByPrimaryKey,deleteByPrimaryKey,selectByExample,selectOneByExample,insertSelective,countByExample
本章介绍deleteByExample(条件删除),batchInsert(批量插入),updateByExampleSelective(单个字段更新),updateByExample(整条更新)的使用

  • 默认已安装JDK
  • 默认已安装mysql

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),单条增删改查CRUD(一)

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),selectByExample,insertSelective,countByExample,selectOneByExample的使用(二)

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),deleteByExample,batchInsert,updateByExampleSelective,updateByExample的使用(三)

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),page分页和limit的使用(四)

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),insert和update插入返回带自增主键的两种方式(五)

DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),Datetime时间插入五种方式(六)

代码更改

1 下载上篇文章(二)的代码,依赖和插件配置不用改,只需要更改测试代码

package com.yg.mybatisgenerator.springbootTest;


import com.yg.mybatisgenerator.dao.mysql.GeneratorRecordMapper;
import com.yg.mybatisgenerator.entity.mysql.GeneratorRecord;
import com.yg.mybatisgenerator.entity.mysql.GeneratorRecordExample;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@Slf4j
public class GeneratorTest {

    @Autowired
    GeneratorRecordMapper generatorRecordMapper;

    // 单条插入测试
    @Test
    public void insertTest() {
        GeneratorRecord generatorRecord = GeneratorRecord.builder()
                .createTime("444444")
                .updateTime("222222")
                .build();
        int insert = generatorRecordMapper.insert(generatorRecord); // 成功返回1,插入一条数据成功
        log.info("insert: {}", insert);
        GeneratorRecord record = generatorRecordMapper.selectByPrimaryKey(1L);
        log.info("generatorRecord: {}", record);
    }

    // 单条查询测试,通过主键查询
    @Test
    public void selectTest() {
        GeneratorRecord record = generatorRecordMapper.selectByPrimaryKey(1L);
        log.info("generatorRecord: {}", record);
    }


    // 单条更新测试,通过主键更新
    @Test
    public void updateTest() {
        GeneratorRecord generatorRecord = GeneratorRecord.builder()
                .id(2L)
                .createTime("111111")
                .updateTime("222222")
                .build();
        int i = generatorRecordMapper.updateByPrimaryKey(generatorRecord);
        log.info("i: {}", i);
    }

    // 单条删除测试,通过主键进行删除
    @Test
    public void deleteTest() {
        int i = generatorRecordMapper.deleteByPrimaryKey(1L);
        log.info("i: {}", i);
    }

    // 根据字段条件选择测试
    @Test
    public void selectCriteriaTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("111111").andUpdateTimeEqualTo("222222");
        List<GeneratorRecord> generatorRecords = generatorRecordMapper.selectByExample(example);
        for (GeneratorRecord i : generatorRecords) {
            log.info("generatorRecord: {}", i);
        }
    }

    // 根据条件查询,选出第一个符合条件的,limit 1
    @Test
    public void selectoneTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("111111").andUpdateTimeEqualTo("222222");
        GeneratorRecord record = generatorRecordMapper.selectOneByExample(example);
        log.info("record: {}", record);
    }

    // 选择性插入测试
    @Test
    public void insertSelectTest() {
        GeneratorRecord generatorRecord = GeneratorRecord.builder() // 设置id会被自动覆盖
                .createTime("789")
                .build();
        int insert = generatorRecordMapper.insertSelective(generatorRecord); // 成功返回1,插入一条数据成功
        log.info("insert: {}", insert);
    }

    // 通过条件进行计数
    @Test
    public void countByExampleTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("444444");
        long count = generatorRecordMapper.countByExample(example);
        log.info("count: {}", count);
    }

    // 按照条件进行删除测试
    @Test
    public void deleteByExampleTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("789");
        int i = generatorRecordMapper.deleteByExample(example);  // 删除的条数
        log.info("i: {}", i);
    }

    // 批量插入测试
    @Test
    public void batchInsertTest() {
        List<GeneratorRecord> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            GeneratorRecord generatorRecord = GeneratorRecord.builder()
                    .createTime("147" + i)
                    .updateTime("258" + i)
                    .build();
            list.add(generatorRecord);
        }
        int i = generatorRecordMapper.batchInsert(list);  // list的插入成功的个数
        log.info("i: {}", i);
    }

    // 根据条件更新所有的符合条件的值,可以不用设置全部的字段,没有设置的字段保持为原值
    @Test
    public void updateByExampleSelectiveTest() {
        GeneratorRecord generatorRecord = new GeneratorRecord();
       // generatorRecord.setId(4L);
        generatorRecord.setCreateTime("123456");

        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("444444");
        int i = generatorRecordMapper.updateByExampleSelective(generatorRecord, example);
        log.info("i: {}", i);
    }

    // 主键必须设置表中有的值,其他字段没有设置会置为null,如果只更新一个或少数几个字段选择使用updateByExampleSelective
    @Test
    public void updateByExampleTest() {
        GeneratorRecord generatorRecord = GeneratorRecord.builder()
                .id(2L)
                .createTime("147")
            //    .updateTime("258")
                .build();

        GeneratorRecordExample example = new GeneratorRecordExample();
        GeneratorRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeEqualTo("x");
        int i = generatorRecordMapper.updateByExample(generatorRecord, example);
        log.info("i: {}", i);
    }
}

总结

1 updateByExample与updateByExampleSelective主要区别:

  • updateByExample的主键不能为空,criteria条件选项必须存在,非主键字段没有设置会默认设置为null
  • updateByExampleSelective没有设置的字段会保留之前的值,用来更新整个表某个字段的值




作为程序员第 48 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric:开始没人注意到我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值