mybatis plus 配置

pom.xml

	<!--整合 mybatis plus start  -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatisplus-spring-boot-starter</artifactId>
			<version>1.0.5</version>
		</dependency>
		<!-- MP 核心库 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus</artifactId>
			<version>2.1.8</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<!-- 模板引擎 -->
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId>
			<version>2.0</version>
		</dependency>

		<!-- 模板引擎,需要指定 mpg.setTemplateEngine(new FreemarkerTemplateEngine()); -->
		<!--<dependency>-->
			<!--<groupId>org.freemarker</groupId>-->
			<!--<artifactId>freemarker</artifactId>-->
			<!--<version>2.3.28</version>-->
		<!--</dependency>-->
		<!--整合 mybatis plus end  -->

整合的时候如果有不明白的报错原因,首先查看一下是不是包的版本有问题

application.yml


#mybatis-plus:
#  mapper-locations: classpath:mybatis/mapper/*Mapper.xml
#  type-aliases-package: com.szsm.entity
#  global-config:
#    #主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
#    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
#    id-type: 2
#    #字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
#    #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
#    field-strategy: 2
#     #驼峰下划线转换
#    db-column-underline: true
#    #刷新mapper 调试神器
#    refresh-mapper: true
#    #数据库大写下划线转换
#    #capital-mode: true
#    #序列接口实现类配置
#    #key-generator: com.baomidou.springboot.xxx
#    #逻辑删除配置(下面3个配置)
#    logic-delete-value: 0
#    logic-not-delete-value: 1
#    #自定义SQL注入器
#    #sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
#    #自定义填充策略接口实现
#    #meta-object-handler: com.baomidou.springboot.xxx

    #原生配置
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
MysqlGenerator(代码生成器)
/**
 * Copyright (c) 2011-2016, hubin ([email protected]).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.szsm.config.mybatisplus;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 *   @Description : MybatisPlus代码生成器
 *   ---------------------------------
 *   @Author : gaojme
 *   @Date : Create in 2017/9/19 14:48 
 */
public class MysqlGenerator {

    private static String packageName="sell";    //文件路径
    private static String authorName="gaojme";     //作者
    private static String table="order_detail";                  //table名字
    private static String prefix="sc_";                     //table前缀
    private static File file = new File(packageName);
    private static String path = file.getAbsolutePath();

    public static void main(String[] args) {
        // 自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE));
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
                // 全局配置
                new GlobalConfig()
//                        .setOutputDir(path+"/src/main/java")//输出目录
                        .setOutputDir("D://codeGen/src/main/java")//输出目录
                        .setFileOverride(true)// 是否覆盖文件
                        .setActiveRecord(true)// 开启 activeRecord 模式
                        .setEnableCache(false)// XML 二级缓存
                        .setBaseResultMap(true)// XML ResultMap
                        .setBaseColumnList(true)// XML columList
                        .setOpen(true)//生成后打开文件夹
                        .setAuthor(authorName)
                        // 自定义文件命名,注意 %s 会自动填充表实体属性!
                        .setMapperName("%sMapper")
                        .setXmlName("%sMapper")
                        .setServiceName("%sService")
                        .setServiceImplName("%sServiceImpl")
                        .setControllerName("%sController")
        ).setDataSource(
                // 数据源配置
                new DataSourceConfig()
                        .setDbType(DbType.MYSQL)// 数据库类型
                        .setTypeConvert(new MySqlTypeConvert() {
                            // 自定义数据库表字段类型转换【可选】
                            @Override
                            public DbColumnType processTypeConvert(String fieldType) {
                                System.out.println("转换类型:" + fieldType);
                                // if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
                                //    return DbColumnType.BOOLEAN;
                                // }
                                return super.processTypeConvert(fieldType);
                            }
                        })
                        .setDriverName("com.mysql.jdbc.Driver")
                        .setUsername("root")
                        .setPassword("123456")
                        .setUrl("jdbc:mysql://192.168.199.210:3306/sell?characterEncoding=utf8")
        ).setStrategy(
                // 策略配置
                new StrategyConfig()
                        // .setCapitalMode(true)// 全局大写命名
                        //.setDbColumnUnderline(true)//全局下划线命名
//                        .setTablePrefix(new String[]{prefix})// 此处可以修改为您的表前缀
                        .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                        .setInclude(new String[] { table }) // 需要生成的表
                        .setRestControllerStyle(true)
                        //.setExclude(new String[]{"test"}) // 排除生成的表
                        // 自定义实体父类
                        // .setSuperEntityClass("com.baomidou.demo.TestEntity")
                        // 自定义实体,公共字段
                        //.setSuperEntityColumns(new String[]{"test_id"})
                        .setTableFillList(tableFillList)
                        // 自定义 mapper 父类
                        // .setSuperMapperClass("com.baomidou.demo.TestMapper")
                        // 自定义 service 父类
                        // .setSuperServiceClass("com.baomidou.demo.TestService")
                        // 自定义 service 实现类父类
                        // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")
                        // 自定义 controller 父类
//                        .setSuperControllerClass("com.szsm."+packageName+".controller.AbstractController")
                        .setSuperControllerClass("com.szsm.entity.common.AbstractController")
                // 【实体】是否生成字段常量(默认 false)
                // public static final String ID = "test_id";
                // .setEntityColumnConstant(true)
                // 【实体】是否为构建者模型(默认 false)
                // public User setName(String name) {this.name = name; return this;}
                // .setEntityBuilderModel(true)
                // 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
                // .setEntityLombokModel(true)
                // Boolean类型字段是否移除is前缀处理
                // .setEntityBooleanColumnRemoveIsPrefix(true)
                // .setRestControllerStyle(true)
                // .setControllerMappingHyphenStyle(true)
        ).setPackageInfo(
                // 包配置
                new PackageConfig()
                        //.setModuleName("User")
//                        .setParent("com.szsm."+packageName)// 自定义包路径
                        .setParent("com.szsm")// 自定义包路径
                        .setController("controller")// 这里是控制器包名,默认 web
                        .setEntity("entity")
                        .setMapper("mapper")
                        .setService("service")
                        .setServiceImpl("service.impl")
                //.setXml("mapper")
        ).setCfg(
                // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String, Object> map = new HashMap<>();
                        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                        this.setMap(map);
                    }
                }.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig("/templates/mapper.xml.vm") {
                    // 自定义输出文件目录
                    @Override
                    public String outputFile(TableInfo tableInfo) {
//                        return path+"/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
                        return "D://codeGen/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";

                    }
                }))
        ).setTemplate(
                // 关闭默认 xml 生成,调整生成 至 根目录
                new TemplateConfig().setXml(null)
                // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
                // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
//                 .setController("/templates/mybatisgenerator/controller.java.vm")
//                 .setEntity("/templates/mybatisgenerator/entity.java.vm")
//                 .setMapper("/templates/mybatisgenerator/mapper.java.vm")
//                 .setXml("/templates/mybatisgenerator/mapper.xml.vm")
//                 .setService("/templates/mybatisgenerator/service.java.vm")
//                 .setServiceImpl("/templates/mybatisgenerator/serviceImpl.java.vm")
        );

        // 执行生成
        mpg.execute();

        // 打印注入设置,这里演示模板里面怎么获取注入内容【可无】
        System.err.println(mpg.getCfg().getMap().get("abc"));
    }

}
扫描二维码关注公众号,回复: 3213082 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_33146717/article/details/81537436