您的位置:首页 > 其它

【ssh】 ssh完整例子--ssh的初步灵活运用

2015-05-04 17:16 260 查看
<strong>一共有6步(文章底部附有源码下载地址,刚学完ssh的可以借鉴)</strong>

1 写一个Hibernate应用,完成用户的增加
1) User实体
2)UserDao接口
save(User u)
3)UserDaoImpl实现UserDao
save(User u){
//用hibernate api
}

2 让spring注入SessionFactory

1)在上一个应用中导入Spring的jar包
除了基础的6个包,还得导入2个jar包
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar

2) 增加配置文件beans.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"  value="classpath:hibernate.cfg.xml"/>
</bean>

<bean id="userDao" class="包名.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory">
</bean>

3) 在测试方法中,获得userDao,然后测试save()

UserDao userDao= (UserDao)cxt.getBean("userDao");

3 添加数据源,并注入到SessionFactory

1) 需要导入2个包
commons-dbcp.jar
commons-pool.jar

注:数据源的第3方实现有非常多,现我们用其中一种,commons-dbcp

2) 配置Datasource
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

3)在SessionFactory Bean中注入dataSource

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入DataSource Bean-->
<property name="dataSource" ref="myDataSource"/>

<property name="mappingResources">
<list>
<value>com/toceansoft/entity/User.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>

4) 测试结果

4 使用spring提供的HibernateTemplate

1) 在Dao中添加
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

2)修改方法,改成使用hibernateTemplate
save(o)
hibernateTemplate.save(o);

5 使用spring在业务层切入事务

1)在业务层写一个接口UserService
public void add(User u);
2) 写一个实现类UserServiceImpl实现接口UserService
public void add(User u){}
3)实现类UserServiceImpl提供userDao的get/set方法

4)在beans.xml中配置UserServiceImpl 的Bean,并注入userDao

5)配置事务管理器Bean
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
6) 配置事务通知
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 包括隔离级别,事务传播属性。。。 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
7)配置aop切面
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* com.toceansoft.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
8)测试结果
9)在add方法内抛出异常,再测试观察结果
throw new RuntimeException();

6 整合Spring与Struts
1)在把上面的java 应用变成web应用
--把上面应用Src下的所有东东复制到web应用的src

2)导入struts2开发的基础jar包,还需要一个特殊包
struts-spring-plugin.jar

导入与struts2整合的spring相应的包
org.springframework.web.struts-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar

3) 把beans.xml 复制到WEB-INF,并且改名为applicationContext.xml(可选的)

4) 在web.xml中配置spring启动的监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,classpath*:applicationContext2.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5)在web.xml配置Struts2的启动
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

6)在Web包下写UserAction,并且提供业务层UserSerivce的set方法

7)在appplicationContext.xml中配置UserAction bean
<bean id="userAction" class="com.toceansoft.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
注:scope="prototype"

8) 在struts.xml中配置UserAction
<!-- 跳转到添加产品页面 -->
<action name="User_toAdd" >
<result>/User_add.jsp</result>
</action>

<!-- 添加产品 -->
<!-- 关键点:这里的class设置action bean在spring配置文件中bean的id -->
<action name="User_add" class="userAction" method="add">
<result name="success">/grobal_message.jsp</result>
</action>

9) 写2个页面
User_add.jsp
grobal_message.jsp

10)部署测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: