โดยไม่ต้องกังวลใจเพิ่มเติมรหัสสำคัญมีดังนี้:
1. รหัส: dbcontextholder
คลาสสาธารณะ dbcontextholder {// threadsafe threadlocalprivate static final threadlocal <String> contextholder = ใหม่ ThreadLocal <String> (); โมฆะคงที่สาธารณะ setDbType (สตริง dbtype) {contextholder.set (dbtype); ((String) Contextholder.get ());} โมฆะคงที่สาธารณะ cleardbtype () {contextholder.remove ();}} 2. รหัส: DynamicDataSource
นำเข้า org.springframework.jdbc.datasource.lookup.abstractroutingDatasource; DynamicDataSource ระดับสาธารณะ
3. รหัส: spring.xml
<!-ไฟล์การกำหนดค่าคุณสมบัติของแหล่งข้อมูล-> <บริบท: สถานที่ตั้งสถานที่ตั้งตำแหน่ง = "classpath: ibatis.properties" /> <bean id = "jksh" destroy-method = "close"> <! name = "username" value = "$ {username.jksh}" /> <property name = "รหัสผ่าน" value = "$ {password.jksh}" /> <!-การเชื่อมต่อข้อมูลการรวม-> <property name = "maxidle" value = "$ {maxidle.jksh}" /> name = "defaultautocommit" value = "false" /> <property name = "timebetweenevictionrunsmillis" value = "$ {timebetweenevictionrunsmillis.jksh}" /> <property name = "minevictableidletimeLis" value = "$ id = "jclt" destroy-method = "close"> <!-ข้อมูลการเชื่อมต่อ-> <property name = "driverclassname" value = "$ {driver.jclt}" /> <name property = "url" value = "$ {url.jclt}" /> <property name = "username" value = "$ {password.jclt}" /> <!-การเชื่อมต่อการรวมข้อมูล-> <property name = "maxidle" value = "$ {maxidle.jclt}" /> <property name = "maxactive" value = "$ {maxactive.jclt}" /> value = "$ {timebetweenevictionrunsmillis.jclt}" /> <property name = "minevictableidletItimeLis" value = "$ {minevictableidletimeLis.jclt}" /> </epean> <bean id = "DataSource" key-type = "java.lang.string"> <entry key = "jksh" value-ref = "jksh" /> <entry key = "jclt" value-ref = "jclt" /> < /map> 4. รหัส: วิธีหลัก
นำเข้า javax.sql.datasource; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext com.jclt.service.commons.dbcontextholder; นำเข้า com.jclt.service.model.user; นำเข้า org.apache.ibatis.session.sqlsession; นำเข้า org.apache.ibatis.session.sqlsessionfary; args*/public static void main (string [] args) {applicationcontext appcontext = ใหม่ classpathxmlapplicationContext ("client-beans.xml"); dbcontextholder.setDbType ("JCLT") AppContext.getBean ("DataSource"); sqlsessionfactorybean ถั่ว = ใหม่ sqlsessionfactorybean (); bean.setDataSource (dataSource) ทรัพยากรทรัพยากร = ใหม่ filesystorsource (res); bean.setConfiglocation (ทรัพยากร); session = sessionfactory.opensession (); ผู้ใช้ผู้ใช้ = session.selectone ("com.jclt.service.dao.readjksh.findone"); system.out.println (user.getName ());} catch (Exception e) res1 = "src/main/resources/ibatis-config.xml"; dataSource dataSource1 = (dataSource) appcontext.getBean ("DataSource"); SQLSessionFactoryBean1 = new SQLSessionFactoryBean (); bean1.setDatasource filesystemresource (res1); bean1.setConfigLocation (Resource1); ลอง {SQLSessionFactory SessionFactory = bean.getObject (); SQLSession Session = SessionFactory.opensession (); ผู้ใช้ user = session.selectone ("com.jclt.service.dao.readjksh.findone"); System.out.println (user.getName ());} catch (Exception e) {e.printstacktrace ();ด้านบนคือการรวมกันของฤดูใบไม้ผลิและ mybatis เพื่อใช้ฟังก์ชั่นการสลับแหล่งข้อมูลหลายข้อมูลที่ตัวแก้ไขแนะนำให้คุณ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!