SpringBoot-mysql-mybatis 逆向工程

646 阅读1分钟

SpringBoot-mysql-mybatis 逆向工程

  1. 使用 sql 语句创建数据库及表

  2. 创建 springboot 项目,使用 maven 仓库

  3. 配置 maven 文件(MySQL、Mybatis、alibaba-druid、mybatis-generator插件)

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.6</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>shoppingG</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>shoppingG</name>
        <description>shoppingG</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.25</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
            <!--mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.7</version>
            </dependency>
            <!--阿里巴巴连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.8</version>
            </dependency>
        </dependencies>
    ​
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    ​
                <plugin>
                    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <configuration>
                        <!--配置文件的位置-->
                        <configurationFile>src\main\resources\generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <executions>
                        <execution>
                            <id>Generate MyBatis Artifacts</id>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.4.0</version>
                        </dependency>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>8.0.25</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
    ​
        </build></project>
  4. 配置数据库连接(application.properties)

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=password
    spring.datasource.url=jdbc:mysql://localhost:3306/shoppingms?useSSL=false&amp
    
  5. 配置maven逆向工程文件(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>
        <!--加载资源文件-->
        <properties resource="application.properties"></properties>
        <context id="testTables" targetRuntime="MyBatis3">
            <commentGenerator>
                <!--是否去除自动生成的注释 true是:false 否-->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
            <!--数据库连接-->
            <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                            connectionURL="${spring.datasource.url}"
                            userId="${spring.datasource.username}"
                            password="${spring.datasource.password}"></jdbcConnection>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            <!--targetPackage目标包,生成实体类的位置-->
            <javaModelGenerator targetPackage="com.G.shoppingg.entity" targetProject="src/main/java">
                <!--enableSubPackages,是否让schema作为包的后缀-->
                <property name="enableSubPackages" value="false"/>
                <!--从数据库返回的值被清除前后空格-->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!--targetProject:mapper映射文件生成的位置-->
            <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
                <property name="enableSubPackages" value="false"></property>
    ​
            </sqlMapGenerator>
            <!--targetPackage:mapper接口生成的位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.G.shoppingg.dao" targetProject="src/main/java">
                <property name="enableSubPackages" value="false"/>
            </javaClientGenerator>
            <!--指定数据库表,要和数据库中进行对应,否则将会出错-->
            <table tableName="GOODS"  domainObjectName="GOODS"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"></table>
            <table tableName="GSALES"  domainObjectName="GSALES"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"></table>
            <table tableName="SALESMAN"  domainObjectName="SALESMAN"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"></table>
        </context>
    </generatorConfiguration>
    

    注:IDEA:mybatis.org/dtd/mybatis… 文件识别不到,需要下载文件并引入。

    1. 先去这个mybatis.org/dtd/mybatis…网址下载一个dtd

    2. 在IDEA中引入文件,

      路径:setting -> Languages & Frameworks -> Schemas and DTDS

      右上角引入添加刚刚下载的文件

image.png 0. 配置 mybatis 运行命令

创建 Maven 命令

在命令行中添加命令:mybatis-generator:generate

image.png

  1. 运行 Mybatis 命令