都知道使用Mybatis generator能够给项目自动生成mybatis相关代码,但是对于Spring Cloud分布式项目来说,每个项目配置一个未免太过杂乱,故统一整合为一个项目cloud-generator,为其他各个子项目分别生成数据库操作的相关代码。
Mybatis-generator的使用可参考:Springboot mybatis generate 自动生成实体类和Mapper
在整个分布式项目中的位置大概如图所示:
其中generator项目下具体结构案例有如下:
项目分解
- cloud-generator / cloud-generator-A
PaginationPlugin.java
//PaginationPlugin.java
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ShellRunner;
import java.util.List;
/**
* The class Pagination plugin.
*
* @author
*/
public class PaginationPlugin extends PluginAdapter {
/**
* Validate boolean.
*
* @param warnings the warnings
*
* @return the boolean
*/
@Override
public boolean validate(List<String> warnings) {
return true;
}
private static void generate() {
String config = PaginationPlugin.class.getClassLoader().getResource("generator/generatorConfig-B.xml").getFile();
String[] arg = {"-configfile", config, "-overwrite"};
ShellRunner.main(arg);
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
generate();
}
}
// generatorConfig-A.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>
<properties resource="config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple"
defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="${mapper.plugin}">
<property name="mappers" value="${mapper.Mapper}"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}" userId="${jdbc.user}" password="${jdbc.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="${targetModelPackage}"
targetProject="${targetJavaProject}"/>
<sqlMapGenerator targetPackage="${targetXMLPackage}"
targetProject="${targetResourcesProject}"/>
<javaClientGenerator targetPackage="${targetMapperPackage}"
targetProject="${targetJavaProject}" type="XMLMAPPER"/>
<!-- 设置要生成代码的表 -->
<table tableName="hc_uac_user_token" domainObjectName="UacUserToken">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="hc_mq_message_data" domainObjectName="MqMessageData">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
// generatorConfig-B.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>
<properties resource="config.properties"/>
<!-- 数据库驱动 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!-- 数据库链接URL, 用户名、密码 -->
<jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.user}" password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.cloud.provider.model.domain" targetProject="D:\git\cloud-git\cloud-master\cloud-generator\cloud-generator-uac\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="D:\git\hcloud-git\cloud-master\cloud-generator\cloud-generator-uac\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.cloud.provider.mapper" targetProject="D:\git\cloud-git\cloud-master\cloud-generator\cloud-generator-uac\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成哪些表 -->
<!--
<table tableName="hc_uac_action" domainObjectName="UacAction" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="hc_uac_group" domainObjectName="UacGroup" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="hc_uac_log" domainObjectName="UacLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="hc_uac_role" domainObjectName="UacRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
<!--<table tableName="hc_uac_user_token" domainObjectName="UacUserToken" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
<table tableName="hc_mq_message_data" domainObjectName="MqMessageData" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
// config.proties
# \u6570\u636E\u5E93\u914D\u7F6E
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xxxxxxxxx:3306/cloud-A?useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.password=XXXX
#c3p0
jdbc.maxPoolSize=50
jdbc.minPoolSize=10
jdbc.maxStatements=100
jdbc.testConnection=true
mapper.plugin = tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper = tk.mybatis.mapper.common.Mapper
// pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-generator</artifactId>
<groupId>com.hyacinthcloud.generator</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-generator-app</artifactId>
<packaging>jar</packaging>
<name>cloud-generator-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.6.RELEASE</spring.version>
<!-- MyBatis Generator -->
<!-- Java接口和实体类 -->
<targetJavaProject>${basedir}/src/main/java</targetJavaProject>
<targetMapperPackage>com.cloud.provider.mapper</targetMapperPackage>
<targetModelPackage>com.cloud.provider.model.domain</targetModelPackage>
<!-- XML生成路径 -->
<targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
<targetXMLPackage>mapper</targetXMLPackage>
</properties>
</project>
版权声明:本文为qq_28202661原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。