首页 > 学院 > 开发设计 > 正文

SSH整合

2019-11-14 09:23:24
字体:
来源:转载
供稿:网友

第一步:引入所需jar 第二步:配置web.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> <!-- Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

classpath:bean*.xml指定路径下的文件 第三步:写指定路径下的文件 bean-action.xml bean-service.xml bean-dao.xml 首先配置bean-dao.xml: Dao配置 <bean id="studao" class="dao.stuDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> 连接池 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:jtds:sqlserver://127.0.0.1:49593/mybysj;charset=gbk;SelectMethod=CURSOR"></property> <property name="user" value="sa"></property> <property name="passWord" value="sa123"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="15"></property> </bean> 配置sessionFactory <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm1ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:hbm/*.hbm.xml</value> </list> </property> </bean> 配置事物 <bean id="txMannger" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> 配置事物增强 <tx:advice id="txadvice" transaction-manager="txMannger"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> 配置aop <aop:config> <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txadvice" pointcut-ref="pt"/> </aop:config> classpath:hbm/*.hbm.xml 指定这个路径下的映射文件 这里配置连接池,sessionFactory,事物及增强,aop切点表达式

第五步:写pojo和*.hbm.xml

public class Student { private String sid; private String name; private Integer age; private String address; public Student(){} public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}

student.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> <class name="entity.Student" table="student"> <id name="sid" column="s_id"> </id> <property name="name" column="s_name" type="java.lang.String"/> <property name="age" column="s_age" /> <property name="address" column="s_address" type="java.lang.String"/> </class></hibernate-mapping>

第六步:写bean-action.xml和bean-service.xml bean-action.xml:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="stuAction" class="action.stuAction"> <property name="stuservice" ref="stuservice"></property> </bean></beans>

bean-service.xml:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.Forg/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="stuservice" class="service.stuService"> <property name="studao" ref="studao"></property> </bean></beans>

第七步:写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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="ture" /> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.objectFactory.spring.autoWire" value="no" /> <package name="default" namespace="/" extends="struts-default"> <action name="stu" class="stuAction" method="inertStu"> <result name="success">text.jsp</result> </action> </package></struts>

整合完毕,在整合期间遇到各种问题,记录一下: Session session = sessionFactory.getCurrentSession(); 用这个方法取session,不能用sessionFactory.getSeeion();因为连接池支持线程 还有就是一定要细心,测试的代码要注释掉,我忘了把 sessionFactory = new Configuration().configure().buildSessionFactory();注释掉,老是报找不到hibernate.cf.xml文件,可是整合的时候没有这个文件啊,所以以后一定要细心; 最后谢谢大家观看。让我们一起进步!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表