springBoot 整合mybatisPlus指定各类文件输出位置,springBoot整合p6spy

        最近使用了一下mybatisPlus(以下简称mp),在此记录一下其中代码生成器中如何指定各类文件的生成位置(因为mp可以帮助你生成controller、service、serviceImpl、mapper、mapperxml)文件。

        好了,话不多说,直接奔入主题。使用PackageConfig 这个对象就可以指定生成的各种文件的位置。一下是我的整个代码生成器内容。

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 当前程序所在目录
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("czw");
        gc.setFileOverride(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(true);
        // 是否覆盖同名文件,默认是false
        gc.setFileOverride(true);
        gc.setOpen(false);

        mpg.setGlobalConfig(gc);
        // 不需要ActiveRecord特性的请改为false
        gc.setActiveRecord(true);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/practicedb?useSSL=false&serverTimezone=CTT&nullNamePatternMatchesAll=true");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("***");
        dsc.setPassword("****");
        dsc.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                //timestamp转换成String
                if (fieldType.toLowerCase().contains("timestamp")) {
                    return DbColumnType.STRING;
                }
                //timestamp转换成String
                if (fieldType.toLowerCase().contains("datetime")) {
                    return DbColumnType.STRING;
                }
                return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setDataSource(dsc);


        // 包配置
        PackageConfig pc = new PackageConfig();
        // 父包名
        pc.setParent("com.mybatisplus");
        // 实体类包名
        pc.setEntity("models.sys.user");
        // Service包名
        pc.setService("service.sys.user");
        // ServiceImpl包名
        pc.setServiceImpl("service.sys.user.impl");
        // Mapper包名
        pc.setMapper("mapper.sys.user");
        // controller包名
        pc.setController("controller.sys.user");
        mpg.setPackageInfo(pc);

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

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

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mybatis/mapper/sys/user/"
                        //+ pc.getModuleName()
                        //+ "/"
                        + tableInfo.getEntityName() + "Mapper.xml";
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude("sys_user");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

        在此先记录下,后续会继续更新使用mp的各种感受。还有,明天就是咱们传统节日——七夕节了,祝各位猿友们明晚……。哈哈,有情人终成眷属!

 

-----------------------------------------------------------2020.09.03-----------------------------------------------------------------------------------------------

由于mp 3.2.0以上的版本不再支持 PerformanceInterceptor,官方文档推荐使用p6spy 组件。so,给项目整合一下这个组件。

第一步:添加依赖

        <!--SQL分析打印-p6spy-->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.9.1</version>
        </dependency>
第二步:修改数据库驱动
com.p6spy.engine.spy.P6SpyDriver

示例:

第三步:修改连接数据库url(在jdbc和mysql中间添加p6spy)

jdbc:p6spy:mysql://localhost:

示例:

第四步:添加spy.properties文件 

文件内容:

modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

至此,配置工作完成。启动项目

完成!

还有一个配置p6spy时出现的问题,就是完成上面这些配置后,启动项目时报错了:

经过好一番的排查才找到问题的原因:

druid的filters属性使用wall时就会出现上面的错误。

把wall去掉即可。进一步的原因还在研究中。若有知道的猿友可以评论一起讨论。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值