首页 > 代码库 > 利用mybatis-generator生成po

利用mybatis-generator生成po

import java.io.File;
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.internal.DefaultShellCallback;



public class AutoCreateBean {
    public static void main(String[] args) {
         String path = AutoCreateBean.class.getResource("").toString();  
        if (path != null)  
            {  
                path = path.substring(6,path.length());//如果是windwos将5变成6  
                if(path.indexOf("%e9%83%ad%e7%ab%af%e7%ab%af") !=-1){
                    path = path.replace("%e9%83%ad%e7%ab%af%e7%ab%af", "郭端端");
                }
                if(path.indexOf("%20")!=-1){
                    path = path.replaceAll("%20", " ") ; //本地包含空格的注意替换
            }
                System.out.println("current path :" + path);  
            }
        try{
          List<String> warnings = new ArrayList<String>();  
          boolean overwrite = true;  
          File configFile = new File(path+"generator.xml");//获取项目路径,在generator.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); 
        }catch(Exception 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:数据库的JDBC驱动的jar包地址-->  
    <classPathEntry location="D:/repository/mysql/mysql-connector-java/5.1.13/mysql-connector-java-5.1.13-bin.jar" />
    <context id="openshopsqlSessionFactory" targetRuntime="MyBatis3">

        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://10.19.105.184:3306/wms" userId="ggs"
            password="ZPIOSVYLXMNI">
        </jdbcConnection>
     <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
<!--        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" -->
<!--             connectionURL="jdbc:oracle:thin:@10.19.105.113:1521:orcl" userId="MWS" -->
<!--             password="MWS2015"> -->
<!--         </jdbcConnection> -->

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
         <!-- targetProject:自动生成javaBean代码的位置 --> 
        <javaModelGenerator targetPackage="com.yunda.inter"
            targetProject="D:/workspace/">
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格  -->   
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        
        <!-- xml文件 自动生成mapper.xml代码位置-->
        <!-- enableSubPackages:是否让schema作为包的后缀 -->  
        <sqlMapGenerator targetPackage="com.yunda.inter"
            targetProject="D:/workspace/">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        
         <!-- 自动生成mapper接口代码位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.yunda.inter" 
             targetProject="D:/workspace/">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
         <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
         <!--如果想要生成一个demo,则enableCountByExample设为true  -->
        <table tableName="riv_owner_tx_type" domainObjectName="RivOwnerTxType" 
        enableCountByExample="false" 
        enableUpdateByExample="false" 
        enableDeleteByExample="false" 
        enableSelectByExample="false" 
        selectByExampleQueryId="false" />
    
    </context>
</generatorConfiguration> 

mybatis-generator-core-1.3.2.jar

利用mybatis-generator生成po