给自己留个SSM整合的纪念

在这里插入图片描述
虽然说现在大部分的项目已经使用上了SpringBoot,分布式也有Spring Cloud,Dubbo。SSM快要消失在历史的长河,但终究还是有存活于世的幸存者,在此,仅以总结一下SSM的配置来纪念一下逝去的岁月。

1、项目环境搭建

要进行SSM整合的话,首先你得有一个项目的构架,先创建Maven的web工程,此处就不做演示了。

2、整合Spring框架

2.1、在pom.xml文件中,引入Spring的依赖jar包

该依赖包含了SpringMVC开发时的核心类,也有Spring的核心jar文件

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>

该jar文件包含对Spring对JDBC数据访问进行封装的所有类

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>

提供对AspectJ的支持

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>

2.2、在resources/spring目录下,创建spring的核心配置文件:applicationContext.xml

mybatis和spring文件夹是我自己创建的
在这里插入图片描述
配置内容如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">




</beans>

在这里插入图片描述

3、整合SpringMVC框架

因为前面在添加spring的jar包同时, 将springmvc的jar包也引入了,所以这里不再引入springmvc的jar包。

3.1、创建springmvc的核心配置文件

在resources/spring目录下,创建springmvc的核心配置文件:springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/context
          				http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) -->
	<mvc:default-servlet-handler/>
	
	<!-- 2.配置注解驱动,用于识别注解(比如@Controller-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类,
		如果扫描到的类上有 @Controller@Service@Component等注解,
		将会自动将类注册为bean 
	 -->
	<context:component-scan base-package="com.aaa.controller">
	</context:component-scan>
	
	<!-- 4.配置内部资源视图解析器
		prefix:配置路径前缀
		suffix:配置文件后缀
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	
</beans>

3.2、在web.xml中配置springmvc

<!-- 配置springmvc, 将所有请求交给springmvc来处理 -->
<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 配置springmvc核心配置文件的位置,默认Springmvc的配置文件是在WEB-INF目录下,默认的名字为springmvc-servlet.xml,如果要放在其他目录,则需要指定如下配置:
		-->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/*.xml</param-value>
	</init-param>		
</servlet>
<!-- 其中的斜杠(/)表示拦截所有请求(除JSP以外), 所有请求都要经过springmvc前端控制器 -->
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

3.3、配置springmvc乱码处理过滤器

<!-- 乱码处理过滤器 -->
<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>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<!-- 指定拦截方式为拦截所有请求 -->
	<url-pattern>/*</url-pattern>
</filter-mapping>

在这里插入图片描述

4、整合mybatis框架

4.1、在pom.xml文件中,引入mybatis及相关依赖包

<!-- 整合mybatis框架 -->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.2.8</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
	<version>1.2.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.32</version>
</dependency>
<!-- druid连接池 -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.1.6</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
</dependency>    
<!--javaScript标签库-->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>

4.2、创建mybatis的核心配置文件

在resources/mybatis目录下,创建mybatis的核心配置文件: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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置开发环境 -->
	<environments default="develop">
		<!-- 这里可以配置多个环境,比如develop,test等 -->
		<environment id="develop">
			<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/xxdb?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
	<mappers>
		<mapper resource="mybatis/mapper/xxMapper.xml"/>
	</mappers>
</configuration>

4.3

创建封装信息用的实体类pojo,在mapper目录下创建映射问价xxmapper.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 门店表的映射文件	namespace值为对应接口的全路径 -->
<mapper namespace="com.aa.dao.xxMapper">
	<!-- 1.查询所有门店信息,id值为对应接口中方法的名字
		resultType指定将查询的结果封装到哪个pojo对象中
	 -->
	<select id="findAll" resultType="com.aa.pojo.pojo">
		select * from xx_door
	</select>
	
	

</mapper>

4.4、在mybatis的全局配置文件中引入xxrMapper.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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置开发环境 -->
	...
	<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
	<!-- 配置映射文件 -->
	<mappers>
		<mapper resource="mybatis/mapper/xxMapper.xml"/>
	</mappers>
	
</configuration>

在这里插入图片描述

5、整合spring和mybatis

5.1、修改mybatis-config.xml文件,将连接池等配置移除,在spring中配置

<?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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置开发环境 -->
	<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
	<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
	<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
	
</configuration>

5.2、在applicationContext.xml中配置druid连接池

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 1.加载jdbc.properties文件的位置 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<!-- 配置连接数据库的基本信息 -->
		<property name="driverClassName" value="${db.driverClassName}"></property>
		<property name="url" value="${db.url}"></property>
		<property name="username" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
	</bean>
	
	<!-- 3.整合spring和mybatis框架	
		将SqlSession等对象的创建交给Spring容器
		id值(sqlSessionFactory)是固定值
	 -->
	<bean id="sqlSessionFactory" 
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 3.1.指定mybatis核心配置文件的位置 -->
		<property name="configLocation" 
				value="classpath:mybatis/mybatis-config.xml"></property>
		<!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
	</bean>
	
	<!-- 4、定义mapper接口扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 -->
		<property name="basePackage" 
			value="com.aa.dao"/>
	</bean>
	
	<!-- 5.配置需要扫描的包(service层):spring自动去扫描 base-package下的类,
		如果扫描到的类上有 @Controller@Service@Component等注解,
		将会自动将类注册为bean(即由spring创建实例)
	 -->
	<context:component-scan 
		base-package="com.aa.service">
	</context:component-scan>

</beans>

5.3、在resources目录下创建jdbc.properties文件,将连接数据库的基本信息提取到文件中

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///xxdb?characterEncoding=utf-8
db.username=root
db.password=root

在这里插入图片描述
再往下就是业务了,哈哈哈
在这里插入图片描述
如有问题的话,请提出,一起学习一起进步。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值