在成功搭建OA-portal模块后,现在让我们一鼓作气完成OA-system模块的SSH整合.
Dao层整合
由于将spring和hibernate整合之后,就不需要hibernate的配置文件了,所以并不需要在OA-system-dao中添加hibernate.cfg.xml.
下面我们在OA-system-service的src/main/resources目录下新建一个spring文件夹,然后在该文件夹下新建一个applicationContext-dao.xml文件,如下图所示。
我们在applicationContext-dao.xml文件当中配置数据库连接池、sessionFactory(hibernate的连接工厂)、实体类扫描器,配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties"/>
<!-- 配置hibernateTemplate,用于持久层 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- hibernate参数设置 -->
<property name="hibernateProperties">
<props>
<!-- 数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 显示sql语句-->
<prop key="hibernate.show_sql">true</prop>
<!-- 格式化SQL语句 -->
<prop key="hibernate.format_sql">true</prop>
<!-- create:根据映射关系生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!-- 使用注解后,用该方式指定实体类的包 -->
<property name="packagesToScan">
<array>
<value>com.QEcode.OA.pojo</value>
</array>
</property>
</bean>
<!-- 数据库连接池 -->
<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="${jdbc.maxActive}" />
<property name="minIdle" value="${jdbc.minIdle}" />
</bean>
</beans>
从中可以看到我们配置数据库连接池配置的是Druid连接池,Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。Druid已经在阿里巴巴部署了超过600个应用,经过多年生产环境大规模部署的严苛考验。
从applicationContext-dao.xml文件当中还可看出数据库的配置直接读取的是配置文件,因此我们需要在classpath(src/main/resource)目录下新建一个properties文件夹,然后在该目录下新建一个db.properties文件,如下图所示。
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/oa?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456 jdbc.maxActive=10 jdbc.minIdle=5 |
除此之外,我们还要创建一个包com.QEcode.OA.pojo来存放实体类,所以我们应在OA-system- pojo中创建这个包.
并且还要把system工程所需要的实体类粘贴到这个包中.
在导入实体类后发现,实体类都报错了.
这是因为我们使用注解的方式来配置实体类,所以我们现在要在pom.xml中添加hibernate的依赖.
<dependencies>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
</dependencies>
最后,我们需要在OA-system-service工程中的pom.xml中添加pojo的依赖.现在pom.xml的内容如下.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.QEcode</groupId>
<artifactId>OA-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>OA-system-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.QEcode</groupId>
<artifactId>OA-system-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
</project>
Service层整合
我们需要在OA-system-service工程的src/main/resources/spring目录下新建一个applicationContext-service.xml文件,如下图所示。
applicationContext-service.xml文件的内容如下所示,可以看到我们配置包扫描器,扫描com.QEcode.OA.controller包。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 配置spring容器创建时要扫描的包 -->
<context:component-scan base-package="com.QEcode.OA.service"></context:component-scan>
</beans>
service一般是由接口和实现类组成,因此我们需要先新建接口所在的目录,我们把它放在OA-system-interface工程下,接口的实现类放在OA-system-service工程下,我们在OA-system-interface工程的src/main/java目录下新建com.QEcode.OA.service包,在OA-system-service工程的src/main/java目录下新建com.QEcode.OA.service.impl包,如下图所示。
service需要配置事务,以确保数据的安全性,下面我们来配置事务,由于我们把hibernate与spring整合,所以事务也交给spring管理,使用spring的AOP来配置事务.在OA-system-service工程的src/main/resources/spring目录下新建applicationContext-trans.xml文件
applicationContext-trans.xml文件的内容如下所示。其中事务的传播行为需要说明一下,当接口名以save、insert、add、create、delete、upate开头时spring会帮我们开启事务,REQUIRED表示如果当前session有事务则加入,如果没有则创建一个事务,而find、select、get开头的接口是查询,不涉及更改数据库,因此设为real-only。下面再说说切面,也就是事务的作用范围,execution(* com.QEcode.OA.service.*.*(..))的意思是,com.QEcode.OA.service包下的任意类的任意方法的任意参数及任意返回值都是事务的切入点。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</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.QEcode.OA.service.*.*(..))" />
</aop:config>
</beans>
在配置完以上spring配置文件后,不要忘了要在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>OA-system-service</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>
</web-app>
至此,我们的Service层便整合完了.最后一个要整合的就是表现层了.
表现层整合
我们在OA-system-web工程的src/main/resource目录下新建一个spring文件夹,在该目录下新建一个applicationContext-web.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="com.QEcode.OA.controller" />
</beans>
此外还需要在src/main/resource目录下新建一个struts目录,在该目录下新建struts.xml文件.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置Struts2常量 -->
<!-- 禁用动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- 开启开发模式,只在项目开发阶段配置 -->
<constant name="struts.devMode" value="true" />
<!-- 配置访问后缀为action -->
<constant name="struts.action.extension" value="action"/>
<!-- 把主题配置成simple -->
<constant name="struts.ui.theme" value="simple" />
</struts>
现在将jsp页面导入OA-system-web工程中.
上面所有的jsp页面都可以在我的github项目下载
下面我们需要在OA-system-web工程下的web.xml文件中配置一下编码和前端控制器,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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>OA-system-web</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>
<!-- 解决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>
<!-- 加载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>
<!-- struts2的核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
至此,OA-system工程已经整合完毕了,不过还有一件事,那就是配置Tomcat.而且不仅OA-system-web需要发布到Tomcat中,OA-system-service也需要发布到Tomcat中,如果不知道如何配置Tomcat的可以参考上一篇博客:
SSH项目实战OA-搭建portal工程
添加两个Tomcat ,OA-system-service,OA-system-web
记录Tomcat的端口
===============================================================================================
在写博客的时候,可能在项目中有一些问题没有被发现,在我修改后,忘记写到博客上,所以我将这个项目上传到github上,大家可以在github上获取项目的代码
下面是github地址,大家Fork and Star