再用mybatis-plus

博客介绍了项目中使用MyBatis-Plus可提高开发效率,并说明了重新配置的步骤,包括引入Maven依赖、配置参数、注入组件等。还提及逻辑删除和乐观锁功能及对应注解,强调代码生成器可简化开发。

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

这个项目又能用mybaits-plus了,太棒了,大大增加开发效率;重新配置下;

(1)引入maven依赖(没写版本号):

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
        </dependency>

(2)配置参数:

#驼峰命名
mybatis-plus.configuration.map-underscore-to-camel-case=true
#指定实体包
mybatis-plus.type-aliases-package=com.door.domain.entity
#指定xml
mybatis-plus.mapperLocations=classpath*:mapper/*.xml
#逻辑删除指定值
mybatis-plus.global-config.db-config.logic-delete-value=-1
mybatis-plus.global-config.db-config.logic-not-delete-value=1

(3)注入一些组件:

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    
    //乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

---逻辑删除自定注入了,实现功能删除自动使用指定字段逻辑删除;指定字段注解为:@TableLogic

---乐观锁就是每次更新都会查版本号并加一,例如:update table set  a = 1,version = version+1 where version = version;注解为:@Version

(4)代码生成器(这个是必须用的,大大简化开发):

package com.door.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.VelocityTemplateEngine;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Auther: gaoyang
 * @Date: 2019/5/22 17:51
 * @Description:
 */
public class CodeGenerator {

    public static void main(String[] args) {
        String url = "jdbc:MySql://xxxx?characterEncoding=UTF-8&allowMultiQueries=true&useSSL=true";
        //项目实际路径
        String projectPath = "C:\\java\\project\\menpad\\springboot\\door-spring-boot\\door-service";
        //登陆账号
        String username = "root";
        //密码
        String password = "xxxx";
        //要生成的表
        String tables = "tb_ware_house_new";
        //生成代码的包名
        String parent = "com.door";
        //各模块包名
        String entity = "domain.warehouse";
        String service = "service.warehouse";
        String mapper = "dao.warehouse";
        String controller = "";
        //主键类型(下面是用户输入,还有自增啥的)
        IdType idType = IdType.INPUT;
        //逻辑删除字段
        String delete = "yn";
        //乐观锁字段
        String version = "sys_version";
        generator(url, username, password, projectPath, parent, entity, mapper, service, tables, idType, delete, version, controller);
    }

    public static void generator(String url, String username, String password
            , String projectPath, String parent, String entity, String mapper, String service
            , String tables, IdType idType, String delete, String version, String controller) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("gaoyang");
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setIdType(idType);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //  pc.setModuleName("door-service");
        pc.setParent(parent);
        pc.setEntity(entity);
        pc.setServiceImpl(service + ".impl");
        pc.setMapper(mapper);
        pc.setService(service);
        pc.setController(controller);
        pc.setXml("");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        // String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        //自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
            }
        });
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();
 
      /*  templateConfig.setXml(null);
        templateConfig.setEntity("domain/%.java");
        templateConfig.setMapper("dao/%mapper.java");
        templateConfig.setService("service/%service.java");*/
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        //设置表
        strategy.setInclude(tables);
        // strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        // strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setLogicDeleteFieldName(delete);
        if (StringUtils.isNotBlank(version)) {
            strategy.setVersionFieldName(version);
        }
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值