1. 程式人生 > >Mybatis 自動生成程式碼示例分享

Mybatis 自動生成程式碼示例分享

首先感謝網際網路,以及前輩的demo

二話不說了直接上程式碼先

1、Java部分

package org.icpcheck.dataRiskOnRecordApp;

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 MybatisGenerator {
	public static void main(String[] args) {
		  List<String> warnings = new ArrayList<String>();
		   boolean overwrite = true;
		try {
			File directory = new File("");
			System.out.println(directory.getCanonicalPath());//獲取標準的路徑 
			System.out.println(directory.getAbsolutePath());//獲取絕對路徑
			System.out.println(System.getProperty("user.dir"));
			File configFile = new File("D:/Software/Java_workSpace180712/dubbo/xxxx-parent/xxxxx-xxxxx/src/main/resources/generatorConfig.xml");
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config;
			
			config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (IOException | XMLParserException | InvalidConfigurationException | SQLException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   
	}
}

2、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>
    <!--資料庫驅動-->
    <!--注意要保證執行MyBatis Generator的時候能夠找到mysql的connector-->
    <!-- location   The full path name of a JAR/ZIP file to add to the classpath, or a directory to add to the classpath.-->
    <classPathEntry    location="E:\xxxxx\mvn1209\mysql\mysql-connector-java\6.0.6\mysql-connector-java-6.0.6.jar"/>
    <!-- A unique identifier for this context. This value will be used in some error messages. -->
    <context id="mysql2java"    targetRuntime="MyBatis3">
        <!-- http://www.mybatis.org/generator/configreference/commentGenerator.html -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--資料庫連結地址賬號密碼-->
<!--         <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://xx.xx.xx.xx:3306/test1?useUnicode=true" userId="root" password="1qaz2wsx3edc"> -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://xx.xx.xx.xx:3306/bahcpt?useUnicode=true&amp;useSSL=true&amp;serverTimezone=UTC" userId="root" password="1q2w3e4r">
        </jdbcConnection>
<!--         useUnicode=true&useSSL=true&serverTimezone=GMT%2BB -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <!-- targetProject 指定存放生成檔案的根目錄-->
        <javaModelGenerator targetPackage="main.java.org.xxxx.dataRiskOnRecordApp.bean" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成對映檔案存放位置-->
        <sqlMapGenerator targetPackage="main.java.org.xxxx.dataRiskOnRecordApp.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="main.java.org.xxxx.dataRiskOnRecordApp.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--配置MBG對哪個資料表進行生成工作, 對於MYSQL資料庫來說, 因為沒有SCHEMA這個概念, 必須使用catalog來指定到底用哪個資料庫, 否則如果多個數據庫有同名表, MYBATIS會自己選一個-->
       <table tableName="view_baxx_base" catalog="bahcpt" domainObjectName="RecordBaseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
 			<property name="constructorBased" value="false"/>
        	<property name="ignoreQualifiersAtRuntime" value="false"/>
        	<property name="modelOnly" value="false"/>
        	<!-- 指定查詢的排列順序 -->
        	<!-- <property name="selectAllOrderByClause" value="age desc,username asc"/> -->
        	<!-- 生成駝峰格式的欄位名 -->
        	<property name="useActualColumnNames" value="false"/>
        </table>
       
       <!--  <table tableName="icp_user" catalog="bahc" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
 			<property name="constructorBased" value="false"/>
        	<property name="ignoreQualifiersAtRuntime" value="false"/>
        	<property name="modelOnly" value="false"/>
        	指定查詢的排列順序
        	<property name="selectAllOrderByClause" value="age desc,username asc"/>
        	生成駝峰格式的欄位名
        	<property name="useActualColumnNames" value="false"/>
        </table>
        <table tableName="icp_role" catalog="bahc" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        	<property name="constructorBased" value="false"/>
        	<property name="ignoreQualifiersAtRuntime" value="false"/>
        	<property name="modelOnly" value="false"/>
        	指定查詢的排列順序
        	<property name="selectAllOrderByClause" value="age desc,username asc"/>
        	生成駝峰格式的欄位名
        	<property name="useActualColumnNames" value="false"/>
        </table>
        
        <table tableName="icp_auth" catalog="bahc" domainObjectName="Auth" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        	<property name="constructorBased" value="false"/>
        	<property name="ignoreQualifiersAtRuntime" value="false"/>
        	<property name="modelOnly" value="false"/>
        	指定查詢的排列順序
        	<property name="selectAllOrderByClause" value="age desc,username asc"/>
        	生成駝峰格式的欄位名
        	<property name="useActualColumnNames" value="false"/>
        </table>
        
        <table tableName="icp_organization" catalog="bahc" domainObjectName="Organization" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        	<property name="constructorBased" value="false"/>
        	<property name="ignoreQualifiersAtRuntime" value="false"/>
        	<property name="modelOnly" value="false"/>
        	指定查詢的排列順序
        	<property name="selectAllOrderByClause" value="age desc,username asc"/>
        	生成駝峰格式的欄位名
        	<property name="useActualColumnNames" value="false"/>
        </table> -->
    </context>
</generatorConfiguration>

3、需要jar包

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
		<dependency>
		    <groupId>org.mybatis.generator</groupId>
		    <artifactId>mybatis-generator-core</artifactId>
		    <version>1.3.7</version>
		</dependency>
		
		<dependency>
		    <groupId>org.mybatis.generator</groupId>
		    <artifactId>mybatis-generator-maven-plugin</artifactId>
		    <version>1.3.7</version>
		</dependency>

4、資料庫自建謝謝

5、工程結構

5、斧正!!!