前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SSM整合

SSM整合

作者头像
爱撒谎的男孩
发布2019-12-31 15:03:59
6980
发布2019-12-31 15:03:59
举报
文章被收录于专栏:码猿技术专栏

文章目录

  1. 1. Spring + SpringMVC + Mybatis整合
    1. 1.1. 依赖
    2. 1.2. 配置数据库连接信息 — db.properties文件
    3. 1.3. Mybatis和Spring整合 — spring-dao.xml
    4. 1.4. Spring与SpringMVC不需要整合
    5. 1.5. 业务层的配置文件 – spring-service.xml
    6. 1.6. 配置 web.xml 文件
    7. 1.7. 包的结构

Spring + SpringMVC + Mybatis整合

依赖

代码语言:javascript
复制
<!-- SpringMVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- MyBatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>

		<!-- MyBatis-Spring 整合jar包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- MySQL驱动jar包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.28</version>
		</dependency>

		<!-- DBCP连接池 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- 添加jackson,自动转换为JSON数据 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.4</version>
		</dependency>

		<!-- 添加jstl标签库 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>


		<!-- 导入aspectj依赖 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.13</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.13</version>
		</dependency>

		<!-- 导入spring的aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- 添加文件上传的依赖 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

配置数据库连接信息 — db.properties文件

代码语言:javascript
复制
url=jdbc:mysql://localhost:3306/db_blog3?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=2
maxSize=10

Mybatis和Spring整合 — spring-dao.xml

  • 读取db.properties文件配置DBCP连接池创建数据源
  • 使用上面的数据源配置SqlSessionFactoryBean
  • 配置组件扫描Mybatis的接口mapper的包,用于创建mapper接口对象
  • 配置批量扫描Mybatis的XXMapper.xml文件的MapperScannerConfigurer
  • 配置事务管理器
代码语言:javascript
复制
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 用于自动创建mapper包下的所有接口的对象,否则将不能使用@Resource注解的方式注入接口对象 -->
	<context:component-scan base-package="cn.tedu.blog.mapper" />
	
	<!-- 配置MapperScannerConfigurer -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 用于配置持久层接口在哪里,指定mapper的包 -->
		<property name="basePackage" value="cn.tedu.blog.mapper" />
	</bean>
	
	<!-- 加载db.properties,其中定义了数据库的配置信息 -->
	<util:properties id="dbConfig" location="classpath:db.properties" />

	<!-- 数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="#{dbConfig.url}" />
		<property name="driverClassName" value="#{dbConfig.driver}" />
		<property name="username" value="#{dbConfig.user}" />
		<property name="password" value="#{dbConfig.password}" />
		<property name="initialSize" value="#{dbConfig.initSize}" />
		<property name="maxActive" value="#{dbConfig.maxSize}" />
	</bean>


	<!-- 配置SqlSessionFactoryBean -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 用于配置数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 用于配置持久层映射文件在哪里,所有的xml文件,使用通配符 -->
		<property name="mapperLocations" value="classpath:mappers/*.xml" />
	</bean>
	

	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源,这里使用的是上面配置好的DataSource -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 开启事务注解 ,transaction-manager指定的是上面配置的事务管理器的id-->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Spring与SpringMVC不需要整合

  • 我们只需要创建一个springMVC.xml文件即可
  • 扫描controller包下,组件扫描,自动创建对象
  • 配置视图解析器
  • 配置注解驱动
  • 配置拦截器
  • 配置上传文件的解析器
  • …………………
代码语言:javascript
复制
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 自动创建对象 -->
	<context:component-scan base-package="cn.tedu.blog.controller" />

	<!-- 配置ViewResolver视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 配置驱动,用于@ResponseBody的使用 -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 配置拦截器的路径 -->
			<mvc:mapping path="/blogger/*" />
			<mvc:mapping path="/blogType/*" />
			<!-- 配置不拦截的路径 -->
			<mvc:exclude-mapping path="/blogger/showLogin.do"></mvc:exclude-mapping>
			<mvc:exclude-mapping path="/blogger/login.do"></mvc:exclude-mapping>
			<mvc:exclude-mapping path="/blogger/showInfo.do"></mvc:exclude-mapping>

			<bean class="cn.tedu.blog.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	
	<!-- 上传组件的解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 上传文件大小 -->
		<property name="maxUploadSize" value="10000000"></property>
		<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
		<property name="defaultEncoding" value="utf-8"></property>
	</bean>

</beans>

业务层的配置文件 – spring-service.xml

  • 配置组件自动扫描业务层的类,自动创建对象
代码语言:javascript
复制
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 -->
	<context:component-scan base-package="cn.tedu.blog.service" />

</beans>

配置 web.xml 文件

  • 配置POST中文乱码过滤器
  • 配置Spring的监听器ContextLoaderListener,用于在容器启动的时候就加载spring的配置文件
  • 配置SpringMVC的前端控制器DispatcherServlet,其中指定的是springMVC的配置文件springMVC.xml
代码语言:javascript
复制
<!-- 解决POST提交方式的中文乱码的过滤器 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置SpringMVC的前端控制器 -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- springmvc的配置文件 -->
			<param-value>classpath:springMVC.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<!-- 使用ContextLoaderListener配置spring的监听器,主要是在启动的时候加载spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
			<!-- 指定所有的spring配置文件,这里使用 * 通配符 -->
			<param-value>classpath:spring-*.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

包的结构

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring + SpringMVC + Mybatis整合
    • 依赖
      • 配置数据库连接信息 — db.properties文件
        • Mybatis和Spring整合 — spring-dao.xml
          • Spring与SpringMVC不需要整合
            • 业务层的配置文件 – spring-service.xml
              • 配置 web.xml 文件
                • 包的结构
                相关产品与服务
                云数据库 MySQL
                腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档