本系列目錄 http://www.itdecent.cn/p/22f3b28d974c
mybatis手動創(chuàng)建實體,dao以及mapper配置文件的確浪費時間,而且容易出錯。自動化工具替我們省了很多時間,讓我們專注于業(yè)務(wù)層的實現(xiàn)。
一、下載工具包
-
mybatis-generator-core-1.3.5
github被墻的的讀者可以自行g(shù)oogle or 百度去下載 - 數(shù)據(jù)庫驅(qū)動jar包
我這里是mysql數(shù)據(jù)庫,mysql-connector-java-5.1.21.jar
二、配置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>
<!-- 數(shù)據(jù)庫驅(qū)動包位置 -->
<classPathEntry location="e:\generator\mysql-connector-java-5.1.21.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 數(shù)據(jù)庫鏈接URL、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tale?characterEncoding=utf8" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.albert.domain.table" targetProject="e:\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="com.albert.dao" targetProject="e:\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.albert.dao" targetProject="e:\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="t_contents" domainObjectName="Contents" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="t_attach" domainObjectName="Attach" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
三、創(chuàng)建src目錄
這個目錄下面就是生成我們model,dao以及mapper文件的地方。上面三部分配置好后目錄結(jié)構(gòu)下圖所示

Paste_Image.png
四、最激動 的一步
在這一步將會生成model,dao以及mapper.xml文件。在命令行進入上圖的目錄,然后運行下面的命令
java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite
運行成功后,命令窗口出現(xiàn) MyBatis Generator finished successfully.大功告成,看看src目錄下面吧