MyBatis Generator Mybatis代碼生成介紹

Mybatis官方提供了代碼生成工具,這裏是官方網站: http://mybatis.github.io/generator/index.html

可以自動生成 Java POJOs, Mapper.xml, Mapper.java。

1、下載 mybatis-generator-core-1.3.2.jar  google上有

2、下載 myeclipse插件 https://github.com/mybatis/generator/tree/master/eclipse/UpdateSite

3、下載configure.xml 配置文件 http://mybatis.github.io/generator/configreference/xmlconfig.html

 

下載完成後先安裝MyEclipse插件,安裝好以後新建一個項目,然後mybatis-generator-core-1.3.2.jar 扔到lib裏面去, 然後將configure.xml放到src下面。

複製代碼
<?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>

    <!-- 根據不同數據庫選擇驅動   注意路徑需要改成你自己的路徑   -->
    <!--<classPathEntry location="D:\Workspaces\MyEclipse 10\CodeGenerator\WebRoot\WEB-INF\lib\ojdbc14.jar" /> -->
    <classPathEntry location="E:\Workspaces\MyEclipse10\CodeGenerator\WebRoot\WEB-INF\lib\jtds-1.2.jar" />
    
    <!-- id隨便起沒什麼意義 -->
    <context id="contextOne" targetRuntime="MyBatis3" defaultModelType="conditional">
        
        <!-- 控制生成註釋 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
        
        <!-- ORACLE 的連接串
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@192.168.1.24:1521:ORCL" userId="dloa"
            password="dloa">
        </jdbcConnection>
        -->
        
        <!-- MSSQL2008 -->
        <jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver"
            connectionURL="jdbc:jtds:sqlserver://124.95.165.217/ZhengFuBan" userId="sa"
            password="sql2008!@#">
        </jdbcConnection> 
        
        <javaTypeResolver type="com.code.generator.Convert" />

        <!-- 實體類  (targetPackage="com.framework.code.domain"這裏是生成的包名,要和程序實際包名一致) -->
        <javaModelGenerator targetPackage="com.framework.code.domain"
            targetProject="CodeGenerator">
        </javaModelGenerator>

        <!-- mapper.xml (targetPackage="com.framework.code.domain"這裏是生成的包名,要和程序實際包名一致) -->
        <sqlMapGenerator targetPackage="com.framework.code.mapper"
            targetProject="CodeGenerator" />

        <!-- mapper.java (targetPackage="com.framework.code.domain"這裏是生成的包名,要和程序實際包名一致) -->
        <javaClientGenerator targetPackage="com.framework.code.mapper"
            targetProject="CodeGenerator" type="XMLMAPPER" />
            
        <!--   tableName="你要生成的數據庫表名" -->
        <table schema="" tableName="TEST"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false"
        ></table>
        
    </context>
</generatorConfiguration>
複製代碼

然後在配置文件上右鍵-運行就可以了。

 

安裝好插件就帶這個右鍵功能了,然後就可以生成代碼了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章