struts2 sping2 hibernate3.2新手入门

最近正在学习ssh的最新版本。由于一直没有用ssh做过东西,尤其是中间的spring,项目中用到的struts和hibernate,所以对于在中间层加上spring该如何处理,还存在一些困惑,这是我做的一个三者结合的例子,其主要目的是充分利用spring的ioc和hibernate3的annoation

1.struts2和spring的结合,以及如何把action纳入spring的管理

首先:工程中应加入struts2-spring-plugin-2.0.8.jar

其次:在web.xml中加入如下

 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

第三:在struts.xml和applicationContext.xml中的配置如下

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>
    <package name="com" extends="struts-default">
        <action name="Login" class="Login" method="getAllUserInfo"><!--此处的class属性不能写成包名.类名的格式,否则会报空指针异常-->
            <result name="input">Login.jsp</result><!-- 如果输入为空的话跳转到Login.jsp -->
            <result >userindex.jsp</result><!--因为此处没有声明name属性,所以默认为成功时的跳转页, 如果成功的话跳转到userindex.jsp -->
        </action>
    </package>
   
</struts>

applicationContext.xml中(配置片段)

 <bean id="Login" class="com.web.action.Login" p:iuserinfo-ref="UserInfoImpl" scope ="prototype"/>

 注意”红色“字体部分,其中action中的class=“Login”要和<bean>中的id对应,action的属性mehtod如果没有制定的话,将默认调用struts中action中的execute方法,在struts2官方的faq中提到要把action纳入spring的管理还得添加<beans default-autowire="autodetect">(原因还不太清除),由于strus2为每一个请求实例化一个action所以bean的scope要设成protype,默认为singleton

ok,struts2和spring已经结合到一起,剩下的就是根据项目需要"求精了"

 

 2,spring和hibernate的结合hibernate3 aonotation

目前的例子只是演示如何集成二者,所以只是将hibernte的SessionFactory交由spring管理,其他的如(事务管理没有考虑很细致,全部在dao层控制)

如下applicationContext.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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
 default-lazy-init="true" default-autowire="autodetect"><!-- 默认是byName,可以改为byType,autodetect将先按照byName处理在按照byType -->
 <!--datasource config  -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  p:driverClassName="com.mysql.jdbc.Driver"
  p:url="jdbc:mysql://localhost:3306/paulstudy" p:username="root"
  p:password="1234" />
 <!-- SessionFactory  annotation config, instead of "hibernate.cfg.xml"-->

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.hbm2ddl.auto">none</prop>
   </props>
  </property>
  <property name="annotatedClasses">
   <list>
    <value>com.model.UserInfo</value>
   </list>
  </property>
  <property name="annotatedPackages">
   <list>
    <value>com.model</value>
   </list>
  </property>
 </bean>

 <!-- SessionFactory  annotation config, i end -->
 <bean id="usermanager" class="com.dao.hibernate.UserManagerImpl">
  <property name="dataSource">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="UserInfoImpl" class="com.business.service.UserInfoImpl"
  p:iusermanager-ref="usermanager" />
 <bean id="Login" class="com.web.action.Login"
  p:iuserinfo-ref="UserInfoImpl" scope="prototype" />
 <bean id="userinfo" class="com.UserInfo" p:userName="paul"
  p:age="25" />

</beans>

通过以上实现了hibernate的零配置文件。如下domainobject

package com.model;

import javax.persistence.Column;//所在jar包是 persistence-api-1.0.jar
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import org.hibernate.annotations.AccessType;
//import org.hibernate.annotations.Entity;
import org.hibernate.annotations.GenericGenerator;

@Entity//此处声明必须用avax.persistence.Entity;而不能用 org.hibernate.annotations.Entity
@Table(name = "userinfo")
@AccessType("property")
public class UserInfo {

 private String userId = "";

 private String userName = "";

 private String userSex = "";

 private int userAge = 0;

 @Id
 @Column(name = "userid")
 @GeneratedValue(generator = "system-uuid")
 @GenericGenerator(name = "system-uuid", strategy = "uuid")
 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 @Column(name = "username")
 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 @Column(name = "usersex")
 public String getUserSex() {
  return userSex;
 }

 @Column(name = "userage")
 public int getUserAge() {
  return userAge;
 }

 public void setUserAge(int userAge) {
  this.userAge = userAge;
 }

 public void setUserSex(String userSex) {
  this.userSex = userSex;
 }
}

 

ok。spring和hibernate的结合,hibernate的零配置已经搞定,很简单吧

学好。学精还需要时间啊!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值