【MyBatis】使用Eclipse插件的方式运行代码生成器MBG(MyBatis Generator)

一、MyBatis Generator运行方式

MyBatis Generator有以下几种运行方式:

a、Java代码

b、命令提示行

c、Maven插件

d、Eclipse插件

其中只有Eclipse插件支持代码合并,可以生成新代码的同时保留自己添加的代码。

二、Eclipse插件使用步骤

a、安装

下载插件: https:/github.com/mybatis/generator/releases

b、Eclipse中安装插件

 选择下载的压缩包,然后一直下一步到完成,然后重启Eclipse,即完成了安装。

三、使用MBG插件

添加配置文件

src/main/resources下添加一个文件夹mybatis-generator,添加一个配置文件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>
    <classPathEntry location="C:\Softs\maven\Repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar"/>
    
    <context id="SqlServerContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="javaFileEncoding" value="UTF-8" />
        <property name="autoDelimitKeywords" value="true" />
        <property name="beginningDelimiter " value="[" />
        <property name="endingDelimiter " value="]" />

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>
        
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                        connectionURL="jdbc:sqlserver://127.0.0.1;database=db"
                        userId="sa"
                        password="psw">
        </jdbcConnection>
        
        <javaModelGenerator targetPackage="com.ltsolution.framework.bs.system.model" targetProject="framework\src\main\java">
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mybatis.mapper"  targetProject="framework\src\main\resources"/>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ltsolution.framework.bs.system.mapper"  targetProject="framework\src\main\java"/>
        
        <table tableName="Company">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
        </table>
        <table tableName="Customer">
            <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/>
            <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride>
        </table>
        <table tableName="CustomerType">
            <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/>
            <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride>
        </table>
        <table tableName="Product">
            <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/>
            <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride>
        </table>
        <table tableName="ProductType">
            <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/>
            <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride>
        </table>
        <table tableName="SaleShipmentDetail">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
        </table>
        <table tableName="SaleShipmentHead">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
            <ignoreColumn column="RowId" />
        </table>
        <table tableName="Storage">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
        </table>
        <table tableName="HeadNoPrefix">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
            <ignoreColumn column="RowId" />
        </table>
        <table tableName="StorageQuantity">
            <generatedKey column="RowId" sqlStatement="SQLServer" identity="true"/>
            <ignoreColumn column="RowId" />
        </table>
    </context>
</generatorConfiguration>
View Code

主要修改几个地方

classPathEntry:设置数据库驱动的jar包位置

context-jdbcConnection:设置数据库信息

context-javaModelGenerator:设置model的生成位置

context-sqlMapGenerator:设置mapper.xml文件的生成位置

context-javaClientGenerator:设置mapper接口的生成位置

关于在SQL中使用的几个注意点,可以查看https://www.cnblogs.com/LiveYourLife/p/9000874.html这篇文章

其它的自行查阅详细资料

启动生成

即可看到生成的结果

猜你喜欢

转载自www.cnblogs.com/LiveYourLife/p/9190222.html