Tanpa basa -basi lagi, kode kunci adalah sebagai berikut:
1. Kode: DBContExtholder
public class DbContextHolder {//ThreadSafe ThreadLocalprivate 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 ();}} 2. Kode: DynamicDataSource
Impor org.springframework.jdbc.datasource.lookup.abstractroutingDataSource; kelas publik DynamicDataSource memperluas abstractroutingDataSource {@Overridepublic objek determinecurrentlookkey () {return dbcontextholder. 3. Kode: Spring.xml
<!-File Konfigurasi Properti Sumber Data-> <konteks: lokasi pemegang properti = "classpath: ipatis.properties" /> <bean id = "jksh" dash-method = "tutup"> <!-info koneksi-> <nama properti = "driverclassname" value = "$ {driver.jksh}" /< /"name =" Nilai "$" $ "$" $ "$" name {"$" name = "userName" value = "$ {username.jksh}" /> <name properti = "kata sandi" value = "$ {password.jksh}" /> <!-info koneksi pooling-> <name properti = "maxidle" value = "$ {maxidle.jksh}" /<properti name = "maxActive" value = "$ {maxidle.jksh}" /<properti name = "maxactive" value "value" $ {MaxIdle.jksh} " /<properti name =" maxactive "value" value "value" $ {JKSH {JOVE NAME = "MAXACTIF" NOVE "NOVE" Nilai "Nilai" Nilai "Nilai" MAXACTIF "Nilai" Nilai "Nilai" $ "$" $ " name = "defaultAutOcommit" value = "false" /> <properti name = "timebetweenevictionrunsmillis" value = "$ {timebetweenevictionrunsmillis.jksh}" /> <nama properti = "minevictableidletimillis" value = "$ {minevictableDlleImemeMemeMeme.jykshe" {$ {$ {MinevictableImeMemeMemeMemilis.jyksh ID = "JClt" Destroy-Method = "Tutup"> <!-Info Koneksi-> <nama properti = "driverclassname" value = "$ {driver.jclt}" /> <name properti = "url" value = "$ {url.jclt}" /> <nama properti = "username" value = "$ {Username.jclt}" " value = "$ {password.jclt}" /> <!-Info Pooling Koneksi-> <nama properti = "maxidle" value = "$ {maxidle.jclt}" /> <name properti = "maxactive" value = "$ {maxactive.jclt}" /<name properti = "DEFAULTAUTOMOMMIT" value "value =" value "forse" /> <Properti name = "DEFAULTAUTOMMIT" bernilai "" value "forse" /> <Properten name = "DEFAULTOMOMMIT" value = "value =" nice "forse =" LALEVED "" value = "$ {timebetweenevictionrunsmillis.jclt}" /> <name properti = "minevictableIdletimeMillis" value = "$ {MinEvictableIdleTimeMillis.jclt}" /> < /bean> <bean id = "dataSource"> <nama properti = "targetDatacources =" datasource "> <name properti =" targetDataCources. " key = "jksh" value-ref = "jksh" /> <kunci entri = "jclt" value-ref = "jclt" /> </pap> </propert> <properti nama = "DefaultTargetDataSource" ref = "jksh" /> < /bean> 4. Kode: Metode Utama
impor javax.sql.datasource; impor org.springframework.context.applicationContext; impor org.springframework.context.support.classpathxmlapplicationcontext; impor org.spramework.core.coreo.fileSySySyStemReSource; com.jclt.service.commons.dbcontextholder; impor com.jclt.service.model.user; impor org.apache.ibatis.session.sqlsesession; impor org.apache.apacpy @param args*/public static void main (string [] args) {applicationContext appContext = new classpathxmlapplicationContext ("client-beans.xml"); dbcontextholder.setDbtype ("jclt"); string res = "src/main/sumber daya/iBatis (" jclt "); string res =" src/main/sumber daya/iBatis ("iBatis"); dATREC ("sumber/iBATOURCE (" JCLT "); STROC/SUMBERCE/IBATOURCE (" JCLT "); appContext.getBean ("DataSource"); sqlSessionFactoryBean bean = SQLSessionFactoryBean baru (); bean.setDataSource (DataSource); sumber daya sumber daya = trystemResource (res); bean.setConfiglocation (sumber daya); try {resticyfacyfacyfacyfacory (resticyfacyfacory (Resource); try {resiespacyfacyfacory (resticyfacyfacory (resiesyfacyfiCory (resource); try {res); bean. bean.getObject (); SQLSession session = sessionFactory.opensession (); pengguna pengguna = session.selecTone ("com.jclt.service.dao.readjksh.findone"); System.out.println (user.getname ());} catch (pengecualian e) {E.PrintStackTrace ();} DBContExTholder.SetDBtype ("jksh"); string res1 = "src/main/sumber daya/ibatis-config.xml"; DataSource DataSource1 = (dataSource) appcontext.xgetBean ("DataSource"); DataSource) appcontext.beAnbean ("DataSource"); DataSource) APPCONTEXT.BEANDBEAN ("DATASOURCE"); DATASICECIONCECIONDECREYCE1 ("DATASOURCE"; DATASICESI); SqlSessionFactoryBean();bean1.setDataSource(datasource1);Resource resource1=new FileSystemResource(res1);bean1.setConfigLocation(resource1);try {SqlSessionFactory sessionfactory = bean.getObject();SqlSession session=sessionfactory.openSession();User user = session.selectone ("com.jclt.service.dao.readjksh.findone"); System.out.println (user.getName ());} catch (pengecualian e) {e.printstacktrace ();}}}}Di atas adalah kombinasi pegas dan mybatis untuk mengimplementasikan fungsi switching sumber multi-data yang diperkenalkan oleh editor kepada Anda. Saya harap ini akan membantu Anda. Jika Anda memiliki pertanyaan, silakan tinggalkan saya pesan dan editor akan membalas Anda tepat waktu. Terima kasih banyak atas dukungan Anda ke situs web Wulin.com!