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

导读:本篇文章讲解 DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),page分页和limit的使用(四),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

问题背景

前几篇文章介绍了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);
    }

    // 分页插件测试
    @Test
    public void paginateTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        example.page(1,8); // 每页8条,查看第二页数据
        List<GeneratorRecord> generatorRecords = generatorRecordMapper.selectByExample(example);
        for (GeneratorRecord i : generatorRecords) {
            log.info("i: {}", i);
        }
    }

    // limit(Integer rows)测试
    @Test
    public void limitTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        example.limit(3);  // 输出前三条数据
        List<GeneratorRecord> generatorRecords = generatorRecordMapper.selectByExample(example);
        for (GeneratorRecord i : generatorRecords) {
            log.info("i: {}", i);
        }
    }

    // limit(Integer offset, Integer rows)测试
    @Test
    public void limitOffsetTest() {
        GeneratorRecordExample example = new GeneratorRecordExample();
        example.limit(3,3); // 从第4条数据开始,输出一共3条数据,就是第4-6条数据
        List<GeneratorRecord> generatorRecords = generatorRecordMapper.selectByExample(example);
        for (GeneratorRecord i : generatorRecords) {
            log.info("i: {}", i);
        }
    }
}

总结

  • 分页是提高效率的一种方式,所以在查询时可以选择使用

作为程序员第 49 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),page分页和limit的使用(四)DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),page分页和limit的使用(四)DAO层使用Mybatis-generator生成映射文件连接Mysql入门测试用例(无限速源码下载),page分页和limit的使用(四)

Lyric:等雨变强之前

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110826.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!