美文网首页
Java—SpringBoot+MyBatis自动生成CRUD完

Java—SpringBoot+MyBatis自动生成CRUD完

作者: 任小鲜儿 | 来源:发表于2020-03-27 11:47 被阅读0次

    这个方法是跟一个同事学习到的,真的是很好用

    1.pom.xml文件配置

    为pom文件增加以下依赖:

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.30</version>
    </dependency>
    

    2.pom.xml完整配置

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo2</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <mybatis-plus.version>3.0.7</mybatis-plus.version>
            <spring.boot.version>2.1.1.RELEASE</spring.boot.version>
            <org.springframework.version>5.1.3.RELEASE</org.springframework.version>
            <trace.version>4.1.0</trace.version>
            <springboot.hystrix.version>2.1.0.RELEASE</springboot.hystrix.version>
            <junit.version>4.12</junit.version>
            <mockito.version>2.8.9</mockito.version>
            <powermock.version>1.7.4</powermock.version>
            <jmockit.version>1.43</jmockit.version>
            <logger-appender.version>2.0.4</logger-appender.version>
            <commons-collection4.version>4.0</commons-collection4.version>
            <mybatis-spring.version>RELEASE</mybatis-spring.version>
            <druid.version>1.0.29</druid.version>
            <mysql-connector-java.version>5.1.42</mysql-connector-java.version>
            <mybatis-spring-boot-starter.version>1.3.0</mybatis-spring-boot-starter.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <!-- 生成器依赖 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.30</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- springframework -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
    
            <!-- unit test dependency start -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${spring.boot.version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>org.mockito</artifactId>
                        <groupId>mockito-core</groupId>
                    </exclusion>
                </exclusions>
                <scope>test</scope>
            </dependency>
            <!--  Mockito in spring-boot-test is 2.15.0, not matching Powermock 1.7.x, so include Mockito seperately.-->
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>${mockito.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito2</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jmockit</groupId>
                <artifactId>jmockit</artifactId>
                <version>${jmockit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>${commons-collection4.version}</version>
            </dependency>
            <!-- 添加druid数据源依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- 添加mysql数据源驱动依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector-java.version}</version>
            </dependency>
    
            <!--curator-->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>4.0.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.0.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--jetty-->
            <dependency>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty</artifactId>
                <version>6.1.26</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>transmittable-thread-local</artifactId>
                <version>2.10.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-client</artifactId>
                <version>4.3.0</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    3.启动类

    package com.bot.base.data;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.core.toolkit.StringPool;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.InjectionConfig;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * mysql 代码生成器演示例子
     *
     * @author RL
     * @since 2019-09-12
     */
    public class MysqlGenerator {
    
        /**
         * 读取控制台内容
         */
        public static String scanner(String tip) {
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + ":");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
                String ipt = scanner.next();
                if (StringUtils.isNotEmpty(ipt)) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }
    
        /**
         * RUN THIS
         */
        public static void main(String[] args) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置(生成在本地)
            GlobalConfig gc = new GlobalConfig();
            String projectPath = "D://";
            gc.setOutputDir(projectPath + "MysqlGenerator/src/main/java");
            gc.setAuthor("RL");
            gc.setOpen(false);
            gc.setIdType(IdType.AUTO);
            gc.setDateType(DateType.ONLY_DATE);
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            String baseDataDBUrl = "jdbc:mysql://localhost:3306/base_data?useUnicode=true&characterEncoding=UTF-8";
    
            dsc.setUrl(baseDataDBUrl);
            // dsc.setSchemaName("public");
            dsc.setDriverName("com.mysql.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123456");
            mpg.setDataSource(dsc);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setEntity("entity");
            pc.setMapper("mapper");
            pc.setParent("com.bot.base.data");
            mpg.setPackageInfo(pc);
    
            // 自定义配置
            InjectionConfig cfg = new InjectionConfig() {
                @Override
                public void initMap() {
                    // to do nothing
                }
            };
            List<FileOutConfig> focList = new ArrayList<>();
            focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    // 自定义输入文件名称
                    return projectPath + "MysqlGenerator/src/main/resources/mapper/"
                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            });
            cfg.setFileOutConfigList(focList);
            mpg.setCfg(cfg);
            mpg.setTemplate(new TemplateConfig().setXml(null));
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    
    
            strategy.setEntityLombokModel(true);
            strategy.setSuperEntityColumns("operator","is_delete","create_time","update_time");
            strategy.setSuperEntityClass("com.bot.base.data.entity.BaseEntity");
            //strategy.setSuperControllerClass("com.bot.base.data.common.BaseController");
            strategy.setInclude(("domain_info,skill_info").split(","));
            strategy.setControllerMappingHyphenStyle(true);
            //strategy.setRestControllerStyle(true);
            //strategy.setTablePrefix(pc.getModuleName() + "_");
            mpg.setStrategy(strategy);
            // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
            mpg.execute();
        }
    
    }
    

    4.生成entity文件及常用注解

    package com.bot.base.data.entity;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableId;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.util.Date;
    /**
     * MysqlGenerator 中的配置可以生成基类:
     *  strategy.setSuperEntityColumns("operator","is_delete","create_time","update_time");
     *  strategy.setSuperEntityClass("com.bot.base.data.entity.BaseEntity");
     */
    @Data
    @EqualsAndHashCode(callSuper = true)
    @Accessors(chain = true)
    public class ChannelInfo extends BaseEntity {
    
        private static final long serialVersionUID = 1L;
        /**
         * id
         */
        @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
        /**
         * 渠道id
         */
        private Integer channelId;
        /**
         * 渠道名称(数据库中是json格式,在info文件中用string)
         */
        private String channelName;
        /**
         * 简介说明(value = "`desc`",表示生成sql时的列名,因desc是关键字所以加引号)
         */
        @TableField(value = "`desc`")
        private String desc;
    
    }
    

    5.entity的基类

    package com.bot.base.data.entity;
    
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableLogic;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public class BaseEntity {
    
        @TableField(fill = FieldFill.INSERT)
        private Date createTime;
    
        @TableField(fill = FieldFill.INSERT_UPDATE)
        private Date updateTime;
    
        private String operator;
    
        @JsonIgnore
        @TableLogic
        @TableField(fill = FieldFill.INSERT)
        private Integer isDelete;
    }
    

    6.dto类举例

    package com.bot.base.data.sdk.dto;
    
    import lombok.Data;
    
    /**
     * @ClassName ChannelDto
     * @Description todo
     * @Author RL
     * @Date 2020/3/23
     * @Version 1.0
     **/
    @Data
    public class ChannelDto extends BaseDto{
        /**
         * 渠道id
         */
        private Integer channelId;
        /**
         * 渠道名称
         */
        private String channelName;
        /**
         * 简介说明
         */
        private String desc;
    }
    

    7.dto的基类

    package com.bot.base.data.sdk.dto;
    
    import com.alibaba.fastjson.JSON;
    import lombok.Data;
    
    import java.util.Date;
    
    /**
     * @ClassName BaseDto
     * @Description todo
     * @Author RL
     * @Date 2020/3/23
     * @Version 1.0
     **/
    @Data
    public class BaseDto {
    
        private Integer id;
    
        private Date createTime;
    
        private Date updateTime;
    
        private String operator;
    
        private boolean isDelete;
    
        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
    

    8.包括部分设置的过滤器

    package com.bot.base.data.framework.mybatis;
    
    import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
    import org.apache.ibatis.reflection.MetaObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class FieldFillHandler implements MetaObjectHandler {
    
        private static final Logger logger = LoggerFactory.getLogger(FieldFillHandler.class);
    
    
        @Override
        public void insertFill(MetaObject metaObject) {
            logger.info("Mybatis新增数据自动填充通用字段开始......");
            Date now = new Date();
            this.setInsertFieldValByName("createTime",now,metaObject);
            this.setInsertFieldValByName("updateTime",now,metaObject);
            this.setInsertFieldValByName("isDelete",0,metaObject);
    
        }
    
        @Override
        public void updateFill(MetaObject metaObject) {
            logger.info("Mybatis修改数据自动填充通用字段开始......");
            Date now = new Date();
            this.setUpdateFieldValByName("updateTime",now,metaObject);
        }
    }
    
    

    9.对外sdk接口的设计举例

    1.接口设计
    package com.bot.base.data.sdk;
    
    import com.bot.base.data.sdk.dto.*;
    
    import java.util.List;
    
    /**
     * Author: RL
     * Date: 2020/4/1
     * Time: 10:31
     * Description:
     *      insert接口:参数dto ——> 要插入的列值放入dto相关字段中
     *      update接口:参数dto ——> 必须有id(作为定位),要更新的列值放入dto相关字段中
     *      delete接口: 参数id  ——> 删除数据的唯一id
     *      select接口:参数dto ——> 要查询的列值放入dto相关字段中
     *                  参数pageCurrent ——> 当前页码
     *                  参数pageTotal ——> 当前页的数据条数
     */
    public interface OperateService {
        boolean channelInsert(ChannelDto channelDto);
        boolean channelUpdate(ChannelDto channelDto);
        boolean channelDelete(int id);
        List<ChannelDto> channelSelectByPage(ChannelDto channelDto, int pageCurrent, int pageTotal);
    }
    
    
    2.实现类设计
    package com.bot.base.data.operate;
    
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.bot.base.data.entity.*;
    import com.bot.base.data.sdk.OperateService;
    import com.bot.base.data.sdk.dto.*;
    import com.bot.base.data.service.*;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.BeanUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.*;
    
    /**
     * Author: RL
     * Date: 2020/3/31
     * Time: 10:31
     * Description: No Description
     */
    @Service
    public class OperateServiceImpl implements OperateService {
        private Logger logger = LoggerFactory.getLogger(OperateServiceImpl.class);
        @Autowired
        private IChannelInfoService iChannelInfoService;
        
        @Override
        public boolean channelInsert(ChannelDto channelDto) {
            if(!isJson(channelDto.getChannelName())){
                return false;
            }
            ChannelInfo info = new ChannelInfo();
            BeanUtils.copyProperties(channelDto,info);
            boolean result = false;
            try{
                result = iChannelInfoService.save(info);
            } catch (Exception e){
                logger.error("insert接口返回有误:" + e);
            }
            return result;
        }
    
        @Override
        public boolean channelUpdate(ChannelDto channelDto) {
            if(channelDto.getId() == null){
                logger.warn("参数中缺少唯一id,update失败");
                return false;
            }
            if(!isJson(channelDto.getChannelName())){
                return false;
            }
            ChannelInfo info = new ChannelInfo();
            BeanUtils.copyProperties(channelDto,info);
            boolean result = false;
            try{
                result = iChannelInfoService.updateById(info);
            } catch (Exception e){
                logger.error("update接口返回有误:" + e);
            }
            return result;
        }
    
        @Override
        public boolean channelDelete(int id) {
            boolean result = false;
            try{
                result = iChannelInfoService.removeById(id);
            } catch (Exception e){
                logger.error("delete接口返回有误:" + e);
            }
            return result;
        }
    
        @Override
        public List<ChannelDto> channelSelectByPage(ChannelDto channelDto, int pageCurrent, int pageTotal) {
            IPage<ChannelInfo> iPage = new Page();
            iPage.setCurrent(pageCurrent);
            iPage.setTotal(pageTotal);
    
            Map<String, Object> map = new HashMap<>();
            map.putAll(getCommonMap(channelDto));
            map.put("channel_id",channelDto.getChannelId());
            map.put("channel_name",channelDto.getChannelName());
            map.put("`desc`",channelDto.getDesc());
    
            Wrapper<ChannelInfo> wrapper = new QueryWrapper();
            ((QueryWrapper<ChannelInfo>) wrapper).allEq(map,false);
            List<ChannelDto> dtoList = new ArrayList<>();
            IPage<ChannelInfo> iPages = new Page<>();
            try{
                iPages = iChannelInfoService.page(iPage,wrapper);
            } catch (Exception e){
                logger.error("select接口返回有误:" + e);
            }
            for (int i=0; i<iPages.getRecords().size(); i++){
                ChannelDto tempDto = new ChannelDto();
                BeanUtils.copyProperties(iPages.getRecords().get(i),tempDto);
                dtoList.add(tempDto);
            }
            return dtoList;
        }
    
        /**
          * dto的共同字段的基类添加查询字段时的公共函数
          */
        private Map<String,Object> getCommonMap(BaseDto baseDto){
            Map<String,Object> map = new HashMap<>();
            map.put("id",baseDto.getId());
            map.put("create_time",baseDto.getCreateTime());
            map.put("update_time",baseDto.getUpdateTime());
            map.put("operator",baseDto.getOperator());
            map.put("is_delete",false);
            return map;
        }
        /**
          * 判断json是否合法
          */
        private boolean isJson(String text){
            try {
                JSONObject.parseObject(text);
                return true;
            } catch (Exception e) {
                logger.error("JSON字符串非法:{}" + text + e);
                return false;
            }
        }
    
    }
    
    3.自测用例
    package com.bot.base.data;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.bot.base.data.sdk.OperateService;
    import com.bot.base.data.sdk.dto.*;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Author: RL
     * Date: 2020/4/1
     * Time: 17:25
     * Description: No Description
     */
    @RunWith(SpringRunner.class)
    @ActiveProfiles("local")
    @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
    public class OperateTest {
        @Autowired
        private OperateService operateService;
    
        @Test
        public void testChannel(){
            /**
             insert
             */
            ChannelDto channelDto = new ChannelDto();
            channelDto.setChannelId(10007);
            channelDto.setChannelName("oooo");
            channelDto.setDesc("wowo");
            channelDto.setOperator("1");
            //System.out.println(operateService.channelInsert(channelDto));
            /**
             update
             */
            ChannelDto channelDto2 = new ChannelDto();
            channelDto2.setId(6);
            channelDto2.setChannelName("oppo101");
            channelDto2.setDesc("oppo version 101");
            channelDto2.setDelete(true);
            //System.out.println(operateService.channelUpdate(channelDto2));
            /**
             * delete
             */
            //System.out.println(operateService.channelDelete(7));
            /**
             * select
             */
            ChannelDto channelDto3 = new ChannelDto();
            //channelDto3.setChannelId(10004);
            channelDto3.setDelete(true);
            channelDto3.setDesc("sofa");
            //List<ChannelDto> list = operateService.channelSelectByPage(channelDto3,1,10);
            //System.out.println(list.size() +"\n" + (list.size() == 0 ? "没有合适的结果" : list).size());
        }
    }
    

    相关文章

      网友评论

          本文标题:Java—SpringBoot+MyBatis自动生成CRUD完

          本文链接:https://www.haomeiwen.com/subject/jccluhtx.html