SSM整合常用配置
一. resources文件夹下
1.db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/recruit?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
db.username=root
db.password=root
2. spring-context.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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.smart.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.smart.ssm.controller"/>
<!-- Spring MVC不处理静态资源请求 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven>
<mvc:message-converters >
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件,指定字符集 -->
<property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<!-- 指定默认输出的日期格式 -->
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
<!-- 序列化对象配置可选 -->
<property name="serializerFeatures">
<array>
<!-- 输出key时是否使用双引号,默认为true -->
<value>QuoteFieldNames</value>
<!-- 将JavaBean序列化为带格式的JSON文本 -->
<value>PrettyFormat</value>
<!-- 是否输出值为null的字段,默认为false -->
<value>WriteMapNullValue</value>
<!-- List字段若为null,输出[],而非null -->
<value>WriteNullListAsEmpty</value>
<!-- 数值字段若为null,输出0,而非null -->
<value>WriteNullNumberAsZero</value>
<!-- 字符类型字段若为null,输出”“,而非null -->
<value>WriteNullStringAsEmpty</value>
<!-- Boolean字段若为null,输出false,而非null -->
<value>WriteNullBooleanAsFalse</value>
<!-- 日期格式化 -->
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
4. spring-mybatis.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
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 用来注册所有的mapper.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
<property name="typeAliasesPackage" value="com.smart.ssm.entity"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注册Mapper.java的接口文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.smart.ssm.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init- method="init" destroy-method="close">
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
</beans>
二. webapp/WEB-INF文件夹下
1. web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<!-- spring监听类 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring其他配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- DispatcherServlet类 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 加载spring mvc配置文件 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 配置 DispatcherServlet优先加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三. Maven项目简单结构