SSM框架的spring配置文件一般分为三个(个人习惯):spring-dao.xml,spring-service.xml,spring-mvc.xml。基于maven的web项目中还有pom.xml,web.xml配置文件。特此整理了以上配置文件的常用模板。
1-1 spring-dao.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"
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">
<context:property-placeholder location="classpath:[这里写dataSource.properties的路径]"/>
<!--数据库连接池配置-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置扫面mapper.xml文件的位置,如果使用注解方式,这里无需配置 -->
<property name="mapperLocations" value="classpath:这里写XXmapper.xml所在的文件夹路径"/>
<!--配置包别名,以后在mapper.xml文件中可以省略包名com.cr.zshop.pojo-->
<property name="typeAliasesPackage" value="[这里写pojo类所在的包]"/>
<!-- 配置分页插件 -->
<property name="plugins">
<list>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</list>
</property>
</bean>
<!--配置MapperScannerConfigurer,根据扫描到的dao接口,获取我们想要的mapper对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--dao接口所在的包-->
<property name="basePackage" value="[这里写dao接口所在的包]"/>
</bean>
</beans>
1-2 spring-service.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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--自动扫包-->
<context:component-scan base-package="[这里写service实现类所在的包]"></context:component-scan>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!--支持事务注解的(@Transactional)-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
1-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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 扫包 -->
<context:component-scan base-package="[这里写controller所在的包]"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="[这里写jsp所在文件夹的目录]" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 给静态文件添加映射路径 -->
<mvc:default-servlet-handler/>
<!--配置上传文件,如果无,可以不配置-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
</beans>
2-1pom.xml(父工程)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>[工程groupId]</groupId>
<artifactId>[工程artifactId]</artifactId>
<packaging>pom</packaging>
<version>[版本号]</version>
<name>...</name>
<url>http://maven.apache.org</url>
<!--进行版本号的统一管理-->
<properties>
<junit.version>3.8.1</junit.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
</properties>
<!-- 父工程进行依赖的管理 -->
<dependencyManagement>
<!-- 添加依赖关系 -->
<dependencies>
<!--这里写依赖,例如下:-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<!--指定依赖的作用域,默认为compile,参与编译和打包,test只参与测试相关的编译,不参与打包-->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>[项目最终名称]</finalName>
<plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
<!--插件管理-->
<pluginManagement>
<plugins>
<!--不适用tomcat7插件可以省略下面代码-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<!--子工程的artifactId-->
<module>[子工程的artifactId]</module>
</modules>
</project>
2-2pom.xml(子工程)
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!--父工程-->
<parent>
<groupId>[父工程groupId]</groupId>
<artifactId>[父工程artifactId]</artifactId>
<version>[父工程版本号]</version>
</parent>
<artifactId>[工程artifactId]</artifactId>
<name>[工程名称]</name>
<url>http://maven.apache.org</url>
<packaging>[打包方式:war或jar]</packaging>
<dependencies>
[这里添加项目依赖,不声明依赖的版本号,例如下:]
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
</dependencies>
<build>
<finalName>[这里写最终项目名称]</finalName>
<plugins>
<!--tomcat7插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!--访问路径名称,‘/’代表无-->
<path>/</path>
<!--端口号-->
<port>9002</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
3 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"
version="3.0">
<!-- 配置dispatcher分发器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件
spring-dao.xml,spring-service.xml,spring-mvc.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--spring配置文件所在的路径-->
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<!-- 1、<url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
2.<url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>