mybatis自动生成代码

mybatis自动生成代码有三种方式:命令行、eclipse插件、maven插件。在这里主要介绍比较方便使用的一种方式–maven插件,它可以在eclipse、idea中通用。
在pom.xml文件中配置mybatis-generator插件:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
        <!-- 配置文件的位置 -->
        <configurationFile>generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

逆向生成代码需要的配置文件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>

    <!-- 指定连接数据库的JDBC驱动,指定到本机的完整路径(可以指定到maven仓库中的jar路径) -->
    <classPathEntry
            location="D:/Workspaces/maven_repos/mysql/mysql-connector-java/5.1.8/mysql-connector-java-5.1.8.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用mybatis3的版本 -->
    <context id="my" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文版的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/springdb" userId="root"
                        password="123456"/>

        <!-- 生成model类,targetPackage指定model类的包名,targetProject指定生成的model放在哪个工程中 -->
        <javaModelGenerator targetPackage="com.cui.springboot.model"
                            targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mybatis的mapper.xml文件,targetPackage指定mapper.xml的包名,targetProject指定mapper.xml在哪个工程目录下 -->
        <sqlMapGenerator targetPackage="com.cui.springboot.mapper"
                         targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mybatis的Mapper接口类文件,targetPackage指定Mapper接口类文件的包名,targetProject指定Mapper接口类在哪个工程目录下 -->
        <javaClientGenerator targetPackage="com.cui.springboot.mapper"
                             targetProject="D:/Workspaces/IdeaProjects/springboot-mybatis/src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>-->
        <!-- 数据库表以及对应的java模型类名(如果是多个表的话,可以复制多份然后修改成对应的表以及需要生成的模型名) -->
        <table tableName="student" domainObjectName="Student"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>

    </context>
</generatorConfiguration>

在上面的配置文件中已经将注释给出来了,需要注意的有以下几点:

  1. 在table标签中的tableName和domainObjectName是必须的,分别对应着数据库表名和实体类类名。其余的配置参数可以删除,一般默认是false。
  2. 创建数据表示时,字段名称可以使用下划线“_”将多个单词进行分割,比如:REC_ID这样的生成的实体类的属性是驼峰型,这样子比较好看一点。
  3. oracle中,数值形的字段,如果指定精度,比如Number(12,2),默认生成entity属性是BigDecimal型 ,如果不指定精度,比如:Number(9),指默认生成的是Long型
  4. oracle中的nvarchar/nvarchar2,mybatis-generator会识别成Object型,建议不要用nvarchar2,改用varchar2

弄完以上的步骤之后,就可以进行生成代码操作,如果idea的话,可以通过以下进行生成:
mybatis自动生成代码操作
找到mybatis-generator:generate,然后双击操作,即可生成代码。
生成的目录结构如下:
自动生成的目录结构

还可以通过以下的方式进行执行mybatis-generator:
  在该插件的目录下,按住shift键,选择“在此处打开命令窗口”选项,在弹出的窗口中输入以下的命令,然后回车(注意:需要将配置文件generatorConfig.xml文件也复制到该目录下):

java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值