Spring SpringMvc 3.0 + MyBatis 整合
一、使用的jar包就不詳細講解了,下載了Mybatis 和 Spring 的jar包基本上都新增上去了、
一圖概括:(這是我使用的ar包,有些不是Mybatis 和 Spring 的 )
二、 web.xml配置檔案
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WeShare</display-name> <welcome-file-list> <welcome-file>/jumper.html</welcome-file> </welcome-file-list> <!-- 載入spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 設定Spring容器載入配置檔案路徑 (主要配置都在這裡面) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml </param-value> </context-param> <!-- 配置Spring核心控制器 --> <servlet> <servlet-name>web</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 不指定 <init-param> 會自動找web.xml相同路徑下 web-servlet.xml檔案 --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>web</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 解決工程編碼過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- dwr 新增配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <description></description> <param-name>debug</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/common/jsp/error.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/common/jsp/error403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/common/jsp/error404.jsp</location> </error-page> </web-app>
三、 <!-- 配置Spring核心控制器 -->
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 不指定 <init-param> 會自動找web.xml相同路徑下 web-servlet.xml檔案 -->
<!--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet> 這個我使用的是預設的 web-servlet.xml檔案. 如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <context:component-scan base-package="com.weshare.**.web"/> <!-- /hrtiaoxin/src/java/com/guohualife/ hr/pfc/ web/controller/HrDeptMarkController.java --> </beans>
四: applicationContext.xml 檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- properties配置檔案 -->
<bean id="config"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 是否忽略不可解析的 -->
<property name="ignoreUnresolvablePlaceholders" value="true" />
<!-- 多個locations, 單個location <value> -->
<property name="locations">
<list>
<value>/WEB-INF/config/config.properties</value>
<value>/WEB-INF/config/urlAddress.properties</value>
<!--
<value>/WEB-INF/platform/config/config.properties</value>
<value>/WEB-INF/config/config.properties</value>
<value>/WEB-INF/hr/config/config.properties</value>
-->
</list>
</property>
</bean>
<!-- 檔案上傳 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" ></bean>
<!-- 載入 其他xml檔案 -->
<import resource="/config/aC-common.xml" />
<import resource="/config/aC-interceptor.xml" />
<import resource="/config/aC-properties.xml" />
<import resource="/config/aC-quartz-config.xml" />
</beans>
包含的其他4個xml檔案:
4.1 : aC-common.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.weshare.*.api.dao"></context:component-scan>
<context:component-scan base-package="com.weshare.*.api.service"></context:component-scan>
<context:component-scan base-package="com.weshare.common.utils"></context:component-scan>
<bean id="weshareDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${common.db.driver}" />
<property name="url" value="${common.db.url}" />
<property name="username" value="${common.db.username}" />
<property name="password" value="${common.db.password}" />
<!-- 最大連線資料庫連線數 -->
<property name="maxActive" value="500" />
<!-- 最大等待連線中的數量 0標識沒有限制 -->
<property name="maxIdle" value="10" />
<!-- 最大等待毫秒數 超時報錯 -->
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
<!-- 是否自我中斷 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
<bean id="weshareSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource">
<ref bean="weshareDataSource" />
</property>
<!-- MyBatis 的 XML 配置檔案路徑 -->
<property name="configLocation" value="/WEB-INF/config/mybatisSqlMapConfig.xml" />
<!-- 掃描自動生成的xml檔案 --><!-- Mybatis XML對映檔案 -->
<property name="mapperLocations" >
<list><!-- Mybatis XML對映檔案 -->
<value>classpath*:com/weshare/common/generated/xml/*.xml</value>
<!-- 掃描自己寫的xml檔案-->
<value>classpath*:com/weshare/*/api/xml/*.xml</value>
</list>
</property>
</bean>
<bean id="weshareSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="weshareSessionFactory"></constructor-arg>
</bean>
<!-- 註冊單個 mybatisGenerator 自動生成的 介面檔案-->
<!--
<bean id="TestMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.weshare.common.generated.dao.TestMapper" />
<property name="sqlSessionTemplate" ref="weshareSqlSessionTemplate" ></property>
</bean>
-->
<!-- 掃描mybatisGenerator 自動生成的 所有介面-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.weshare.common.generated.dao" ></property>
</bean>
<!-- 資料庫事務策略-->
<bean id="weshareTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="weshareDataSource" />
</property>
</bean>
<tx:advice id="weshareTxAdvice" transaction-manager="weshareTransactionManager">
<tx:attributes>
<!--
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="ins*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="select*" read-only="true" />
-->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution( * com.weshare.*.api.service.*.*(..))" advice-ref="weshareTxAdvice" />
</aop:config>
</beans>
4.2 aC-interceptor.xml
這裡攔截器只是攔截到controller , 具體攔截到action',後面會有寫到, 這裡的配置只是參考。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<mvc:interceptors>
<mvc:interceptor>
<!--設定攔截的路徑 mvc:mapping指定到哪個action , 用mappingURL匹配方法-->
<mvc:mapping path="/dynamic/dynamic.do" />
<bean class="com.weshare.common.web.LoginInterceptorController">
<property name="mappingURL" value="^.*checklogin$" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
4.3 aC-properties.xml
這個xml作用是在啟動專案的時候給org.springframework.beans.factory.config.PropertiesFactoryBean 賦值,這樣在程式碼中可以使用下面方法獲得這些值
@Resource
private Properties imageUrlProperties;
imageUrlProperties.getProperty("dynamicUrl")
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- platform properties -->
<bean id="imageUrlProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="properties">
<props>
<prop key="aliyuming">${aliyuming}</prop>
<prop key="ACCESS_ID">${ACCESS_ID}</prop>
<prop key="ACCESS_KEY">${ACCESS_KEY}</prop>
<prop key="bucketDynamicAndHeadimages">${bucketDynamicAndHeadimages}</prop>
<prop key="faceurl.pre">${faceurl.pre}</prop>
<prop key="imagesUrl.pre">${imagesUrl.pre}</prop>
<prop key="dynamicUrl">${dynamicUrl}</prop>
<prop key="headUrl">${headUrl}</prop>
<prop key="edge.dynamic">${edge.dynamic}</prop>
<prop key="edge.small">${edge.small}</prop>
<prop key="edge.middle">${edge.middle}</prop>
<prop key="edge.big">${edge.big}</prop>
</props>
</property>
</bean>
</beans>
4aC-quartz-cofig.xml, 這個是批處理定時任務的xml配置方法, 在這裡我並沒有使用, 我使用的總是註解的方式, 後面會講到。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/fex http://www.springframework.org/schema/fex/spring-fex-1.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans">
<!-- 下面是使用註解配置的方法 -->
<context:component-scan base-package="com.weshare.*.api.batch" />
<task:executor pool-size="5" id="executor" />
<task:scheduler pool-size="10" id="scheduler" />
<task:annotation-driven scheduler="scheduler" executor="executor" />
<!-- 下面是 使用xml配置的方法 -->
<!-- <bean id="personBatch" class="com.weshare.person.api.batch.PersonBatch"
/> -->
<!-- 啟動觸發器的配置開始 -->
<!-- <bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property>
</bean> -->
<!-- 啟動觸發器的配置結束 -->
<!-- quartz-2.x的配置 -->
<!-- <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property
name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->
<!-- 排程的配置結束 -->
<!-- job的配置開始 -->
<!-- <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"> <ref bean="personBatch" /> </property> <property
name="targetMethod"> <value>testMethod</value> </property> </bean> -->
<!-- job的配置結束 -->
</beans>
五:註解方式
下面deleteDynamic方法的 呼叫地址為: localhost:8080/xx工程名/dynamic/admin.do?action=deleteDynamic