SSM框架整合

一、用到的技术

        Spring


       SpringMvc


       Mybatis





二、整合的方式



      当我们在做项目的时候,从最基本的三层框架着手,一般分为service层,controller层,dao层,那怎么


Mybatis+Spring+SpringMvc这些技术融合到这三层中,更方便的利于我们的开发和实现呢?





三、具体的实现方式



      大家都知道spring最重要的特性就是AOP和IOC,所以在开发项目的时候,利用Spring的容器对这些包进行管


理,我们在整合这三种技术的时候,基本上都是在配置文件中完成的,下面看看具体是怎么实现的。




Dao层


*使用Mybatis框架,创建SqlMapConfig.xml


*配置数据源,链接数据库


*利用Spring中的容器,管理SqlfessionFactory。


*使用扫描包的方式加载mapper代理对象


<span style="font-family:KaiTi_GB2312;"><?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:resource/db.properties" />
	<!-- 数据库连接池 -->Dao
	<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>
	<!-- 配置sqlsessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
	<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置扫描包,加载mapper代理对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.taotao.mapper"></property>
	</bean>
</beans>
</span>





Service层



*配置事物管理


*将service放到Spring的容器中进行管理



<span style="font-family:KaiTi_GB2312;"><?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>
</span>




表现层


*将controller的实现类放入Spring容器中


*配置注解驱动


*配置视图解析器




SpringMvc.xml


<span style="font-family:KaiTi_GB2312;"><?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" />
	<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>
</span>
</pre><p><span style="font-family:KaiTi_GB2312;"></span></p><p><span style="font-family:KaiTi_GB2312;"></span></p><p><span style="font-family:KaiTi_GB2312;"></span></p><h2 style="margin: 0in; font-size: 10.5pt; color: rgb(58, 56, 56);"><span style="font-family:KaiTi_GB2312;">Web.xml</span></h2><div><span style="font-family:KaiTi_GB2312;"></span></div><div><span style="font-family:KaiTi_GB2312;"></span></div><div><span style="font-family:KaiTi_GB2312;"></span></div><p lang="en-US" style="margin: 0in; color: rgb(58, 56, 56);"></p><pre code_snippet_id="1793040" snippet_file_name="blog_20160729_5_4235143" name="code" class="html" style="font-size: 10.5pt;"><span style="font-family:KaiTi_GB2312;"><?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>
</span>
<span style="font-family:KaiTi_GB2312;">
</span>
<span style="font-family:KaiTi_GB2312;">
</span>


        基本的整合就结束了,最重要的还是利用好Spring中的容器。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值