mybatis generator

啦啦啦嗨嗨嗨發表於2018-08-11

由於第一次寫,不知道怎麼玩這個格式

匯入依賴:

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.5</version>
</dependency>

然後建立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="D:\apache-maven-3.3.9\repository\mysql\mysql-connector-java\5.1.41/mysql-connector-java-5.1.41.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:3306/manage"
                userId="root"
                password="">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 指定javaBean生成的位置 -->
        <javaModelGenerator targetPackage="com.zf.entity"
                            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--指定sql對映檔案生成的位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 指定dao介面生成的位置,mapper介面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.zf.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!-- table指定每個表的生成策略 -->
                        <!--資料庫表明               實體-->
        <table tableName="advertisement" domainObjectName="advertisement"></table>
        <table tableName="feedback" domainObjectName="feedback"></table>
        <table tableName="feedback_picture" domainObjectName="feedback_picture"></table>
        <table tableName="flicker_picture" domainObjectName="flicker_picture"></table>
        <table tableName="flicker_screen" domainObjectName="flicker_screen"></table>
        <table tableName="immediate_message" domainObjectName="immediate_message"></table>
        <table tableName="notice" domainObjectName="notice"></table>
        <table tableName="poster" domainObjectName="poster"></table>
        <table tableName="union_lecture_hell" domainObjectName="union_lecture_hell"></table>
        <table tableName="update_package" domainObjectName="update_package"></table>
    </context>
</generatorConfiguration>

啟動終端,或者穿件一個java類main方法啟動

我用的是java類:

public class generator {
   public static void main(String[] args) throws Exception{
      List<String> warnings = new ArrayList<String>();
      boolean overwrite = true;
      File configFile = new File("generatorConfig.xml");    // 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);
   }

相關文章