前言
上一篇博客中,我们讲了 如何搭建项目框架结构。搭建好了最基本的目录结构后,那么我们就可以对 SSM 进行整合了,接下来具体介绍下如何进行整合。
DAO 层整合
在 src/main/resources /spring 下创建 spring-dao.xml,用于整合 dao 层,接下来一步一步介绍如何整合。
数据库相关配置
首先准备一个数据库配置文件 jdbc.properties,放在 src/main/resources 目录下,配置数据库相关参数;
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8jdbc.username=用户名jdbc.password=用户密码
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- 1.引入数据库配置信息 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 2.数据库连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 2.1连接池属性 --><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><!-- 2.2 c3p0 独有属性 --><property name="maxPoolSize" value="30"/><property name="minPoolSize" value="10"/><!-- 关闭连接后不自动 commit --><property name="autoCommitOnClose" value="false"/><!-- 连接超时时间 --><property name="checkoutTimeout" value="10000"/><!-- 连接失败后重试次数 --><property name="acquireRetryAttempts" value="2"/></bean></beans>
配置 SqlSessionFactory 对象(MyBatis)
在 src/main/resources 下创建 MyBatis 配置文件 mybatis-config.xml,然后再在 spring-dao.xml 中配置 SqlSessionFactory;
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><!-- 使用 jdbc 的 getGeneratedKeys 获取数据库自增主键值 --><setting name="useGeneratedKeys" value="true"/><!-- 使用列别名替换列名 默认:true --><setting name="useColumnLabel" value="true"/><!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} --><setting name="mapUnderscoreToCamelCase" value="true"/></settings></configuration>
<!-- 3. 配置 SqlSessionFactory 对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 3.1 注入数据库连接池 --><property name="dataSource" ref="dataSource"/><!-- 3.2 配置 MyBatis 全局配置文件 --><property name="configLocation" value="classpath:mybatis-config.xml"/><!-- 3.3 扫描 pojo 包 --><property name="typeAliasesPackage" value="com.cunyu.pojo"/><!-- 3.4 扫描 sql 配置文件(mapper 中的 xml 文件) --><property name="mapperLocations" value="classpath:mapper/*.xml"/></bean>
组件扫描
<!-- 4. 扫描 dao 接口包,实现 dao 接口动态注入 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 4.1 注入 SqlSessionFactory --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- 4.2 扫描 dao 包 --><property name="basePackage" value="com.cunyu.dao"/></bean>
service 层整合
在 src/main/resources/spring 下新建 spring-service.xml,用于整合 service 层,主要做的事情是有如下几点:
- 扫描
service包下所有带有注解@Service的类 - 配置事务管理器
- 配置基于注解的声明式事务
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 1. 扫描 service 包 --><context:component-scan base-package="com.cunyu.service"/><!-- 2. 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 2.1 注入数据库连接池 --><property name="dataSource" ref="dataSource"/></bean><!-- 3. 配置基于注解的声明式事务 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>
controller 层整合
既然已经整合了 dao 和 service 层,那怎么能少下 controller 层呢。在 src/main/resources/spring 下新建 spring-service.xml ,controller 层的整合需要做的事情主要有如下几点:
- 开启 Spring MVC 的注解模式,毕竟现在大多都是基于注解开发了
- 对静态资源如图片、样式和脚本等进行处理
- 配置视图解析器;
- 扫描
controller层
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--1. 开启 Spring MVC 注解模式--><mvc:annotation-driven/><!-- 2. 静态资源处理 --><mvc:default-servlet-handler/><!-- 3. 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="suffix" value=".jsp"/><property name="prefix" value="WEB-INF/jsp/"/></bean><!-- 4. 扫描 controller 包 --><context:component-scan base-package="com.cunyu.controller"/></beans>
配置 web.xml
web.xml 中主要就是配置 DispatcherServlet ,同时加载 Spring MVC 的配置文件;
<?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"><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置 Spring MVC 需要加载的配置文件spring-dao.xml -> spring-service.xml -> spring-web.xml--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-*.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>
总结
经过上面的配置之后我们的 SSM 框架就被完全整合了,接下来就可以进行业务逻辑的开发。此外,也可以将此框架用于其他项目的开发,只需要进行细微的修改即可。整合过程总结下来就是如下的顺序:
- 项目框架结构搭建;
- 整合 dao 层
- 整合 service 层
- 整合 controller 层
- 配置 web.xml
