本系列目錄 http://www.reibang.com/p/22f3b28d974c
mybatis手動(dòng)創(chuàng)建實(shí)體谜疤,dao以及mapper配置文件的確浪費(fèi)時(shí)間佃延,而且容易出錯(cuò)。自動(dòng)化工具替我們省了很多時(shí)間夷磕,讓我們專注于業(yè)務(wù)層的實(shí)現(xiàn)履肃。
一、下載工具包
-
mybatis-generator-core-1.3.5
github被墻的的讀者可以自行g(shù)oogle or 百度去下載
- 數(shù)據(jù)庫驅(qū)動(dòng)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ū)動(dòng)包位置 -->
<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目錄
這個(gè)目錄下面就是生成我們model,dao以及mapper文件的地方碾局。上面三部分配置好后目錄結(jié)構(gòu)下圖所示
Paste_Image.png
四荆残、最激動(dòng) 的一步
在這一步將會生成model,dao以及mapper.xml文件净当。在命令行進(jìn)入上圖的目錄内斯,然后運(yùn)行下面的命令
java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite
運(yùn)行成功后,命令窗口出現(xiàn) MyBatis Generator finished successfully.
大功告成像啼,看看src目錄下面吧