Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1789|回复: 1
打印 上一主题 下一主题

使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2017-3-13 21:37:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。

1、相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包

需要一个配置文件:generatorConfig.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6.     <!--数据库驱动-->
  7.     <classPathEntry    location="mysql-connector-java-5.1.18.jar"/>
  8.     <context id="DB2Tables"    targetRuntime="MyBatis3">
  9.         <commentGenerator>
  10.             <property name="suppressDate" value="true"/>
  11.             <property name="suppressAllComments" value="true"/>
  12.         </commentGenerator>
  13.         <!--数据库链接地址账号密码-->
  14.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
  15.         </jdbcConnection>
  16.         <javaTypeResolver>
  17.             <property name="forceBigDecimals" value="false"/>
  18.         </javaTypeResolver>
  19.         <!--生成Model类存放位置-->
  20.         <javaModelGenerator targetPackage="lcw.model" targetProject="src">
  21.             <property name="enableSubPackages" value="true"/>
  22.             <property name="trimStrings" value="true"/>
  23.         </javaModelGenerator>
  24.         <!--生成映射文件存放位置-->
  25.         <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
  26.             <property name="enableSubPackages" value="true"/>
  27.         </sqlMapGenerator>
  28.         <!--生成Dao类存放位置-->
  29.         <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
  30.             <property name="enableSubPackages" value="true"/>
  31.         </javaClientGenerator>
  32.         <!--生成对应表及类名-->
  33.         <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  34.     </context>
  35. </generatorConfiguration>
复制代码


需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:
  1. <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
复制代码
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。
生成文件命令:

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可
  1. java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
复制代码
http://www.cnblogs.com/smileberry/p/4145872.html
回复

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
沙发
 楼主| 发表于 2017-8-2 10:32:46 | 只看该作者
对应表的字段 不要采用关键字,如 desc 等
否则会插入不进去
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-7 18:56 , Processed in 0.059231 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表