maven+spring+mybatis+struts1+oracle中使用到的配置檔案
阿新 • • 發佈:2019-01-30
applicationContext.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Oracle資料庫配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:127.0.0.1:1521:ORCL"> </property> <property name="username" value="system"></property> <property name="password" value="123456"></property> </bean> <!-- 使用工廠模式 --> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 使用別名 --> <property name="typeAliasesPackage" value="com.fei.entity"/> <property name="mapperLocations"> <list> <value>classpath:com/fei/mapper/*.xml</value> </list> </property> </bean> <!-- 自動掃描Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.fei.mapper"></property> <property name="sqlSessionFactory" ref="SqlSessionFactory"></property> </bean> <!-- 定義事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven /> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 定義切面 --> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.fei.services..*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> <!-- 開啟自動掃描服務 --> <context:component-scan base-package="com.fei.services" /> <!--開啟了自動掃描,下面的Service層的Bean就不需要進行配置了,在Service層使用@Service @Resource註解就可以了 <bean id="OrderService" class="com.fei.services.OrderServiceImpl"> <property name="orderMapper" ref="orderMapper"></property> </bean> --> <!-- 這是整合的struts1的bean ,name的值與action中的path值需要對應 --> <!-- 還可以使用右鍵spring》new bean 的方式新增使用P值注入 --> <bean name="/add" class="com.fei.struts.action.AddAction"> <property name="orderService" ref="orderServiceImpl" /> </bean> </beans>
struts-config.xml中的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <!-- input值需要封裝到的類 --> <form-beans> <form-bean name="addForm" type="com.fei.entity.Order" /> </form-beans> <!-- 一下兩個節點沒有進行配置。但是是比較常見的兩個節點,所以特別說明一下 --> <!-- 全域性異常 --> <global-exceptions /> <!-- 全域性跳轉 --> <global-forwards /> <action-mappings> <action attribute="addForm" input="add.jsp" name="addForm" path="/add" scope="request" parameter="opr" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="SUCCESS" path="/success.jsp" redirect="true" /> </action> <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"> <!-- 跳轉網頁 --> <forward name="SUCCESS" path="/add.jsp" redirect="true" /> </action> </action-mappings> <!-- 預設生成的資源路徑 --> <message-resources parameter="com.fei.struts.ApplicationResources" /> <!-- 配置為Spring託管 --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in> <!-- 配置型別轉換器 --> <!--<plug-in className="com.fei.converter.DateConverter"/>--> <!-- <set-property property="cancellable" value="false" /> --> <!-- type="org.springframework.web.struts.DelegatingActionProxy"--> </struts-config>
另外需要注意幾點:
1 在services實現類或者在action中,建立的物件屬性,都是使用的介面屬性。如果使用例項類會報錯
2 屬性需要get,set封裝
3 mapper.xml中,封裝的資料資料庫要和實體類一致,如果不同,需要使用resultMap進行關聯collection是一對多,就是屬 性裡面是list或者別的集合型別,association是多對一,就是指一個物件型別的屬性
4 還有在JSP頁面使用JSTL的話,除了pom.xml新增依賴,還需要在JSP頁面頁頭進行如下宣告才可以使用:
後面pom的依賴我在另一個帖子中進行貼出<%@ page isELIgnored="false"%>