SSM框架的整合(Spring,SpringMVC,MyBatis)
1、导入spring的包以及springmvc的包以及json包的支持
2、创建web工程(项目编码–>jdk–>Tomcat的依赖–>项目工程结构的改变(web.xml))
3、web.xml中添加Springmvc的核心
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、编写applicationcontext-springmvc.xml配置文件
(1)添加对注解的扫描
<!-- 对注解的扫描 -->
<context:component-scan base-package="com.hqyj"></context:component-scan>
(2)资源视图解析
<!-- 资源视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/page/"></property>
<!-- 后缀s -->
<property name="suffix" value=".jsp"></property>
</bean>
(3)静态资源的处理
<!-- 静态资源的处理 -->
<mvc:default-servlet-handler/>
<!-- mvc注解的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
5、整合spring和mybatis
(1)引入相关包:
mybatis的包、数据库驱动包、对事务的支持包(aspectj的支持(AOP))、数据库连接池技术
(2)引入mybatis对spring框架整合的jar包:
<!-- 引入mybatis对spring框架整合的jar包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
6、编写applicationcontext-dao.xml配置文件(就是mybatis进行连接)
这里我们默认创建了mybatis的全局配置文件mybatis-config.xml 、数据库配置文件db.properties 与映射文件mapper.xml
mybatis-config.xml文件里只需要写一些系统设置与别名,其他的全都交由spring来管理
(1)在web.xml中配置监听器,加载mybatis-spring的配置文件(applicationcontext-dao.xml) :
<!-- 配置监听器,加载mybatis-spring的配置文件 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext-dao.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>
下面的操作都在applicationcontext-dao.xml配置文件中进行
(2)加载数据库配置文件:
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
(3)配置数据库连接池:
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxIdleTime" value="30"></property>
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="10"></property>
<property name="maxStatements" value="200"></property>
</bean>
(4)配置SqlSessionFactory (加载mybatis全局配置文件):
<!-- 配置SqlSessionFactory 加载mybatis全局配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 单独的必须属性,就是JDBC的DataSource -->
<property name="dataSource" ref="dataSource" />
<!-- 应用mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 装载所有的mapper映射文件 -->
<property name="mapperLocations" value="classpath*:mappers/**/*.xml"></property>
</bean>
(5)扫描所有接口,应用为代理对象:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hqyj.dao"></property>
</bean>
(6)配置事务管理器与对事务管理器注解的支持:
<!-- 配置事务管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置对事务管理器注解的支持,默认id为transactionManager -->
<tx:annotation-driven transaction-manager="transactionManager"/>
到这里,三大框架的搭建就到此结束,这样就可以创建bean层,dao层,service层,action层,service层和action层添加spring注解并使用自动填充,再在action层返回数据或页面