首先感谢互联网,以及前辈的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&useSSL=true&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、斧正!!!