用mybatis的時候,手寫xml或model文件是一個力氣活,因此能夠用mybatis-gennerator插件自動生成mybatis所須要的dao、bean、mapper xml文件java
(原文地址:http://blog.csdn.net/tolcf/article/details/50835165)node
附件下載地址:http://files.cnblogs.com/files/cc-robot/generator.rarmysql
把附件解壓到本地,我放在d:\web\java目錄下了web
只須要修改下面xml配置文件中加下劃線的部分,sql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 3 <generatorConfiguration> 4 <!-- 數據庫驅動包位置 --> 5 <classPathEntry location="D:\web\java\generator\mysql-connector-java-5.1.34.jar" /> 6 <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />--> 7 <context id="DB2Tables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 <property name="suppressAllComments" value="true" /> 10 </commentGenerator> 11 <!-- 數據庫連接URL、用戶名、密碼 --> 12 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/dbname?characterEncoding=utf8" userId="dbuser" password="dbpass"> 13 <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">--> 14 </jdbcConnection> 15 <javaTypeResolver> 16 <property name="forceBigDecimals" value="false" /> 17 </javaTypeResolver> 18 <!-- 生成模型的包名和位置 --> 19 <javaModelGenerator targetPackage="com.callservice.model" targetProject="D:\web\java\generator\src"> 20 <property name="enableSubPackages" value="true" /> 21 <property name="trimStrings" value="true" /> 22 </javaModelGenerator> 23 <!-- 生成的映射文件包名和位置 --> 24 <sqlMapGenerator targetPackage="com.callservice.mapping" targetProject="D:\web\java\generator\src"> 25 <property name="enableSubPackages" value="true" /> 26 </sqlMapGenerator> 27 <!-- 生成DAO的包名和位置 --> 28 <javaClientGenerator type="XMLMAPPER" targetPackage="com.callservice.dao" targetProject="D:\web\java\generator\src"> 29 <property name="enableSubPackages" value="true" /> 30 </javaClientGenerator> 31 <!-- 要生成那些表(更改tableName和domainObjectName就能夠) --> 32 <table tableName="admin" domainObjectName="admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> 33 <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> 34 <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> --> 35 </context> 36 </generatorConfiguration>
而後windwos下打開命令行模式,切換到generator目錄下,執行:數據庫
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwritemybatis
如圖:oracle
出現successfully,證實生成成功了。app
這時再切換到generator/src目錄下,能夠看到剛纔生成的文件了,有三個文件夾:dao,mapping,model,把生成的文件複製到相應項目目錄下就能夠了。dom