您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)基于mybatis逆向工程的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
使用mybatis生成逆向工程的詳細(xì)步驟,我個(gè)人感覺這個(gè)是最簡(jiǎn)單的一個(gè)了,雖然網(wǎng)上有很多種的方法來生成逆向工程,可是這個(gè)方法最簡(jiǎn)單。在這里我是使用maven搭建的環(huán)境,但是在正常的環(huán)境下也是一樣的。
步驟:
1、創(chuàng)建一個(gè)genreatorConfig.xml文件,這個(gè)文件的名字可以任意。我創(chuàng)建的時(shí)候是將它放在了src/main/resources下,這個(gè)文件的內(nèi)容并不需要去記,只需要去網(wǎng)上找就可以了。我們要做的只是對(duì)配置文件當(dāng)中的一些部分做修改,修改成自己的數(shù)據(jù)就可以了。
<?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),最好不要有中文字符,不然會(huì)找不到--> <classPathEntry location="D:\MysqlJdbcconnector/mysql-connector-java-5.1.41-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數(shù)據(jù)庫鏈接地址賬號(hào)密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/pet_hospital" userId="root" password="112233"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model類存放位置--> <javaModelGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成DaoMapper類存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.qiafeng.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成對(duì)應(yīng)表及類名,需要記住的一點(diǎn)是逆向工程無法生成關(guān)聯(lián)關(guān)系,只能生成單表操作--> <table tableName="owners" domainObjectName="Owners" ></table> <table tableName="pets" domainObjectName="Pets" ></table> <table tableName="types" domainObjectName="Types" ></table> <table tableName="employee" domainObjectName="Employee" ></table> <table tableName="specialties" domainObjectName="Specialties" ></table> <table tableName="vets" domainObjectName="Vets" ></table> <table tableName="visits" domainObjectName="Visits" ></table> <table tableName="vet_specialties" domainObjectName="VetSpecialties" ></table> </context> </generatorConfiguration>
2、導(dǎo)入相應(yīng)的jar包,mybatis-generator-core這個(gè)包和mysql-connector-java這個(gè)包
3、創(chuàng)建一個(gè)測(cè)試類,然后運(yùn)行下面代碼就可以了。
public static void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //項(xiàng)目根路徑不要有中文,我的有中文,所以使用絕對(duì)路徑 File configFile = new File("src/main/resources/genreatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) { try { generator(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
感謝各位的閱讀!關(guān)于“基于mybatis逆向工程的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。