1.spring-web.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: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/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">
<!-- 配置spring MVC -->
<!-- 1.开启springmvc注解模式 -->
<mvc:annotation-driven>
<!-- 处理responseBody 里面日期类型 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js/gif/png
(2)允许使用"/"做整体映射
-->
<mvc:resources mapping="/**" location="/static/" />
<mvc:default-servlet-handler/>
<!-- 3.定义视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置逻辑视图前后缀 -->
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.rms"/>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传的最大限制 -->
<property name="maxUploadSize" value="209715200" />
<!-- 设置请求编码格式 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 上传文件的解析 -->
<property name="resolveLazily" value="true" />
</bean>
<!-- 配置登录拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/manage/**"/>
<mvc:exclude-mapping path="/manage/login.html"/>
<mvc:exclude-mapping path="/manage/login"/>
<mvc:exclude-mapping path="/manage/resources/**"/>
<bean class="com.rms.interceptor.ManageLoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
2.spring-mybatis.xml
1.基于mapper.xml文件的配置
此示例使用dbcp连接池。
<?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">
<!-- 引入 JDBC 配置文件 -->
<context:property-placeholder
location="classpath:properties/jdbc.properties"
ignore-unresolvable="true" />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 驱动类 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 连接地址 -->
<property name="url" value="${jdbc.url}" />
<!-- 用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 连接初始值,连接池启动时创建的连接数量的初始值,默认值为 0 -->
<property name="initialSize"
value="${pool.dbcp2.initialSize}" />
<!-- 最小空间值,当空间的连接数少于阈值时,连接池就会去预申请一些连接,以免洪峰来时来不及申请,默认值为 0 -->
<property name="minIdle" value="${pool.dbcp2.minIdle}" />
<!-- 最大空间值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到 maxIdle 为止 ,0 时无限制,默认值为8 -->
<property name="maxIdle" value="${pool.dbcp2.maxIdle}" />
<!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0 时无限制,默认值为8 -->
<property name="maxTotal" value="${pool.dbcp2.maxTotal}" />
<!--是否自动提交事务,即单条 SQL 语句算是一个事务 -->
<property name="defaultAutoCommit"
value="${pool.dbcp2.defaultAutoCommit}" />
</bean>
<!-- MyBatis 构建 SqlSessionFactory 的实例配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<!-- Mapper 配置文件所在路径 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
<!--配置包别名 -->
<property name="typeAliasesPackage" value="${实体类所在包全限名}"/>
</bean>
<!-- MyBatis 自动查找指定包名下的Mapper类,如果找到对应的映射文件,将包下的 interface 动态定义为 Spring Bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="${mapper接口所在包全限名}"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
jdbc.properties文件示例如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/rms?tinyInt1isBit=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=123456
pool.dbcp2.initialSize=3
pool.dbcp2.minIdle=3
pool.dbcp2.maxIdle=5
pool.dbcp2.maxTotal=15
pool.dbcp2.defaultAutoCommit=false
2.基于mapper注解的Spring整合Mybatis配置
<?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
">
<!--配置Druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/demo?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="3"></property>
<property name="maxActive" value="5"></property>
</bean>
<!--Spring整合Mybatis-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hd.dao"></property>
</bean>
</beans>