Springboot+Mybatis實現多資料來源配置
阿新 • • 發佈:2019-01-31
1、預設application.properties配置檔案增加多資料來源配置,也可另行自己增加新的配置檔案獨立維護
## master資料來源[主端業務]:用於mybatis自動程式碼生成呼叫及spring對資料庫的系列操作 master.datasource.url=jdbc\:mysql\://*.*.*.*\:28801/PMDB?useUnicode=true&&characterEncoding=utf-8&useSSL=false master.datasource.username=root master.datasource.password=root master.datasource.driver-class-name=com.mysql.jdbc.Driver ## cluster資料來源[小品牌業務]:用於mybatis自動程式碼生成呼叫及spring對資料庫的系列操作 cluster.datasource.url=jdbc\:mysql\://*.*.*.*:28801/jdd_ticket?useUnicode=true&&characterEncoding=utf-8&useSSL=false cluster.datasource.username=root cluster.datasource.password=root cluster.datasource.driver-class-name=com.mysql.jdbc.Driver
2、Mybatis配置檔案中根據配置項自動生成不同庫的業務程式碼,配置檔案內容為:
<?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="application.properties" /> <!-- 指定資料連線驅動jar地址 --> <!-- <classPathEntry location="${driverPath}" /> --> <!-- 此處指定生成針對MyBatis3的DAO --> <context id="context" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Criteria" /> </plugin> <!-- 去掉生成出來的程式碼的註解 --> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <!-- jdbc連線資訊 --> <jdbcConnection driverClass="${cluster.datasource.driver-class-name}" connectionURL="${cluster.datasource.url}" userId="${cluster.datasource.username}" password="${cluster.datasource.password}"> </jdbcConnection> <!-- 預設false,表示把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer; true表示把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成bean物件 --> <javaModelGenerator targetPackage="${target_package}.model" targetProject="./src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="true" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成sqlMap xml --> <sqlMapGenerator targetPackage="${target_package}.cluster.mapping" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的類檔案 --> <javaClientGenerator targetPackage="${target_package}.cluster.dao" targetProject="./src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- <table tableName="t_system" domainObjectName="CaseSystem"></table> <table tableName="t_funcrole" domainObjectName="Funcrole"></table> <table tableName="t_function" domainObjectName="Function"></table> <table tableName="t_role" domainObjectName="Role"></table> <table tableName="t_case" domainObjectName="Case"></table> <table tableName="t_result_detail" domainObjectName="ResultDetail"></table> <table tableName="t_result" domainObjectName="Result"></table> --> <table tableName="t_ticketcase" domainObjectName="TicketCase"> <columnOverride column="header" javaType="java.lang.String" jdbcType="VARCHAR" /> <columnOverride column="params" javaType="java.lang.String" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
3、根據Mybatis外掛,封裝自動生成方法
package com.jdd.itesting; 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; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * 通過mybatis框架命令執行生成程式碼 * @author cristin * @version $Id: MyGenerator.java, v 0.1 2018年4月24日 下午10:55:14 cristin Exp $ */ public class MyGenerator { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { boolean overwrite = true; File directory = new File("."); /** 專案中/src/main */ String mainPath = directory.getCanonicalPath() + File.separator + "src" + File.separator; /** 專案中/src/main/resources/generatorConfig.xml */ String generatorConfigPath = mainPath + "main" + File.separator + "resources" + File.separator + "generatorConfig.xml"; System.out.println("generatorConfig.xml=" + generatorConfigPath + " mainPath " + mainPath); File configFile = new File(generatorConfigPath); List<String> warnings = new ArrayList<String>(); 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); System.out.println("Success"); } }