注意:环境最好使用 jdk1.7、1.8
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=%5p [%t] - %m%n
databases.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
jdbc.username=root jdbc.password=123456#可以允许的最小的空闲连接数 (默认:0)
minIdle=45
#可以允许的最大的空闲连接数 (默认:8)
maxIdle=50
#连接池在初始化连接时,第一次需要创建的个数 (默认值:0)
initialSize=5
#同时连接的最大连接数
maxActive=100
#最大的等待连接时间 (默认:-1)
maxWait=100
#是否开启无用连接的回收机制
removeAbandoned=true
#控制连接池超出配置时间后回收机制
removeAbandonedTimeout=180
applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解扫描位置-->
<context:component-scan base-package="com.smbms.mapper"/>
<context:component-scan base-package="com.smbms.service"/>
<!-- 读取数据库配置文件 -->
<context:property-placeholder location="classpath:databases.properties"/>
<!--配置数据源(使用dbcp连接池)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${initialSize}"/> <!--连接池在初始化连接时,第一次需要创建的个数 (默认值:0)-->
<property name="maxActive" value="${maxActive}"/> <!--同时连接的最大连接数-->
<property name="maxIdle" value="${maxIdle}"/> <!--可以允许的最大的空闲连接数 (默认:8)-->
<property name="minIdle" value="${minIdle}"/> <!--可以允许的最小的空闲连接数 (默认:0)-->
<property name="maxWait" value="${maxWait}"/> <!--最大的等待连接时间 (默认:-1)-->
<property name="removeAbandoned" value="${removeAbandoned}"/> <!--是否开启无用连接的回收机制-->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/> <!--控制连接池超出配置时间后回收机制-->
<!--SQL心跳包-->
<property name="testWhileIdle" value="true"/> <!--开启定时校验-->
<property name="testOnBorrow" value="false"/> <!--对拿到的连接是否校验-->
<property name="testOnReturn" value="false"/> <!--对于返回的连接是否校验-->
<property name="validationQuery" value="select 1"/> <!--定义校验所使用的SQL语句-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/> <!--时间间隔-->
<property name="numTestsPerEvictionRun" value="${maxActive}"/> <!--定义每次校验的连接数-->
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.smbms.model"/>
</bean>
<!--配置DAO数据映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--
MapperScannerConfigurer递归扫描基准包下所有接口,
若它们在SQL映射文件中定义过,则动态注册为MapperFactoryBean,
如此即可批量产生映射器实现类
-->
<property name="basePackage" value="com.smbms.mapper.**"/>
</bean>
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解事务的支持-->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- AOP 事务处理 开始 -->
<!-- <aop:aspectj-autoproxy />-->
<!-- <aop:config proxy-target-class="true">-->
<!-- <aop:pointcut expression="execution(* *com.smbms.service..*(..))" id="transService"/>-->
<!-- <aop:advisor pointcut-ref="transService" advice-ref="txAdvice" />-->
<!-- </aop:config>-->
<!-- <tx:advice id="txAdvice" transaction-manager="txManager">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
</beans>
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解扫描位置-->
<context:component-scan base-package="com.smbms.controller"/>
<!--注解驱动-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
<!--
fastjson 用于多视图解析器返回json格式
spring mvc 支持多视图 并没有具体的实现所以要依赖第三方jar fastjson
-->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<!--输出Date的日期转换格式-->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--静态资源文件的引用-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!--
配置多视图解析器:
允许同样的结果内容数据呈现不同的view
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorParameter" value="true"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html;charset=UTF-8"/>
<entry key="json" value="application/json;charset=UTF-8"/>
<entry key="xml" value="application/xml;charset=UTF-8"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<!--
全局异常
java.lang.RuntimeException 自定义的异常类
error 逻辑视图名
-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">error</prop>
<prop key="java.lang.NumberFormatException">error</prop>
</props>
</property>
</bean>
<!--配置multipartResolver,用于文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>
<!--
配置interceptors
权限控制
-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/sys/**"/>
<bean class="com.smbms.interceptor.SysInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--默认访问路径-->
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>
<!--启动spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 监听器加载spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 过滤器,解决post的乱码问题 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
用到的一些jar包
项目结构