标签:
一、使用的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>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml
- </param-value>
- </context-param>
-
-
- <servlet>
- <servlet-name>web</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
- <!--
- <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>
-
-
- <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">
-
-
- <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"/>
-
- </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">
-
-
- <bean id="config"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
- <property name="ignoreUnresolvablePlaceholders" value="true" />
-
- <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>
-
-
- <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
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 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">
-
-
-
- <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" />
-
-
-
-
-
-
-
- <!-- <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> -->
-
-
-
-
-
- <!-- <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> -->
-
-
-
-
-
- <!-- <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject"> <ref bean="personBatch" /> </property> <property
- name="targetMethod"> <value>testMethod</value> </property> </bean> -->
-
-
- </beans>
五:注解方式
下面deleteDynamic方法的 调用地址为: localhost:8080/xx工程名/dynamic/admin.do?action=deleteDynamic
整理的有点乱,【源码地址获取】,希望可以帮助到大家
Spring SpringMvc + MyBatis 整合
标签:
原文地址:http://www.cnblogs.com/zhenjava/p/4714308.html