`

MyBatis 自动生成xml文件

阅读更多
package com.test.mybatis;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MybatisGeneratorUtil {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            System.out.println("start generator ...");
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File(MybatisGeneratorUtil.class.getResource(
                    "./generator.xml").getFile());
            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);
            System.out.println("end generator!");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (XMLParserException e) {
            e.printStackTrace();
        }
        catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}



配置文件:
<?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="E:\WorkSpace\webtest\web\WEB-INF\lib\ojdbc14-10.2.0.4.0.jar" />  
  
  <context id="Mysql2Tables" targetRuntime="MyBatis3">  
    <jdbcConnection driverClass="oracle.jdbc.OracleDriver"  
        connectionURL="jdbc:oracle:thin:@10.10.10.10:1521:oracletemp"  
        userId="devdb"  
        password="devdb">  
    </jdbcConnection>  
  
    <javaTypeResolver >  
      <property name="forceBigDecimals" value="false" />  
    </javaTypeResolver>  
  
    <javaModelGenerator targetPackage="com.mybatis" targetProject="./">  
      <property name="enableSubPackages" value="true" />  
      <property name="trimStrings" value="true" />  
    </javaModelGenerator>  
  
    <sqlMapGenerator targetPackage="com.mybatis"  targetProject="./">  
      <property name="enableSubPackages" value="true" />  
    </sqlMapGenerator>  
  
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis"  targetProject="./">  
      <property name="enableSubPackages" value="true" />  
    </javaClientGenerator>  
  
    <!-- table schema="oratcdb" Schema cannot be read. tableName="VT_DEBTCOLLECTIONPLAN" domainObjectName="DebtCollectionPlan" enableCountByExample="true" enableUpdateByExample="true"  
           enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">  
    </table -->  
    <table schema="oracledb" tableName="T_TABLE_NAME" domainObjectName="DebtCollectionPlan" enableCountByExample="true" enableUpdateByExample="true"  
           enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">  
    </table>  
  
  </context>  
</generatorConfiguration>  

系统pom.xml依赖
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <outputDirectory>${project.basedir}/target</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/${project.build.finalName}</path>
                    <uriEncoding>UTF-8</uriEncoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
            </plugin>
        </plugins>



输出目录: target\generated-test-sources

mybatis-generator:generate -e


<!-- http://www.mybatis.org/generator/configreference/xmlconfig.html -->
<!-- http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html -->
<?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>
    <!-- SqlServer数据库驱动jar -->
    <classPathEntry location="renxiaoyao\.m2\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar"/>

    <context id="DB2Tables" targetRuntime="Ibatis2Java5">
        <!--去除注释  -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接 -->
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                        connectionURL="jdbc:sqlserver://10.10.10.10:3433;DatabaseName=SchemaName" userId="DBAdmin"
                        password="DBAdmin!@#$%^"/>
 
        <!--默认false
           Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC.
         -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建  使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.xxx.entity" targetProject="MAVEN">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="com.xxx.mybatis" targetProject="MAVEN">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现  context id="DB2Tables" 修改targetRuntime="MyBatis3"  -->
        <javaClientGenerator type="SPRING" targetPackage="com.xxx.dao" targetProject="MAVEN">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等-->
        <table tableName="ApiType" domainObjectName="ApiType"
                       enableCountByExample="true" enableUpdateByExample="true"
                       enableDeleteByExample="true" enableSelectByExample="true"
                       selectByExampleQueryId="true">
                    <property name="useActualColumnNames" value="true" />
                </table>
</context>
        </generatorConfiguration>

  • 大小: 41.2 KB
  • 大小: 41.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics