您的位置:首页 > 编程语言

MyBatis使用Generator自动生成代码

2013-07-02 12:23 645 查看
MyBatis中,可以使用Generator自动生成代码,包括DAO层、 MODEL层 、MAPPING SQL映射文件。

第一步:
配置好自动生成代码所需的XML配置文件,例如(generator.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>
<!-- classPathEntry:数据库的JDBC驱动-->
<classPathEntry location="F:\Jansun\workspace\wicketAjax\ojdbc14-10.2.0.1.jar" />

<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>

<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@10.87.61.233:1521:orcl11g"
userId="ibis"
password="ibis">
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="com.test.model" targetProject="F:\Jansun\workspace\wicketAjax\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<sqlMapGenerator targetPackage="com.test.mapping" targetProject="F:\Jansun\workspace\wicketAjax\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="F:\Jansun\workspace\wicketAjax\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<table schema="ibis" tableName="ibis_info" domainObjectName="Ibis" >

</table>

</context>

</generatorConfiguration>

第二步:下载mybatis-generator-core-1.3.1.jar包。

第三步:进入XML配置文件(generator.xml)所在的的目录并执行命令:

java -jar F:\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar -configfile genrator.xml -overwrite

(PS:F:\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar,是你所下载的mybatis-generator-core-1.3.1.jar包所在的目录)
//--------------------------------------------------------------------------------------

Mybatis-generator-------------Mybatis的代码生成器

使用Hibernate时, 可以很方便的生成model,dao,和映射配置文件.在mybatis里, 也有生成器,即mybatis generator, 简称MBG. 下面为大家介绍一下MBG的使用.

Mybatis-generator的思想是编写一个配置文件,然后运行该配置文件,就会得到相应的java代码。那么重点就是这个配置文件,配置文件中都需要些什么呢?必须包括的有数据库的datasource,Mybatis的版本信息,至少一个的数据表,还有是需要生成的代码所在的包的信息。

1.下载类包

下载mybatis-generator-core-1.3.1-bundle.zip之后, 解压得到mybatis-generator-core-1.3.1.jar, 即生成器的jar包, 将mybatis-3.0.6.jar和mybatis-generator-core-1.3.1.jar加入到项目的lib里, 然后编写一个生成器的配置文件generatorConfig.xml.

2. mybatis-generator的配置文件

mybatis-generator最重要的就是一个配置文件,配置文件中配置了生成的.dao 与pojo生成的package路径等。

介绍一下genernatorConfig.xml



?

其中的properties元素里引用了一个generatorConfig.properties配置文件是为了方便移植的时候,只需修改resource的路径值和generatorConfig.properties里的值即进行生成操作.

generatorConfig.properties代码如下:

Properties代码



?
其中, targetProject是工程的源代码存放位置

classPath是数据库驱动包的位置, modelPackage是存放model实体的包, 对应数据库表, sqlMapperPackage是存放实体映射的XML配置文件, daoMapperPackage是存放Mapper接口的地方.

注意: 要事先建立这些包才可以生成.

如果不想生成mybatis的注释信息, 可以将commentGenerator里的suppressAllComments的值设为true,

table元素对应数据库表.



<table schema="minghan" tableName="sys_admin" domainObjectName="Admin"

enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false"

selectByExampleQueryId="false">

</table>

schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 就不会生成对应的Example类了.

3.运行

配置文件配好了之后,进行生成代码, 既可以使用命令的方式, 也可以自已写一个带main的类来运行, 下面提供了一个带main的运行类

MyBatisGeneratorTool.java

Java代码



?
运行此类即可生成对应于数据库表的实体类, Mapper配置文件, Mapper接口, 生成后可以测试其可用性.

使用命令的方式运行:

java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite

4.其他学习网址推荐

推荐一个比较好的mybatis-generator的教学网址:

/article/5262897.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: