当前位置: 代码迷 >> J2EE >> spring+struts+ibatis怎么配置多数据源
  详细解决方案

spring+struts+ibatis怎么配置多数据源

热度:101   发布时间:2016-04-17 23:42:06.0
spring+struts+ibatis如何配置多数据源?
spring.xml文件:
 <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://192.168.1.69;dataBaseName=TMS" />
<property name="username" value="sa" />
<property name="password" value="sql" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="DataBaseImpl"
class="com.publicsoft.database.DataBaseImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
    <import resource="classpath:spring-xt.xml"/>     

spring-xt.xml文件:
<beans>
<bean id="loginActionSpring"
class="com.publicsoft.xt.LoginAction" scope="prototype">
<property name="database" ref="DataBaseImpl" />
</bean>       
    </beans> 

struts.xml文件:
<struts>
    <include file="/struts-xt.xml"></include>
</struts>

struts-xt.xml文件:
<struts>
<constant name="struts.i18n.encoding" value="GB2312" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
   
<package name="default"  extends="struts-default">
<global-results>
<result name="xtException">/xt/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="xtException" />
</global-exception-mappings>
    </package>
    <package name="xt" namespace="/xt" extends="default">
    <!-- 登录信息 -->
<action name="login" class="loginActionSpring" >
<result name="success">/xt/mainframe.jsp</result>
</action>
</package>
</struts>

sqlmap.xml文件:
<sqlMapConfig>
   <!-- 该文件是sql-map-config.xml文件改名而来,是iBatis专用文件 -->
   <sqlMap resource="sqlmap-xt.xml"/>
</sqlMapConfig>

sqlmap-xt.xml文件:
<sqlMap namespace="xt">
<select id="qryD_XT_KEYS" resultClass="java.lang.Integer"
parameterClass="java.util.Map">
exec tools_xt_keybuilder #table_name#,#field_name#
</select>

<select id="qryD_XT_BASECODE" resultClass="java.util.HashMap"
parameterClass="java.lang.String">
select
SCOPE_ID,SCOPE_DES,BASECODE_NO,BASECODE_VALUE,BASECODE_NAME,BASECODE_DES
from d_xt_basecode where $filter$ order by order_id
</select>
</sqlMap>

web.xml文件:
<web-app id="WebApp_9" version="2.4">
<!--配置SPRING的BEAN管理文件,默认情况下会到WEB-INF/下去找applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml, classpath:service.xml
</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在这个URI下开放Web Service服务 -->
<url-pattern>/service/*</url-pattern>
  相关解决方案