Spring 相关文章

mybatis plus 代码生产器

2020-02-19  本文已影响0人  刘小刀tina

#设置数据源mysql
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://152.136.27.48:3306/d_tina?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456

mybatis-plus:
  #mybatisPlus mapper xml文件地址
  mapper-locations:  classpath*:mapper/*.xml
  # mybaits-plus type-aliases 文件地址
  type-aliases-package: com.example.mybatisplus.entity
  # 驼峰下划线转换
  global-config:
    db-column-underline: true
#    配置mybatis plus 打印sql语句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 8001


pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
        </dependency>

        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>


        <!--添加swagger2依赖的jar包-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


1. MpCodeGenerator

public class MpCodeGenerator {

    @Test
    public void run(){

        //1.创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //2.全局配置
        GlobalConfig gc = new GlobalConfig();

        //输出目录-->更该第1个地方
        gc.setOutputDir("/Users/lvxiaokai/Desktop/tina/idea-work/edu_online/edu_teacher/src/main/java/");

        //作者
        gc.setAuthor("tina");

        //生成后是否打开资源管理器
        gc.setOpen(false);

        //重新生成时文件是否覆盖
        gc.setFileOverride(false);

        //去掉Service接口的首字母I
        gc.setServiceName("%sService");

        //主键策略 ID_WORKER_STR对应String ID_WORKER对应Long
        gc.setIdType(IdType.ID_WORKER_STR);

        //定义生成的实体类中日期类型
        gc.setDateType(DateType.ONLY_DATE);

        //开启Swagger2模式
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        //3.数据源配置  -->更该第2个地方
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://152.136.27.48:3306/d_tina?useUnicode=true&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        //4.包配置 -->更该第3个地方
        PackageConfig pc = new PackageConfig();
        //模块名
        pc.setModuleName("tina");
        //启动类的包路径 如com.online.edu.service 需拆分parent:com.online.edu  module:service
        pc.setParent("com");

        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        //5.策略配置
        StrategyConfig strategy = new StrategyConfig();
        //对应的表名   -->更该第4个地方
        strategy.setInclude("t_teacher");
        strategy.setTablePrefix("t_");//去掉表明的前缀
        //数据库字段映射到实体类的转换策略 如下为驼峰式
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //开启lombok
        strategy.setEntityLombokModel(true);
        //restful开启
        strategy.setRestControllerStyle(true);
        //url中驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        //执行
        mpg.execute();
    }


}

上一篇 下一篇

猜你喜欢

热点阅读