Build two util classes first
Package Util;import org.springframework.stereotype.Component; @Componentpublic class Datasourcecontextholder { private static final threadlocal<string> Contextholder = new threadlocal<string> (); public static void Setdbtype (String dbType) { contextholder.set (dbType); } public static String Getdbtype () { return (String) contextholder.get ()); } public static void Cleardbtype () { contextholder.remove (); } }
Package Util;import java.sql.SQLFeatureNotSupportedException; Import Java.util.logging.Logger; Import Org.springframework.jdbc.datasource.lookup.abstractroutingdatasource;import org.springframework.stereotype.Component; @Componentpublic class Dynamicdatasource extends Abstractroutingdatasource { @Override public Logger Getparentlogger () { return null; } @Override protected Object Determinecurrentlookupkey () { return datasourcecontextholder. Getdbtype (); } }
directly in the controller
Datasourcetwo and Datasourceone are data sources that match in the properties
Spring +mybatis Multi-data source