1. 程式人生 > >springboot-mycat-mybatis-plus

springboot-mycat-mybatis-plus

  1. pom座標
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId
>
mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId
>
<version>2.2.0</version> </dependency> <!-- Code generator test sample--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>
1.7</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency>
  1. application.yml連線mycat
spring:
  datasource:
    druid:
      url: jdbc:mysql://192.168.0.184:8066/TESTDB?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      driver-class-name: com.mysql.jdbc.Driver

#mybatis
mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml
  global-config:
    id-type: 2
    field-strategy: 2
    refresh-mapper: true
    logic-delete-value: 0
    logic-not-delete-value: 1
    sql-parser-cache: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
  1. 編寫MpGenerator類,使用mybatis-plus自動生成entity,dao,seriver.controller層
public class MpGenerator {

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 選擇 freemarker 引擎,預設 Veloctiy
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全域性配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(new File("").getAbsolutePath()+"/src/main/java/");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的請改為false
        gc.setEnableCache(false);// XML 二級快取
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("liuxing");

        // 自定義檔案命名,注意 %s 會自動填充表實體屬性!
        gc.setMapperName("%sDao");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 資料來源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定義資料庫表字段型別轉換【可選】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("轉換型別:" + fieldType);
                // 注意!!processTypeConvert 存在預設型別轉換,如果不是你要的效果請自定義返回、非如下直接返回。
                return super.processTypeConvert(fieldType);
            }
        });
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setUrl("jdbc:mysql://192.168.0.184:8066/TESTDB?characterEncoding=utf8");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全域性大寫命名 ORACLE 注意
        //strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此處可以修改為您的表字首
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setEntityBuilderModel(true);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.haiyu");
        mpg.setPackageInfo(pc);

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

    }

}