maven自动生成mapper和实体类

maven自动生成mapper和实体类

pom.xml文件添加以下配置

<dependencies>
	<dependency>  <!-- 自动生成dao vo mapper文件所需依赖 -->
		<groupId>org.mybatis.generator</groupId>  
		<artifactId>mybatis-generator-core</artifactId>  
		<version>1.3.2</version>  
	</dependency>        
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
		
		<plugin>
		
			<!-- 自动生成dao vo mapper文件 -->
			<groupId>org.mybatis.generator</groupId>
               <artifactId>mybatis-generator-maven-plugin</artifactId>
               <version>1.3.5</version>
               <configuration>
                   <!--允许移动生成的文件 -->
                   <verbose>true</verbose>
                   <!--允许覆盖生成的文件 -->
                   <overwrite>true</overwrite>
               </configuration>
               
		</plugin>
	</plugins>
</build>

配置generatorConfig.xml文件

generatorConfig.xml文件放在resouces目录下

<?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>    
    <context id="sqlserverTables">    
        <!-- 生成的pojo,将implements Serializable-->    
        <!-- <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>   -->  
        <commentGenerator>    
            <!-- 是否去除自动生成的注释 true:是 : false:-->    
            <property name="suppressAllComments" value="false" />    
        </commentGenerator>    
    
        <!-- 数据库链接URL、用户名、密码 -->    
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"    
                        connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl"    
                        userId="admin"    
                        password="123456">    
        </jdbcConnection>    
    
        <!--      
        	默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer    
            true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal      
        -->    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false" />    
        </javaTypeResolver>    
    
        <!--     
		        生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,./src/main/java,    
		        也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下    
        -->    
        <!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->    
        <!-- <javaModelGenerator targetPackage="com.csdn.ingo.entity" targetProject="./src/main/java">    
            <property name="enableSubPackages" value="true"/>    
            从数据库返回的值被清理前后的空格     
            <property name="trimStrings" value="true" />    
        </javaModelGenerator>  -->   
    
        <!--对应的mapper.xml文件  -->    
       <!--  <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    --> 
    
        <!-- 对应的Mapper接口类文件 -->    
       <!--  <javaClientGenerator type="XMLMAPPER" targetPackage="com.kk.lx.sys.dao" targetProject="./src/main/java">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>     -->
        
        <javaModelGenerator targetPackage="com.kk.lx.sys.vo" targetProject="src/main/java"/>
		<sqlMapGenerator targetPackage="com.kk.lx.sys.dao"	targetProject="src/main/java" />
		<javaClientGenerator targetPackage="com.kk.lx.sys.dao"	targetProject="src/main/java" type="XMLMAPPER"/> 
    
    
        <!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->    
        <table tableName="T_SYS_USER" domainObjectName="User"    
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"    
               enableSelectByExample="false" selectByExampleQueryId="false" >    
            <property name="useActualColumnNames" value="false"/>    
        </table>   
         
    </context>    
</generatorConfiguration>

运行入口GenMain.java

package com.kk.demo;

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 GenMain {
	public static void main(String[] args) {  
        List warnings = new ArrayList();  
        boolean overwrite = true;  
        String genCfg = "/generatorConfig.xml";  
        File configFile = new File(GenMain.class.getResource(genCfg).getFile());  
        ConfigurationParser cp = new ConfigurationParser(warnings);  
        Configuration config = null;  
        try {  
            config = cp.parseConfiguration(configFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (XMLParserException e) {  
            e.printStackTrace();  
        }  
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
        MyBatisGenerator myBatisGenerator = null;  
        try {  
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
        } catch (InvalidConfigurationException e) {  
            e.printStackTrace();  
        }  
        try {  
            myBatisGenerator.generate(null);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}

你可能感兴趣的:(java)