src/java/resources
1:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--这里进行文件的整合-->
<!--import标签可以进行隐式的导入 将springmvc.xml,spring-service.xml,spring-mybatis.xml放在同一个上下文之中-->
<import resource="classpath:spring-mybatis.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:springmvc.xml"/>
</beans>
2:database.properties
# java database connection driver
jdbc.driver=com.mysql.jdbc.Driver
# java database connection url
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
# java database connection username
jdbc.username=root
# java database connection password
jdbc.password=root
maxActive=20
initialSize=1
maxWait=60000
minIdle=10
3: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>
<!--mybatis核心配置文件-->
<!--sqlSessionFactory对象的创建交给springioc容器去创建,数据源也交给spring去管理,因此这里就剩下别名的配置和
映射文件在核心配置文件中的注册-->
<typeAliases>
<!--给包名起别名,默认别名是类名首字母小写-->
<package name="com.pojo"/>
</typeAliases>
<!--进行mapper映射文件的register-->
<mappers>
<!--在spring-mybatis.xml中配置了MapperScannerConfigurer,这里就不去进行mapper的编写-->
</mappers>
</configuration>
4:spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--spring-mybatis整合,需要将sqlSessionFactory注入进来and数据库连接池and引入数据库的配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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 = "maxActive" value = "${maxActive}"/>
<!-- 初始化连接数量 -->
<property name = "initialSize" value = "${initialSize}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name = "maxWait" value = "${maxWait}"/>
<!-- 最小空闲连接数 -->
<property name = "minIdle" value = "${minIdle}"/>
</bean>
<!--使用spring+mybatis的环境下,我们需要配置一个sqlSessionFactoryBean来顶替sqlSessionFactory
在基本的mybatis使用中,sqlSessionFactory可以使用sqlSessionFactoryBuild对象调用它的build方法进行创建
而在mybatis-spring中,则使用sqlSessionFactoryBean来创建
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--为什么需要装配dataSource?
因为在你学习mybatis的时候,environment标签中不是配置了数据库和连接池吗
后来mybatis-config配置文件通过形参的方式传递给了build()方法创建了SqlSessionFactory,
现在mybatis-config配置文件中并没有配置environment
因此这里创建sqlSessionFactory需要添加这两个!!!
-->
<property name="dataSource" ref="dataSource"/>
<!--读取mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--加载mapper文件-->
<property name="mapperLocations" value="classpath:../*Mapper.xml"/>
</bean>
<!--spring与mybatis整合,自动扫描dao包,将mapper接口生成的代理实现注入到spring的IOC容器中-->
<!--spring-mybatis包下的MapperScannerConfigurer的作用是取代手动的去mybatis-config.xml
文件里在<mappers>标签下进行映射文件的注册 ,自动扫描完成代理
-->
<!--还是不明白为什么要在MapperScannerConfigurer中去注入sqlSessionFactory,
可能是创建接口的代理实现需要这个东西吧!!!-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--还记得学习mybatis的时候,<dataSource>标签上的这两个配置吗?
<environment id="development">
<transactionManager type="JDBC"/>
那么现在就要配事务管理
-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--然后注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<context:component-scan base-package="com.dao"/>
</beans>
5:spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.service"/>
<!--<bean class="com.kuang.service.impl.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"></property>
</bean>-->
</beans>
6:springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1:配置组件扫描-->
<context:component-scan base-package="com.controller"/>
<!--2:配置使用默认的servlet处理静态资源,不让springmvc处理静态资源,
不然静态资源会在web.xml中被过滤掉-->
<!---->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--3:配置注解驱动,这个帮我们省去了配置处理器和适配器的麻烦-->
<mvc:annotation-driven/>
<!--4:配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7: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">
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--1:配置springmvc的前端控制器dispatcherServlet-->
<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:applicationContext.xml</param-value>
</init-param>
<!--配置延迟加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--/:匹配所有请求,不包括.jsp类型的后缀-->
<!--/*:匹配所有请求,包括.jsp类型的后缀-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--2:配置字符编码前端过滤器-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3:设置session的过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>