MyBatis Generator工具用于将数据库中的表反转成对应的 Domain、XML文件 和 对应的 DAO Java文件
注 : 访问路径不能有中文字符
1> generator.xml 配置文件
<?
xml version
="1.0"
encoding
="UTF-8"
?>
<!DOCTYPE
generatorConfiguration
PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
<
generatorConfiguration
>
<!-- 数据库驱动包位置 -->
<!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
<
classPathEntry
location
="/Users/mew/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar"
/>
<!-- id:数据库方言 -->
<
context
id
="mysqlGen"
targetRuntime
="MyBatis3"
>
<
property
name
="javaFileEncoding"
value
="UTF-8"
/>
<
commentGenerator
>
<
property
name
="suppressDate"
value
="true"
/>
<!-- 是否去除自动生成的注释.true:是;false:否 -->
<
property
name
="suppressAllComments"
value
="true"
/>
</
commentGenerator
>
<!-- 数据库链接URL、用户名、密码 -->
<
jdbcConnection
driverClass
="com.mysql.jdbc.Driver"
connectionURL
="jdbc:mysql://localhost:3306/sy"
userId
="sypro"
password
="sypro"
>
<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> -->
</
jdbcConnection
>
<!--
default false
Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC.
-->
<
javaTypeResolver
>
<
property
name
="forceBigDecimals"
value
="false"
/>
</
javaTypeResolver
>
<!--
生成模型的包名和位置
指定包名以及生成的目标地址(可以自定义地址,但是路径不存在不会自动创建 .使用MAVEN将默认生成在target目录下,会自动创建路径)
-->
<
javaModelGenerator
targetPackage
="sy.model"
targetProject
="D:\study\mybatis\src"
>
<
property
name
="enableSubPackages"
value
="true"
/>
<
property
name
="trimStrings"
value
="true"
/>
</
javaModelGenerator
>
<!--
生成的映射文件包名和位置
指定包名以及生成的目标地址(可以自定义地址,但是路径不存在不会自动创建 .使用MAVEN将默认生成在target目录下,会自动创建路径)
-->
<
sqlMapGenerator
targetPackage
="sy.mapping"
targetProject
="D:\study\mybatis\src"
>
<
property
name
="enableSubPackages"
value
="true"
/>
</
sqlMapGenerator
>
<!--
生成DAO的包名和位置
指定包名以及生成的目标地址(可以自定义地址,但是路径不存在不会自动创建 .使用MAVEN将默认生成在target目录下,会自动创建路径)
-->
<
javaClientGenerator
type
="XMLMAPPER"
targetPackage
="sy.dao"
targetProject
="D:\study\mybatis\src"
>
<
property
name
="enableSubPackages"
value
="true"
/>
</
javaClientGenerator
>
<!-- 要生成那些表(更改tableName和domainObjectName就可以,对于后面几个属性可以防止产生其他不需要的配置) -->
<
table
tableName
="tbug"
domainObjectName
="Bug"
enableCountByExample
="false"
enableUpdateByExample
="false"
enableDeleteByExample
="false"
enableSelectByExample
="false"
selectByExampleQueryId
="false"
/>
<
table
tableName
="tmenu"
domainObjectName
="Menu"
enableCountByExample
="false"
enableUpdateByExample
="false"
enableDeleteByExample
="false"
enableSelectByExample
="false"
selectByExampleQueryId
="false"
/>
<
table
tableName
="tonline"
domainObjectName
="Online"
enableCountByExample
="false"
enableUpdateByExample
="false"
enableDeleteByExample
="false"
enableSelectByExample
="false"
selectByExampleQueryId
="false"
/>
</
context
>
</
generatorConfiguration
>
2> 生成对应的 xml 和 Java文件
方法一 : 使用 java指令
进入到含有 mybatis-generator-core-1.3.2.jar 和 generator.xml 的文件夹,使用指令
$> java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
方法二 : 使用 mybatis-generator-maven-plugin 插件生成
在 maven 工程的 pom.xml 中添加
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<build>
<finalName>mybatis_generator</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
</plugins>
</build>
安装成功后,将 generator.xml 放入 resources目录中,此时在 maven 的插件中找到 mybatis-generator插件,双击就会在 target 文件夹下产生需要的文件