Introduction to 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:WorkspacesMyEclipse 10CodeGeneratorWebRootWEB-INFlibojdbc14.jar" /> -->
    <classPathEntry location="E:WorkspacesMyEclipse10CodeGeneratorWebRootWEB-INFlibjtds-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>

然后在配置文件上右键-运行就可以了。

安装好插件就带这个右键功能了,然后就可以生成代码了。

原文地址:https://www.cnblogs.com/daxin/p/4281137.html