SSH2集成开发步骤

分布集成三大框架:

第一步:集合三大框架开发的jar包,并放置项目的lib目录下,如下图所示:


这些jar包有一些可有可无,但是对于做一个比较全面的,考虑周全的系统,建议都加载这些jar包,初学者也更不容易出错。

第二步:在自己的项目下新建一个bean.xml(这里面主要是集成配置三大框架)

① 配置数据源:

<!-- 采用的事c3p0数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>

② 将sssionFactory交给spring的事物管理器中,并将数据源注入到sessionFactory中:

<!-- hibernate的sessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
 <property name="mappingResources">
    <list>
      <value>cn/jxau/po/Employee.hbm.xml</value>
    </list>
</property>
 <property name="hibernateProperties">
 <value>
      hibernate.dialect=org.hibernate.dialect.MySQL5Dialect<!-- 使用的Mysql5的方言 -->
      hibernate.hbm2ddl.auto=update<!--是否根据映射源数据,根据数据生成表结构  -->
      hibernate.show_sql=false<!--是否打印出hibernate中的sql语句  -->
      hibernate.format_sql=false<!-- 是否对其sql语句进行格式化 -->
  </value>
 </property>
</bean>


通过这个配置,我们就获得一个spring对外的一个对数据库操作的单例sesionFactory对象。

第三步:配置springhibernate的事物管理器

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

对创建的sessionFactory对象进行事物管理。

第四步:声明事物(这里采用注解的方式进行配置)

<tx:annotation-driven transaction-manager="txManager"/>

第五步:配置PO层映射文件:

首先建立自己对应数据表的PO,并在该类的包下对应建立相应的xml文件,比如PO的名字为Employee.java,那个采用xml映射数据库表的话,就需要建立Employee.hbm.xml,并将这个文件交给spring中的sessionFactory进行管理,配置如下:

<property name="mappingResources">
    <list>
      <value>cn/itcast/po/Employee.hbm.xml</value>
    </list>
</property>

第六步:编写Employee.hbm.xml文件(数据库映射配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.linhao.po">
    <class name="Employee">
       <id name="username" length="20"/>
       <property name="password" length="20" not-null="true"/>
       <property name="sex" not-null="true" length="5">
        	<type name="org.hibernate.type.EnumType">
        	 <param name="enumClass">cn.itcast.po.Gender</param>
<!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字面值到数据库。如果不指定type参数,保存枚举的索引值(从0开始)到数据库-->
        	 <param name="type">12</param>
        	</type>
        </property>
    </class>
</hibernate-mapping>

以上就完成hibernatespring的集成,当然你也可以选择不写数据表的映射文件,利用注解进行配置hibernate,这个注解配置映射我就不多说 了。

第六步:集成好hiberbate和spring,那么进行测试(测试可以用以下例子来进行,personService业务benan自行配置,这里只介绍如何集成三大框架):

以上是已经集成好hibernatespring,但是要注意在编写配置好了之后,用以下代码进行测试:

package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jxau.ruanjian1201.springWeb1.po.Person;
import com.jxau.ruanjian1201.springWeb1.service.impl.PersonService;
import com.jxau.ruanjian1201.springwe1.service.PersonServiceImpl;

public class JunitTest {
	private static PersonServiceImpl personService;
	public static void setUpBeforeClass() throws Exception {
		try {
			ApplicationContext cxt = new ClassPathXmlApplicationContext(
					"beans.xml");
			personService = (PersonServiceImpl) cxt.getBean("personService");
		    System.out.println(personService);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		try {
			setUpBeforeClass();
			personService.save(new Person("linhao"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
第七步:spring和hibernate集成之后,就是和struts2集成

1、Struts2spring集成步骤:

第一步:在测试的时候我们需要实例化spring容器,才能使用spring里面的事物,但是其实spring对外有个监听器,我们只需要在web.xml进行配置spring容器的监听器就可以了。首先在web.xml中实例化spring容器

<!--指定spring的配置文件,服务器启动的时候默认从web根目录寻找配置文件,我们可以通过spring提供的classpath前缀指定从类路径下寻找->

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- 对spring容器进行实例化 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

该监听器会将spring实例化并将其放在application中,如果想得到这个实例,可以通过application进行得到

2、配置struts2jar包:下载好struts2的压缩包后解压,在其lib目录下所有的不带-plgin结尾的jar文件,但是除了struts2-spring-plugin-2.0.11.1.jar(上面已经给出所需的所有jar包)

3、第三步,利用filterweb.xml中启动struts2,配置如下:

 

  <filter>
        <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
   </filter-mapping>

4、配置struts2的配置模板struts.xml,我这里列举了一些配置选项,对开发的是否更有帮助:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!--指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法  -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!--该属性指定需要的Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理
       如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开     -->
       <constant name="struts.action.extension" value="do"/>
    <!--设置浏览器是否缓存静态内容,默认值是true(生产环境下使用),开发阶段最好关闭  -->
    <constant name="struts.serve.static.browserCache" value="flase"/>
    <!--当struts的配置文件修改后,系统是否自动从新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开  -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!--开发环境下使用,这样可以打印出更详细的错误信息  -->
    <constant name="struts.devMode" value="true"/>
<!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- struts2和spring关键切入点,这个action是声明由spring帮忙创建 -->
 <constant name="struts.objectFactory" value="spring" />
 <package name="person" namespace="/person" extends="struts-default">
 <global-results>
 <result>/WEB-INF/view/message.jsp</result>
 </global-results>
 <!--使用通配符,进行视图的转换  -->
 	<action name="action_*" class="personOperateAction" method="{1}"><!--此class的名字是你配置在spring中的bean的id名字,必须在beans.xml中或者利用注解进行以来注入  -->
 	 <result name="list">/WEB-INF/view/person.jsp</result>
 	 <result name="add">/WEB-INF/view/person_add.jsp</result>
 	 <result name="edit">/WEB-INF/view/person_edit.jsp</result>
 	</action>
 </package>
</struts>
5.完成以上步骤之后就可以进行SSH2集成开发(控制层中的action需要用@Controller声明(或者在beans.xml中配置)这样才能用spring事物管理机制进行管理进行依赖注入)。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值