mybatis-plus程式碼生成器完整範例程式碼

2021-05-10 21:01:36

直接程式碼展示:

package cn.qxstar;

import cn.qxstar.common.base.entity.BaseEntity;
import cn.qxstar.common.utils.web.controller.BaseController;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import org.junit.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;

public class TestMP {

    /**
     * 程式碼生成    範例程式碼
     */
    @Test
    public void testGenerator() {
        //1. 全域性設定
        GlobalConfig config = new GlobalConfig();
        config.setOpen(false)
                .setAuthor("莐星") // 作者
                .setOutputDir("D:\\softWare\\IDEA\\workspace\\chstar-blog\\chstar-service\\biz-service\\src\\main\\java") // 生成路徑
                .setFileOverride(true)  // 檔案覆蓋
                .setIdType(IdType.ASSIGN_ID) // 主鍵策略 數位id
                .setServiceName("%sService")  // 設定生成的service介面的名
                .setMapperName("%sMapper")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController")
                .setSwagger2(true)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setXmlName("%sMapper")
                .setDateType(DateType.TIME_PACK);

        //2. 資料來源設定
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // 設定資料庫型別
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/chstar-blog")
                .setUsername("root")
                .setPassword("123456");

        //3. 策略設定
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true) //全域性大寫命名
                .setLogicDeleteFieldName("deleted")
                //NamingStrategy.underline_to_camel下劃線轉駝峰命名  user_id -> userId
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityTableFieldAnnotationEnable(true)//開啟實體註解
                .setRestControllerStyle(true)//rest風格controller
                .setChainModel(true)//鏈式setter
                .setSuperControllerClass(BaseController.class)//父類別
                .setSuperEntityClass(BaseEntity.class)//父類別
                .setControllerMappingHyphenStyle(true)//?
                .setTablePrefix("t_");//表名字首

        //4. 包名策略設定
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("cn.qxstar.biz_service")
                .setMapper("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .setController("controller")
                .setEntity("entity")
                .setXml("mapper.xml");

        //5. 整合設定
        AutoGenerator ag = new AutoGenerator();

        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig);

        //6. 執行
        ag.execute();
    }
}