转自:http://www.cuiyongzhi.com/post/36.html
MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比较好,可以自由组合完成比较复杂的查询,当然更复杂的就需要手动写了,下面整理下基本使用!
①MBG的下载
我这里分享的是我经常使用的版本,百度云下载地址:http://pan.baidu.com/s/1dEvCThv 下载成功后的大致目录结构为:
②打开目录中的generator.xml文件,其中部分需要修改的地方我都有备注说明,我这里给出的示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<? 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 > <!-- 数据库驱动包位置 --> < classPathEntry location = "F:generatormysql.jar" /> <!--<classPathEntry location="C:oracleproduct10.2.0db_1jdbclibojdbc14.jar" />--> < context id = "DB2Tables" targetRuntime = "MyBatis3" > < commentGenerator > < property name = "suppressAllComments" value = "true" /> </ commentGenerator > <!-- mysql数据库链接URL、用户名、密码 --> < jdbcConnection driverClass = "com.mysql.jdbc.Driver" connectionURL = "jdbc:mysql://localhost:3306/wechat" userId = "root" password = "123456789" > <!--Oracle数据库使用 --> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">--> </ jdbcConnection > < javaTypeResolver > < property name = "forceBigDecimals" value = "false" /> </ javaTypeResolver > <!-- 生成模型的包名和位置 --> < javaModelGenerator targetPackage = "com.cuiyongzhi.web.pojo" targetProject = "F:generatorsrc" > < property name = "enableSubPackages" value = "true" /> < property name = "trimStrings" value = "true" /> </ javaModelGenerator > <!-- 生成的映射文件包名和位置 --> < sqlMapGenerator targetPackage = "com.cuiyongzhi.web.mapping" targetProject = "F:generatorsrc" > < property name = "enableSubPackages" value = "true" /> </ sqlMapGenerator > <!-- 生成DAO的包名和位置 --> < javaClientGenerator type = "XMLMAPPER" targetPackage = "com.cuiyongzhi.web.dao" targetProject = "F:generatorsrc" > < property name = "enableSubPackages" value = "true" /> </ javaClientGenerator > <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> < table tableName = "t_wechatconfig" domainObjectName = "WechatConfig" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false" /> </ context > </ generatorConfiguration > |
这里的mysql的依赖包需要定位到自己的jar包目录,数据库账号密码、数据库选择、导出的位置、数据库表等设置成自己的即可!
③生成自己需要的文件的操作只需要到文件目录空白处按住【shift】然后邮件鼠标右击选择【在此处打开命令窗口】,然后复制【生成语句.txt】中的命令道cmd命令行,【回车】执行即可,然后我们就可以在【src】里面找到我们设置的目录和文件(下图)!