史上最全SSM框架整合(三)-----SSM框架的整合


SSM框架的整合


前言

      在前面的一博文《SSM框架的实践搭建》中我们详细的介绍了如果一步一步的搭建SSM框架,但是搭建好了,SSM还要进行相关的整合,整个框架才能运行起来。


     我们主要是用配置文件对dao层,Service层以及web层进行整合之后,SSM整个框架就能运行起来了,下面我将结合整合各层的思路、具体的代码和步骤进行讲解如下:



SSM搭建整合源码下载http://pan.baidu.com/s/1i5rCjrf



整合的之后建好的文件图如下:


 




3.1框架整合dao层


    3.1.1整合dao层的思路:

整合mybatis和spring


(1)需要的jar包:
       1、mybatis的jar包
       2、Mysql数据库驱动
       3、数据库连接池
       4、Mybatis和spring的整合包。
       5、Spring的jar包


(2)配置文件:
      1、mybatis的配置文件:SqlMapConfig.xml
      2、Spring的配置文件:applicationContext-dao.xml


(3)数据处理
     1、数据源
     2、数据库连接池
     3、配置SqlSessionFactory(mybatis和spring整合包中的)
     4、配置mapper文件的扫描器。


3.1.2实践整合


SqlMapConfig.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">
<configuration>
</configuration>


Db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=1


applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">


	<!-- 数据库连接池 -->
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:properties/*.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.taotao.mapper" />
	</bean>
</beans>



3.2框架整合Service层


 3.2.1 整合Service层思路:

   (1)使用的jar包:spring的jar包。
   (2)配置文件:applicationContext-service.xml
   (3)配置一个包扫描器,扫描所有带@Service注解的类。
   (4)事务配置:


   配置文件:applicationContext-trans.xml
       1、配置一个事务管理器
       2、配置tx
       3、配置切面


3.2.2实践整合
    applicationContext-service.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!-- 包扫描器,扫描带@Service注解的类 -->
	<context:component-scan base-package="com.taotao.service"></context:component-scan>
	
</beans>


applicationContext-trans.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.taotao.service.*.*(..))" />
	</aop:config>
</beans>



3.3表现层taotao-manager-web


3.3.1整合表现层思想
 (1)使用springmvc,需要使用springmvc和spring的jar包。
配置文件:springmvc.xml
        1、配置注解驱动
        2、配置一个视图解析器。
        3、包扫描器,@Controller注解

(2)Web.xml
    1、配置springmvc的前端控制器
    2、Spring容器初始化的listener。


3.3.2实践整合
配置springmvc.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 配置包扫描器 -->    
    <context:component-scan base-package="com.taotao.controller"></context:component-scan>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 视图解析器 -->
    <bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>  
    

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="taotao" version="2.5">
	<display-name>taotao-manager</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 初始化spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- 解决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>taotao-manager</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>taotao-manager</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


小结

     如果你已经做到了这步,恭喜你,你已经成功了百分之九十了,SSM整个框架的搭建、整合和具体有哪些步骤、jar配置文件都一一的实践过了,现在我们要做的就是对自己搭建和整合好的框架进行一个验证,也就是测试。


  这个我将会在下一篇博文中介绍到,详情请看:

                    http://blog.csdn.net/u013067756/article/details/73810582





 SSM搭建整合源码下载http://pan.baidu.com/s/1i5rCjrf



  • 1
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
首先,需要在项目中添加xxl-sso-client的依赖,可以通过Maven或者手动添加jar包的方式。然后,在Spring配置文件中配置xxl-sso-client的相关信息,如下所示: ``` <!-- 配置xxl-sso-client --> <bean id="xxlSsoClient" class="com.xxl.sso.client.filter.XxlSsoClientFilter"> <property name="serverUrlPrefix" value="${sso.server.url.prefix}" /> <property name="clientId" value="${sso.client.id}" /> <property name="clientSecret" value="${sso.client.secret}" /> <property name="logoutPath" value="${sso.client.logout.path:/logout}" /> <property name="loginPath" value="${sso.client.login.path:/login}" /> <property name="excludes" value="${sso.client.excludes}" /> </bean> <!-- 配置拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.xxl.sso.client.filter.XxlSsoClientInterceptor"> <property name="xxlSsoClient" ref="xxlSsoClient" /> </bean> </mvc:interceptor> </mvc:interceptors> ``` 其中,`${sso.server.url.prefix}`为xxl-sso-server的地址前缀,`${sso.client.id}`和`${sso.client.secret}`为xxl-sso-client的身份识别信息,`${sso.client.logout.path}`和`${sso.client.login.path}`为退出登录和登录的路径,`${sso.client.excludes}`为不需要拦截的路径。 最后,在Controller中添加`@XxlSsoClient`注解,表示该接口需要进行身份认证。 ``` @Controller public class UserController { @XxlSsoClient @RequestMapping("/user/info") @ResponseBody public String userInfo(HttpServletRequest request) { // 获取用户信息 XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(XxlSsoConstant.XXL_SSO_USER); return "user info: " + xxlUser.toString(); } } ``` 以上就是集成xxl-sso到SSM框架中的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值