栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

问题背景

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的使用(四) 代码更改测试心得Lyric:这样也好

问题背景

除了上篇文章的增删改查的基本操作以外,通过安装generator插件可以增加一些常用的sql语句

默认已安装mysql默认已安装JDK根据上篇文章的代码做一些改动,也可以直接下载源码进行参考 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的使用(四) 代码更改

1 新增的sql语句需要添加额外的依赖,更改pom文件,如果不能下载依赖,可以尝试把插件里面的依赖放入到外层的依赖里面进行下载之后,再放回原处



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.3
         
    
    com.yg
    mybatisGenerator
    0.0.1-SNAPSHOT
    mybatisGenerator
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            
        
        

        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        
        
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.7
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.6.0
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            

            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.7
                
                    
                        com.itfsw
                        mybatis-generator-plugin
                        1.3.8
                    
                
                
                    
                    true
                    
                    true
                    
                    
                        src/main/resources/generatorConfig.xml
                    
                
            

        
    

2 更改generatorConfig配置文件,添加插件





    
    
    
    
    
    

    
    
    

        
        
        
        
        
        

        
        
        
        
        
        
        
        
        
            
        
        
            
            
            
            
        
        
        
        
        
            
        
        
        
        
        
        
        
        




        
        
        









        

        
            
            
            
            
        


        
        
            
            
            
        

        
        
            
            
        

        
        
            
            
            
        

        
        
            
        

        
        
            
        

        
        

        
        
        
        

3 点击maven插件中的generate进行生成代码

4 生成更多的sql方法,基本上这些方法不是在特殊的要求下都是够用的

5 添加新的测试方法

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.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 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);
    }
}

测试

1 countByExample计数测试,通过条件筛选计数

心得

增加了selectByExample,insertSelective,countByExample,selectOneByExample的方法,并测试通过




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

Lyric:这样也好
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号