利用MyBatis-Generator自动生成代码
一、MyBatis-Generator介绍MyBatis-Generator是一个MyBatis相关代码的自动生成工具,使用MyBatis-Generator可以自动生成Dao层代码、Model层代码、Mapping SQL映射文件。
二、安装MyBatis-Generator插件
网上下载mybatis generator eclipse插件,安装方式跟其它Eclipse插件安装方式一样,这里我就不多说了,详情请见:http://xieke90.iyunv.com/blog/2151568
三、配置自动生成代码所需的xml文件
示例:generatorConfig.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>
<!-- 配置数据库驱动 location:数据库驱动路径 -->
<classPathEntry
location="G:\workspace\javaworkspace\MyBatis_Generator_Test\lib\mysql-connector-java-5.1.18.jar" />
<context id="mbgtest">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 配置数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
password="999999" />
<!-- 配置生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.xieke.test.model"
targetProject="MyBatis_Generator_Test/src" />
<!-- 配置生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.xieke.test.mapper"
targetProject="MyBatis_Generator_Test/src" />
<!-- 配置生成DAO的包名和位置 -->
<javaClientGenerator targetPackage="com.xieke.test.mapper"
targetProject="MyBatis_Generator_Test/src" type="XMLMAPPER" />
<!-- 配置需要生成的表 -->
<table schema="test" tableName="orders" domainObjectName="Orders">
<!-- 使用从数据库元数据获取的列名作为生成的实体对象的属性 -->
<property name="useActualColumnNames" value="true" />
<!-- 指定自动生成主键 -->
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
<table schema="test" tableName="orderItem" domainObjectName="OrderItem">
<property name="useActualColumnNames" value="true" />
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
选中generatorConfig.xml文件【右键】,选择【Generate MyBatis/iBATIS Artifacts】就可以生成相应代码了。
转载请注明出处:http://xieke90.iyunv.com/blog/2240577
页:
[1]