標籤:ati jar ble eclipse xml配置 mysql 持久層 drive
Mybatis和Hibernate都是持久層架構,MyBatis出現的比Hibernate晚,這兩種架構我都用過,對於二者的優勢我的感觸不深,個人感覺MyBatis自動產生model,Mapping,mapper檔案的功能使編碼量減少,但也很容易出錯,出錯後還不易排查。
我在網上搜尋了一下關於Mybatis和Hibernate的比較,知乎上的這個文章講得比較詳細,大家可以參考一下
https://www.zhihu.com/question/21104468
想要自動組建檔案,首先要下載MyBatis Generator Release,https://github.com/mybatis/generator/releases
產生的檔案可以直接放在工程下面的正確的包目錄下,但個人建議還是先建立一個檔案夾,產生後再把對應的檔案拷進不同的包路徑下。
我使用的是Mysql資料庫,這裡還要下載一個用於串連資料庫的jar包,官方網址:http://www.mysql.com/products/connector/
這裡關鍵是配置generatorConfig.xml設定檔,我在src/main/resources目錄下建立了generatorConfig.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> <!-- 用於資料庫連接的jar包 --> <classPathEntry location="D:/code/myeclipse/mybatisGenerator/mysql-connector-java-5.1.40-bin.jar"/> <context id="my" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 資料庫連接配置 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/book" userId="root" password="[email protected]"/> <javaModelGenerator targetPackage="com.ese.book.pojo" targetProject="D:/code/myeclipse/mybatisGenerator/model"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="com.ese.book.mapping" targetProject="D:/code/myeclipse/mybatisGenerator/mapping"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator targetPackage="com.ese.book.dao" targetProject="D:/code/myeclipse/mybatisGenerator/dao" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="books" domainObjectName="Book" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context></generatorConfiguration>
並把這個檔案拷貝到和上面兩個jar包同一路徑下。檔案夾內容為:
條件準備好後,便可以組建檔案了。產生語句:
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
註:這個語句要在目標檔案夾路徑下執行,先要通過命令列進入D:\code\myeclipse\mybatisGenerator檔案夾下。
執行後dao,mapping,model檔案夾下就會分別出現BookMapper.java,BookMapping.xml和Book.java檔案。具體這裡不在給出。
使用MybatisGenerator自動產生Model,Mapping和Mapper檔案