记录一个mybatis自动生成代码

1.pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.boot
    demo
    0.0.1-SNAPSHOT
    test_sm
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1.tmp
        
        
            com.baomidou
            mybatis-plus-generator
            3.3.1.tmp
        

        
            
            org.junit.platform
            junit-platform-launcher
            test
        

        
        
            com.alibaba
            druid
            1.1.12
        
        
            mysql
            mysql-connector-java
        

        
        
            org.apache.velocity
            velocity-engine-core
            2.2
        
        
            org.freemarker
            freemarker
            2.3.30
        
        
            com.ibeetl
            beetl
            3.0.20.RELEASE
        
        
            org.springframework
            spring-test
            5.2.5.RELEASE
            compile
        
        
            junit
            junit
        
        
            org.springframework.boot
            spring-boot-test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.application.yml

# DataSource Config
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
    #mysql8版本以上驱动包指定新的驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    #   数据源其他配置, 在 DruidConfig配置类中手动绑定
    initialSize: 8
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL

3.写一个工具类:

package com.boot.joy.test;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
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.rules.NamingStrategy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestGenerator {

    private static String author ="joy"; //作者名称
    private static String outPath = "E:\\work_test\\generator";//生成的位置
    private static String driver = "com.mysql.cj.jdbc.Driver";//驱动,注意版本
    //连接路径,注意修改数据库名称
    private static String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8";//
    private static String username = "root";//数据库用户名
    private static String password = "123456";//数据库密码
    private static String tablePrefix = "";//数据库表前缀
    private static String [] tables = {"user"};//生成的表,使用逗号分隔
    private static String parentPackage = "com.boot.joy";//顶级包的结构
    private static String mapper = "mapper";//数据库访问层名称
    private static String service = "service";//业务逻辑层包名称
    private static String entity = "entity";//实体层包名称
    private static String controller = "controller";//控制逻辑层包名称
    private static String mapperxml = "dao";//mapper映射包名称

    @Test
    public void testCreat() {

        //1.全局配置
        GlobalConfig config = new GlobalConfig();
        config.setAuthor(author)//作者
                .setOutputDir(outPath)//生成路径
                .setFileOverride(true)//文件覆盖
                .setIdType(IdType.AUTO)//主键策略
                .setServiceName("%sService")//设置生成的Service接口名字的首字母是否为I,加上%s则不生成I
                .setBaseResultMap(true)//映射文件中是否生成ResultMap配置
                .setBaseColumnList(true);//生成通用sql字段

        //2.数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)//设置数据库类型
                .setDriverName(driver)//设置驱动
                .setUrl(url)//设置连接路径
                .setUsername(username)//设置用户名
                .setPassword(password);//设置密码

        //3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)//全局大写命名
                .setNaming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略
                .setTablePrefix(tablePrefix)//表前缀
                .setInclude(tables);//生成的表

        //4.包名策略
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(parentPackage)//顶级包结构
                .setMapper(mapper)//数据访问层
                .setXml(mapperxml)//mapper映射文件
                .setService(service)//业务逻辑层
                .setController(controller)//控制器
                .setEntity(entity);//实体类
        //5.综合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig);
        //6.执行
        autoGenerator.execute();

    }
}


4.运行根据类就好了

 

你可能感兴趣的:(#,后端,Mybatis,mybatis)