mybatis generator插件的使用

对于新项目中使用mybatis,通过使用mybatis generator插件自动生成相应的实体类和XXXmapper.xml文件,大大简化了开发。
1.添加maven 依赖

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
        <configurationFile>${basedir}/src/main/profile/dev/generatorConfig.xml</configurationFile>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
    </dependencies>
</plugin>

2、插件配置文件

配置文件存放路径如下图:
这里写图片描述
插件默认会读到src/main/resources目录下的generatorConfig.xml 文件。

具体配置如下范例:

1、指定连接数据库的URL,用户名和密码;
2、指定生成的实体所存放的目录

<javaModelGenerator targetPackage="com.gen.model" targetProject="src\main\java" />

3、sqlMapGenerator 需要设置包名,和存放映射文件的路径。用maven 管理,一般xml文件放在src/main/resources目录下。

<sqlMapGenerator targetPackage="gen_mappers" targetProject="src\main\resources" />

4、设置与sql映射文件对应的接口文件所存放的目录

<javaClientGenerator targetPackage="com.gen.mapper"
            targetProject="src\main\java" type="XMLMAPPER" />

5、配置你需要生成的数据库中的表名及实体名称

<table schema="test" tableName="test_table" enableCountByExample="false"
           enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"></table>

generatorConfig.xml配置文件(适用于mybatis接口模式)的完整配置如下:

<?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="context1" targetRuntime="MyBatis3Simple" defaultModelType="flat">
       <!-- 避免生成注解 -->
       <commentGenerator>
           <property name="suppressAllComments" value="true" />
       </commentGenerator>
       <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test"
           userId="mysql" password="123456" />
       <javaModelGenerator targetPackage="com.qianbao.gen.model" targetProject="src\main\java" />
       <sqlMapGenerator targetPackage="gen_mappers" targetProject="src\main\resources" />
       <javaClientGenerator targetPackage="com.qianbao.gen.mapper"
           targetProject="src\main\java" type="XMLMAPPER" />
       <table schema="qb_cf" tableName="rxb_big_account" enableCountByExample="false"
           enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"></table>
       <!-- <table schema="cf_qb" tableName="*" omainObjectName="PlanInfo" enableCountByExample="false"
           enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"></table> -->
   </context>
</generatorConfiguration>

三、生成代码
项目 右键–》run as –》 maven bulid –》弹出对话框 –》在goals中输入mybatis-generator:generate 或者 点击select –》选择你的mybatis插件 –》apply –》run
选择项目 按 F5 刷新项目 出现生成的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值