ssm框架整合之Spring4+SpringMVC+Mybaties3之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/29

时间:2021-10-20 17:35:02

经测试,需注意以下几点:

1,controller的自动扫描不能放在applicationContext.xml中,要放在spring-mvc.xml中。同样是<context:component-scan base-package="com.xxx.controller"></context:component-scan>。(不知道为啥必须这样,若相知,请相教!)

2,所有的classpath必须得小写"p",小写"p",小写"p"。下面代码中全部写成classPath,启动时不报错,但是运行时会报错。

以下配置可直接使用,只需更改包名。

关于内部标签的解释及用法,都以注解形式在代码内部说明。个人原创,转载需注明出处。

1,web.xml。添加jar包后首先需要配置WEB-INF下的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>CRM</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <!-- 以下配置在spring文档中可通过搜索web-app获得(不包含中文编码器) --> <!-- spring的配置文件,默认放在WEB-INF下。这里将其放在src下,故需要如下配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:appliationContext.xml</param-value>
</context-param> <!-- 启动web时加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 添加springMVC支持,单独springMVC时也需要在web.xml文件中配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:spring-mvc.xml</param-value>
</init-param>
<!-- 启动web时就加载springmvc的servlet,即启动时就加载springmvc的配置文件 -->
<load-on-startup>1</load-on-startup>
<!-- 可异步执行,最好都配上,项目会比较流畅 -->
<!-- 配置异步时若报错,因为是3.0新特征,可将2.5改为3.0即可 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 拦截所有以.do结尾的请求,后续的业务代码请求后缀都将以.do结尾(个人习惯) -->
<url-pattern>*.do</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> </web-app>

web.xml

2,applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- spring自动扫描base-package下及其子包下的所有Java文件,当扫描到含有@service或@controller注解时,自动将该类注册成bean -->
<!-- 不用再配置<context:annotation-config/>,因为已包含 -->
<context:component-scan base-package="com.xxx.service"></context:component-scan>
<context:component-scan base-package="com.xxx.controller"></context:component-scan> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_Name"></property>
<property name="userName" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classPath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classPath:com/xxx/mappers/*.xml"></property>
<property name="typeAliasesPackage" value="classPath:com.xxx.entity"></property>
</bean>
<!-- sqlSessionFactory的属性 -->
<!-- 1,dataSource:必须属性 -->
<!-- 2,configLocation:当mybatis-config.xml放在src下(个人习惯),配置该属性。 目前别名已经配置在SqlSessionFactoryBean里,可否省略mybatis-config.xml文件我也不清楚。若相知,请相告!谢谢! -->
<!-- 3,mapperLocations:自动扫描mapper.xml文件。 -->
<!-- 4,typeAliasesPackage:自动配置别名 --> <!-- 转换器MapperScannerConfig它可以将接口转换为Spring容器中的Bean,不需要注解(@service) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 配置事务通知 -->
<!-- 没有采用<tx:annotation-driven/>,即注解事务管理。是因为注解事务需要在很多public方法前加上@Transactional,很麻烦,用以下方法可以一劳永逸 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 拦截以'insert','add'...'find'等开头的方法名方法。最后一句代表拦截所有方法 --> <!-- 配置事务切面,将事务通过切面的方式嵌套入代码逻辑,从而不侵入代码业务 -->
<!-- 若需单独用切面,在定义<aop:config>时需要加入切面类,即<aop:aspect> -->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.xxx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
<!-- 切点方法表达式格式 -->
<!-- execution(<scope> <return-type> <fully-qualified-class-name>.<method-name>.(parameters))
例: execution(* com.spring.service.*.*(..)):匹配service包及其子包(service.impl)所有类的所有方法;
execution(public List com.spring.service.impl.UserServiceImpl.getUserList(int,String)):
匹配UserServiceImpl类中返回List且方法名为getUserList且参数为int和String类型的所有公共方法 --> </beans>

applicationContext.xml

3,spring-mvc.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,WEB-INF下的jsp文件夹 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀,以.jsp结尾 -->
<property name="suffix" value=".jsp" />
</bean> </beans>

spring-mvc.xml

4,mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 只需要一个空的<configuration>(必须,不写会报错),数据源、别名、mapper的扫描都已经在applicationContext.xml中定义 -->
<configuration></configuration>

mybatis-config.xml