spring-sprinMVC-mabatis配置:
从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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>fenye</display-name>
<welcome-file-list>
<!-- 设置登陆的第一个页面 ,默认打开的页面-->
<welcome-file>/WEB-INF/views/login.jsp</welcome-file>
</welcome-file-list>
<!-- 1.配置监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--已下面路径为准,加载配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</context-param>
<!-- 加载springmvc配置文件 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<!-- 映射拦截器:控制所有映射 找到相应的controller映射地址 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置日志输出 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
</web-app>
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- spring拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!--拦截器 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
<!--需要拦截的映射 -->
<mvc:mapping path="/user/**"/>
<mvc:mapping path="/address/**"/>
<mvc:mapping path="/record/**"/>
<!-- 不进行拦截 -->
<!-- <mvc:exclude-mapping path=""/> -->
<!-- java 拦截器类地址 -congtroller -->
<bean class="com.ydyz.controller.LoginInterceptor" />
</mvc:interceptor>
<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>
<!-- 扫描包 只扫描 @Controller -->
<context:component-scan base-package="com.ydyz" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 配置相应实体解析器(jsp) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- jsp文件路径的前缀 -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 注解支持 -->
<mvc:annotation-driven />
<!-- 资源配置 -->
<!-- <mvc:resources location="/static/" mapping="/static/**"/> -->
<!-- <mvc:resources location="/bootstrap/" mapping="/bootstrap/**"/> -->
<!-- 默认的控制器 ,有此配置,就不需要上面的"资源配置"-->
<mvc:default-servlet-handler />
<!-- 上传配置 解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传的最大字节数,-1代表没有任何限制 -->
<property name="maxUploadSize" value="10240000" />
</bean>
<!-- 指定生命周期管理器 -->
<!-- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor">
</bean>
<aop:config proxy-target-class="true"></aop:config>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="ProxyTargetClass" value="true"></property>
</bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
-->
</beans>
spring.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 扫描包 扫描 @Service @Repository @Component 排除 @Controller -->
<context:component-scan base-package="com.ydyz" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>
<!-- 引入 mybatis 配置 -->
<import resource="spring-mybatis.xml" />
<!-- 引入spring定时器 -->
<!-- <import resource="spring-job.xml"/> -->
</beans>
sprting-mabatis.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 加载配置资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 加载数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--连接池中保留的最大连接数。默认值: 15 -->
<property name="maxPoolSize" value="${max.pool.size}" />
<!-- 连接池中保留的最小连接数,默认为:3 -->
<property name="minPoolSize" value="${min.pool.size}" />
<!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3 -->
<property name="initialPoolSize" value="1" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- Mapper.xml -->
<property name="mapperLocations" value="classpath*:com/ydyz/mapper/**/*Mapper.xml" />
<!-- 实体的别名 -->
<property name="typeAliasesPackage" value="com.ydyz.domain" />
<!-- settings -->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="autoMappingBehavior" value="FULL" />
<property name="useGeneratedKeys" value="true" />
<property name="mapUnderscoreToCamelCase" value="true" />
</bean>
</property>
</bean>
<!-- mapper 配置扫描器 DAO -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ydyz.mapper" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
</bean>
<!-- 声明式事务 -->
<!-- 数据库事务管理类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务拦截点 、切点 -->
<!-- <aop:config>
拦截service,不能拦截Dao
<aop:pointcut id="txPointcut" expression="execution(* com.ydyz..*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
name对应目标方法的方法名
REQUIRED 表示有开启的事务就用这个,没有开启的打开一个
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="importExcel*" propagation="REQUIRED" />
read-only表示只读,只能查看,不能修改
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
除了定义的方法,其他方法都是只读模式,即只能查看不能修改
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice> -->
<!-- 注解式事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
工程结构: