做OA这个项目有一段时间了,下面我来讲讲OA这个项目的架构。OA这个项目整体采用SSH来实现的,即spring、struts、hibernate。在这个架构中,我们需要通过hibernate实现数据持久化,通过hibernate维护实体之间的依赖关系;spring实现容器管理,管理各个bean,这里我们通过注解的方式实现容器注入,容器管理;采用上struts实现对于业务的管理.在项目架构的过程中需要实现的spring和hibernate的融合,spring和struts融合。
一、spring和hibernate融合:
在applicationContext中设置数据库连接(本地使用):
<span style="font-size:14px;"><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置C3P0连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSourc">
<property name="jdbcUrl" value=""></property>
<property name="driverClass" value=""></property>
<property name="user" value=""></property>
<property name="password" value=""></property>
<!-- 其他配置 -->
<!-- 初始化时获取是哪个连接,取值印在minpoolSize和maxPoolSize之间,Default:3 -->
<property name="initialPoolSize" value="3"/>
<!-- 连接池中保存的最小连接数,default:3 -->
<property name="minpoolSize" value="3"/>
<!-- 连接池中保存的最大连接数,default:3 -->
<property name="maxPoolSize" value="3"/>
<!-- 当连接池中的连接耗尽的时候,C3P0一次同事获取的连接数,Default:3 -->
<property name="acquireIncrement" value="3"/>
<!-- 控制数据源内加载的PreparedStatements数量,如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭 -->
<property name="maxStatements" value="8"/>
<!-- maxStatementsPerConnetion定义了连接池内单个连接所拥有的最大缓存statements数,default:0 -->
<property name="maxStatementsPerConnetion" value="3"/>
<!-- 最大空闲时间,1800秒内未被使用则连接被丢弃,default:0 -->
<property name="maxIdleTime" value="1800"/>
</bean>
</property>
</bean> </span>
好处:实现容器管理sessionfactory对象,是我们的sessionfactory是一个单例对象,在使用的时候实现的对象的一致性。才外可以使用声明式事务,只要写一个注解就能开启事务,非常简便!
二、spring和struts的融合,实现实现容器管理action
1.在web.xml添加对applicationContext的核心监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
2.添加jar包:Struts2-spring-plugin-2.1.8.1.jar
三、在架构的时候需要注意的小细节:
(1)注意文件扫描范围
<span style="font-size:14px;"> <context:component-scan base-package="cn.itcast.oa"></context:component-scan></span>
(2)因为spring和hibernate融合后,sessionFactory交给容器管理,所以在字符集设置应该写在数据库连接文件jdbc.properties
jdbcUrl = jdbc\:mysql\:///itcastoa11?useUnicode\=true&characterEncoding\=UTF-8