欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IDEA Springboot 整合Mybatis 完整教程系列 02

程序员文章站 2022-05-08 11:06:20
...

02  配置mybatis,利用自动生成插件,生产与数据库表对应的

      mapper接口,和xml文件


(1)设置pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>


        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>



    </dependencies>

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




            <!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--generator配置文件的位置-->
                    <configurationFile>${basedir}/src/main/resources/mybatis-generator-config.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>

        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>


</project>


值得注意的是,下图指定的是mybatis-generator-config的配置文件路径

IDEA Springboot 整合Mybatis 完整教程系列 02


还有,如下,加入目录下的所有配置文件,防止遗漏

IDEA Springboot 整合Mybatis 完整教程系列 02


(2)mybatis-generator-config.xml

其中,生成entity类,和对应的路径如下

IDEA Springboot 整合Mybatis 完整教程系列 02


生成mapper接口,和对应的路径如下

IDEA Springboot 整合Mybatis 完整教程系列 02

生成mapper.xml,和对应的路径如下

IDEA Springboot 整合Mybatis 完整教程系列 02



完整的mybatis-generator-config.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>
    <!--mysql 连接数据库jar 这里选择自己本地位置-->
    <classPathEntry location="E:\jar\mysql-connector-java-5.1.0-bin.jar" />
    <context id="testTables" targetRuntime="MyBatis3">


        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/easyloan" userId="root"
                        password="122026">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
           NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成entity类的位置 -->
        <javaModelGenerator targetPackage="com.example.demo.entity"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置
           如果maven工程只是单独的一个工程,targetProject="src/main/java"
           若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:
           targetProject="ecps-manager-mapper",下同-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.demo.mapper"
                             targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>


        <!-- 指定数据库表 -->

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 schema 重复代码生成-->
        <table schema="" tableName="user" >
            <!--设置自增字段-->
            <generatedKey  column="user_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="department" >
        </table>

        <table schema="" tableName="audit" >
            <!--设置自增字段-->
            <generatedKey  column="aud_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="employee" >
            <!--设置自增字段-->
            <generatedKey  column="emp_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="expected_repay" >
            <!--设置自增字段-->
            <generatedKey  column="exp_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="linkman" >
        </table>

        <table schema="" tableName="loan_apply" >
            <!--设置自增字段-->
            <generatedKey  column="loan_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="manager" >
            <!--设置自增字段-->
            <generatedKey  column="m_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="manage" >
        </table>

        <table schema="" tableName="overdue_times" >
            <!--设置自增字段-->
            <generatedKey  column="overdue_id"  identity="false" sqlStatement="JDBC"/>
        </table>

        <table schema="" tableName="real_repay" >
            <!--设置自增字段-->
            <generatedKey  column="real_id"  identity="false" sqlStatement="JDBC"/>

        </table>

        //数据库对应的表
        <table schema="" tableName="user_login" >

        </table>

    </context>
</generatorConfiguration>

(3)mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

   <settings>
       <setting name="useGeneratedKeys" value="true"/>
       <setting name="useColumnLabel" value="true"/>
       <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>
</configuration>


上述的文件以及对应的包目录建立好之后,就可以使用mybatis generator 插件自动生成代码了

在IDEA软件界面中,有如下,点击一下generator,即可自动生成代码。

如果找不到这个自带插件,请按如下链接操作,手动配置一个插件。

mybatis-generator自动生成

IDEA Springboot 整合Mybatis 完整教程系列 02





相关标签: mybatis generator