springboot整合mybatis逆向工程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014155085/article/details/79095198

1.添加依赖

<!--mysql依赖-->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- SpringBoot - MyBatis -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.1</version>
</dependency>

<!-- SpringBoot - MyBatis 逆向工程 -->
<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.5</version>
</dependency>

2.添加插件

<!-- MyBatis 逆向工程 插件 -->
<plugin>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-maven-plugin</artifactId>
   <version>1.3.5</version>
   <configuration>
      <!--允许移动生成的文件 -->
      <verbose>true</verbose>
      <!-- 是否覆盖 -->
      <overwrite>true</overwrite>
   </configuration>

   <dependencies>
      <dependency>
         <groupId> mysql</groupId>
         <artifactId> mysql-connector-java</artifactId>
         <version>5.1.30</version>
      </dependency>
   </dependencies>
</plugin>

3.application配置mysql连接

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wechat
    username: root
    password: root

4.generatorConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--去掉注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--需要配置数据库连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://****:3306/competition?characterEncoding=utf-8&amp;useSSL=false"
                        userId="****"
                        password="****">
        </jdbcConnection>
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--指定entity生成的位置-->
        <javaModelGenerator targetPackage="com.example.competition.dao.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--sql映射文件生成的位置 mapper.xml-->
        <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--指定DaoMapper生成的位置 mapper.java-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.competition.dao.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--table是指定每个表的生成策略-->
        <!--<table tableName="department" domainObjectName="Department"></table>-->
        <!--<table tableName="group_teacher_rel" domainObjectName="Group_teacher_rel"></table>-->
        <!--<table tableName="groups" domainObjectName="Groups"></table>-->
        <!--<table tableName="specialty" domainObjectName="Specialty"></table>-->
        <!--<table tableName="student" domainObjectName="Student"></table>-->
        <!--<table tableName="teacher" domainObjectName="Teacher"></table>-->
        <table tableName="test" domainObjectName="Test"></table>
    </context>
</generatorConfiguration>
5.启动类Generator

public class Generator {
    public static void main(String[] args){
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        String genCfg = "/generatorConfig.xml";
        File configFile = new File(Generator.class.getResource(genCfg).getFile());
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}



PS:

1.springboot,mybatis 开启驼峰转换

#开启驼峰转换
mybatis.configuration.map-underscore-to-camel-case=true

猜你喜欢

转载自blog.csdn.net/u014155085/article/details/79095198