您的位置:首页 > 数据库

newstyles项目实战(五)SSM框架整合

2018-03-09 16:51 441 查看
    本项目是基于SSM框架的,前面家里好了数据库,实现了数据库映射需要的mapper和pojo类等,同时,也在最开始将项目的依赖关系使用maven实现了很好的管理,这里呢,需要进行spring,springmvc,mybatis的框架的整合,这个需要结合本专栏文章的第一个篇,工程的建立,因为虽然为框架的整合,但是整合同时和项目的类型是分不开的,界限来先进行框架整合的分析:

     首先配置dao层:

     Dao层使用了mybatis框架,所以应当按照mybatis框架的要求来整合此部分,首先,我们需要将mybaits逆向工程生成的文件分别转移到newstyles-pojo和newstyles-mapper项目中的src/mian/java目录下,因为在工程真正发布的时候,这两个是要打包成一个jar文件的,所以在调试编译的时候应该建立的是一个jar文件的maven工程,这个在第一篇文章中有所体现了。

因为项目需要打包,所以一般的配置文件等,不能放置在这样的工程里面,因为一旦被封装后,就无法读取配置文件了,这样,建立为jar的工程中就不能够存放类似于sqlMapperConfig.xml之类的文件了。同样的建立为pom的聚合工程则也不能够存放这些文件,因为pom工程类似于一种管理的作用,里面不存放代码,所以,类型为pom的聚合工程也不能够存放以上的配置文件了。这样只剩下一个war类型的maven工程newstyles-mannager-web了,这个里面可以的,因为tomcat实现了一个加载器,可以直接加载读取对应的配置文件,实现了对应的配置。所以我们在src/main/resources文件夹下面加几个文件夹,存放对应的配置文件,因为在tomcat读取封装的war包时,可以读取文件夹下面的内容。



首选,整合Dao时,需要Mybatis框架,使用这个框架对应的就会有一个对应的关于Mybatis配置文件,可以命名为sqlMapperConfig.xml,这个文件中不需要配置数据源等,因为数据源等需要转交给spring,实现了由spring来管理。也就是说这个文件里面基本上不配置什么,但是这个文件需要有,因为这个使用Mybatis所必须的文件。

<?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>
     其次,需要整合spring框架了,这个框架主要用来存储dao层和service生成的bean,而springmvc则管理cotroller生成的bean,这个在下面会介绍一下。接下来需要配置spring的配置文件了。
首先需要配置的是Dao的配置文件,其名称为applicationContext-dao.xml:

<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/*.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" />
&
4000
lt;/bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.newstyles.mapper" />
</bean>
</beans>
可以看到,数据库的连接是在spring的dao层的配置文件中继续宁配置的,也可发现,我们将数据库连接的内容和xml文件分离了,单独分离出来一个properties文件,这样便于修改,对应的文件为:db.properties;内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/newstyles?characterEncoding=utf-8
jdbc.username=root
jdbc.password=**********
   同时在下面的配置中通过使用包扫描的方式,实现了让spring来管理mybatis,加载了sqlMapperConfig.xml。主要是通过生成单例的sqlSessionFactory来对数据源进行管理。

Service层整合:

这一层主要是实现业务逻辑和事务的管理,所以在这一层,我们应指定扫描包,扫描service包,为什么这么做,下面将会解释。在这里我们需要配置事务的管理,会扫描@Controller、@Service、@Repository、@Compnent:

<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:component-scan base-package="com.newstyles.service"></context:component-scan>

</beans>

在这里将sevice层的bean也交由spring进行管理,也就是说spring管理Dao层和Service层的bean;对于事物的管理我们单独将其从service的配置文件中拿出来进行配置,便于分开管理和修改;事务的配置如下:

<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.newstyles.service.*.*(..))" />
</aop:config>

</beans>
     其中在里面实现了对某些方法的切面的控制,事务的管理主要使用的是DatasourceTransactionManager来进行管理的。

表现层配置:

表现层我们使用的是springmvc作为变现层,所以应使用springmvc的配置文件进行配置,因为前面讲到,我们要把controller放到springmvc的容器中,所以,在配置springmvc扫描包的时候将定位到controller包,这里存在一个父容器和子容器的概念:



在这两个容器中,spring容器可以认为是父容器,springmvc容器为子容器,子容器可以访问父容器中的内容,而父容器不可以访问子容器中的内容,有些人会问,在开发的时候完全可以用一个容器呀,没必要用两个呀,是的,当我们想快速的开发出某一个产品,我们可以只使用springmvc框架,然而,当我们扩展业务,或者时维护呢,都放在一个容器中显得优点杂,所以,借用这两个,将dao和service于controller进行分离,分别管理,这样既满足业务变化的需要,也能够方便管理内容;这也就说明了什么我们在配置service层,或者是dao层的时候要指定扫描的包,而不是全部扫描的一个原因,分开扫描可以分别使用两个容器进行管理。springmvc文件配置如下:当然,不要忘了添加springmvc的注解驱动。
<?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.newstyles.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>

<!-- 		由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标签: -->
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

</beans>
上面我们定义了两个mvc的resource标签,因为web中配置的servlet的拦截url为“/”,所以,同样的静态资源等都会被拦截,为了能够正常的显示创建的网页,我们需要能正常的加载。
接下来看一下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_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>newstyles-manager</display-name>
<welcome-file-list>
<welcome-file>login.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>
<!-- <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>newstyles-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>newstyles-manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

  在这里实现了通过web.xml加载我们上面配置好的xml文件,同时加载了springmvc的servlet以及对应的配置,可以看到,配置的url-patten为/。同时加载了前端控制器和解决Post乱码问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息