SSM整合
在博客的前面介绍了mybatis,spring,springmvc的使用,那么这篇博客将介绍将mybatis和spring,springmvc的整合。
整合之前,我们需要明白一个点,spring和mybatis之间进行整合不像spring和springmvc一样是天然一体,所以在中间就需要一个磨合剂,那就是mybatis-spring
这个依赖。
jar包的导入
使用maven进行项目管理
<!--添加Servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--JSTL用于在控制器中将模型绑定到JSP中-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加spring的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--上下文-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--上下文支持包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--添加Spring MVC的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--利用它处理事务问题-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--添加数据库Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--添加Mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
<!--整合Mybatis与Spring的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Mybatis分页依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- 处理时间日期格式 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<!-- 用于MD5加密 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--添加Json依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!--有时候不加可能会报错-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 新添加处理json为java bean -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 数据源的引入, 池化技术 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--阿里巴巴json库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version> 1.2.47</version>
</dependency>
<!--java发送http请求的库-->
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
在这里面导入的jar包有点多,不过可以基本满足我们的需求了。
文件的配置
首先,我们要明确文件的配置从哪里开始,那么肯定是需要最先配置web.xml
了,因为它管理着web项目的配置信息。
-
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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd http://www.springframework.org/schema/mvc/spring-mvc.xsd" version="3.1"> <!--配置ContextLoaderListener,初始化IOC容器 作用就是启动Web容器时,自动装配ApplicationContext的配置信息 经常配合下面<context-param>使用 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <!--classpath其实就相当于resources文件夹--> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--servlet分发--> <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:/spring/springmvc-servlet.xml</param-value> </init-param> </servlet> <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>characterEncodingFilter</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> <!--是否强制设置request的编码为encoding,即UTF-8,默认为false --> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--是否强制设置response的编码为encoding,即UTF-8 --> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!-- 这里必须写 /* --> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
applicationContext.xml文件的配置
在web.xml文件配置中,我们关于上下文文件的引入就只写了一个
applicationContext.xml
,那么其他文件怎么办呢?我们可以在applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--引入spring和其他整合的配置文件 比如spring-mybatis.xml等--> <import resource="classpath:/spring/spring-*.xml"/> </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:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.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"> <!--扫描controller的注解 填写自己的控制器的包 --> <context:component-scan base-package="cc.weno.controller"/> <!--静态资源放行问题--> <mvc:default-servlet-handler/> <!--配置代理--> <mvc:annotation-driven/> <!--配置拦截器--> <mvc:interceptors> <!--配置一个拦截器 可以配置多个,那么拦截顺序则是从上到下 --> <mvc:interceptor> <!--拦截的URI /* 代表拦截这一层的任意字符 /** 代表拦截任意层次的任意字符 --> <mvc:mapping path="/**"/> <!--不进行拦截的uri--> <mvc:exclude-mapping path="/index"/> <mvc:exclude-mapping path="/login"/> <!-- 拦截器的包 --> <bean class="cc.weno.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
在配置mybatis文件之前,我们需要配置一下
jdbc.properties
mysql.driver=com.mysql.jdbc.Driver mysql.url=数据库地址 例如:jdbc:mysql://localhost:3306/test mysql.username=账号 mysql.password=密码 #定义初始连接数 dbcp.initialSize=0 #定义最大连接数 dbcp.maxActive=20 #定义最大空闲 dbcp.maxIdle=20 #定义最小空闲 dbcp.minIdle=1 #定义最长等待时间 dbcp.maxWait=60000
-
spring-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/cache" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--自动扫描包,包括控制层和持久层 --> <context:component-scan base-package="cc.weno"/> <!--引入数据库配置文件--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- value 填写jdbc.properties文件的位置 --> <property name="location" value="classpath:/config/jdbc.properties"/> </bean> <!--配置连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > <property name="driverClassName" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> <!--定义数据库的连接--> <property name="initialSize" value="${dbcp.initialSize}"/> <property name="maxIdle" value="${dbcp.maxIdle}"/> <property name="minIdle" value="${dbcp.minIdle}"/> <property name="maxWait" value="${dbcp.maxWait}"/> <property name="maxActive" value="${dbcp.maxActive}"/> </bean> <!--整合spring和mybatis,就不需要像以前一样写mybati的配置文件了--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mapping.xml文件 注意路径是"/"不是"."--> <property name="mapperLocations" value="classpath:mybatis/*.xml"/> <property name="configuration"> <!--可以将之前mybatis的一些配置项转移到这里来--> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property> </bean> <!-- 扫描DAO持久层接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cc.weno.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--数据库事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
项目结构如图所示
层 | 作用 | 设计 |
---|---|---|
持久层(DAO层) | DAO层负责与数据库联系,进行数据持久化 | 首先设计接口,然后再配置×××mapper.xml文件 |
业务层(Service层) | 负责业务模块的逻辑应用设计 | 先设计接口,再设计实现类,然后调用接口来实现逻辑业务(spring-mybatis.xml文件配置后,直接调用) |
Control层(控制层) | 负责具体模块的控制 | 调用Service层的接口来控制业务 |
DAO层和前面的博客配置没有什么区别,让我们主要来看看Service层的配置。
首先,先定义UserService的接口
public interface UserService {
User getUser(String name);
}
然后进行Service接口的实现,使用@Service标注Service层
@Service("userService")
public class UserServiceImpl implements UserService {
/**
* mybatis的接口
*/
@Resource
private UserMapper userMapper;
// 直接进行使用
@Override
public User getUser(String name) {
userMapper.getUser(name);
}
}
在Controller层中
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "/index")
@ResponseBody
public String index(@RequestParam("name")String name) {
User u =userService.getUser(name);
return u;
}
}
UserMapper.xml文件倒是没有任何要更改的地方,只要别忘了配置<mapper namespace="cc.weno.mapper.UserMapper">
文件就行了。
然后SSM框架基本的整合就到这里了,在这里面代码没几行,配置文件倒是写了一大堆〒_〒【此时好想说:springboot
真香】