/ssm/WebContent/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>ssm</display-name>
<!-- 配置spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-bean.xml</param-value>
</context-param>
<!-- 配置spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springMVC -->
<servlet>
<servlet-name>dispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>
/ssm/src/spring-bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 1集成mybatis -->
<!-- 引入数据库资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></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>
</bean>
<!-- 配置mybatis的mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.**.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 2配置注解扫描 -->
<!-- 开启注解扫描 -->
<context:annotation-config/>
<!-- 扫描service包 -->
<context:component-scan base-package="com.**.service"></context:component-scan>
<!-- 3配置事务注解扫描 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
/ssm/src/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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置静态资源映射路径 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<!-- 配置controller注解扫描 -->
<!-- <mvc:annotation-driven/>:支持MVC注解 -->
<mvc:annotation-driven/>
<!-- 扫描controller包 -->
<context:component-scan base-package="com.**.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jstl视图解析器 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 视图页面路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 视图页面路径的后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 在存在多个匹配的视图页面的情况下,优先使用order值最小的视图解析器 -->
<property name="order" value="0"></property>
</bean>
<mvc:annotation-driven conversion-service="conversionService">
<!-- 配置返回json的日前转换 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 后台向前台返回json时, 将日期类型(java.util.Date)转换成格式化字符串 统一配置为格式yyyy-MM-dd
HH:mm:ss, 如需要其他格式,可以在实体类日期类型的属性上加注解 @JsonFormat(pattern="yyyy-MM-dd") private
Date birthdate; -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置全局日期转换 -->
<!-- 日期转换,后台接值 日期字符串 转换成 日期类型(java.util.Date) -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.convertor.DateConvertor"></bean>
</list>
</property>
</bean>
<!-- 配置文件上传 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置上传文件的大小限制 -->
<property name="maxUploadSize" value="1024000000"></property>
</bean>
<!-- 配置拦截器 --><!-- 按需配置 -->
<!-- 国际化操作拦截器 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
<!-- 国际化语言切换请求参数 -->
<property name="paramName" value="locale" />
</bean>
</mvc:interceptors>
<!-- 国际化信息资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- basename用来指定properties文件的通用名
比如message_en.properties,message_zh_CN.properties通用名都是message -->
<property name="basename" value="message" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 国际化本地区域解析器 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver" >
<property name="cookieName" value="locale"/>
<property name="cookieMaxAge" value="31536000"/>
<property name="defaultLocale" value="zh_CN"/>
</bean>
</beans>
/ssm/src/db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/databaseName?useUnicode=true&characterEncoding=UTF8
username=root
password=123456
/ssm/src/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>
<!-- 配置 -->
<settings>
<!-- 控制台打印日志,可以查看打印的sql语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
/ssm/src/log4j.properties
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout
log4j.logger.org.mybatis=debug
log4j.logger.com.oldnoop=debug
#log4j.logger.org.mybatis.spring=debug
#log4j.logger.org.mybatis.spring.mapper=debug
/ssm/src/com/convertor/DateConvertor.java
package com.convertor;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class DateConvertor implements Converter<String, Date>{
@Override
public Date convert(String text) {
if(text==null) {return null;}
text=text.trim();
try {
if("yyyy-MM-dd".length()==text.length()) {
return new SimpleDateFormat("yyyy-MM-dd").parse(text);
}
if("yyyy-MM-dd hh:mm:ss".length()==text.length()) {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(text);
}
} catch (Exception e) {e.printStackTrace();}
return null;
}
}