计算机系统应用教程网站

网站首页 > 技术文章 正文

使用 MyBatis-Generator 自动生成代码

btikc 2024-09-29 09:58:51 技术文章 13 ℃ 0 评论

前言

Mybatis-generator 可以生成关于 mybatis 的模板代码。在数据库表复杂的情况下,使用 mybatis-generator 生成 model 等工程必要类无疑是极其方便的。

生成的内容

最简单的:Model类、Dao接口、基于主键的基础查询mapper语句。

具体步骤

1、添加Maven依赖

mybatis-generator 其实是一个开发插件,运行插件就可以生成模板代码。

 			<plugin>
                <!--自动生成的文件只限定于数据库表,对于复杂查询和Query不应该写在自动生成代码中-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.39</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 自动生成的配置 -->
                    <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>

2、mybatis-generator

虽然看着比较长,但是比较简单,该有的配置项都在这里了。

<?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">
        <!--生成POJO时是否自动生成注释-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="xxxxxxxx"
                        userId="xxxx" password="xxxx">
        </jdbcConnection>
        <!--数据库BigDecimals字段在java中定义-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.xxxxxx.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件mapper.xml存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/xxxxx">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类mapper.java存放位置-->
        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
                type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxxxx.dao.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名
            domainObjectName 是生成的model类名-->
        <table tableName="xxxxx"
               domainObjectName="xxxxxx"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--添加属性useActualColumnNames为true,那么生成的对象字段就跟表一样-->
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

3、通过运行maven插件,生成模板文件

在 mybatis-generator.xml 文件中,右键单击,按图所示,找到 generator 插件,点击运行。

之后,程序就会在配置文件配置的地方,自动生成我们所需要的模板代码了。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表