ssh经典例子

原创   Struts2,Spring,Hibernate整合例子一个 收藏

1.建立web项目
2.在web.xml  配置 struts2,和 spring ,
 <!-- 配置Struts2 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher(struts的过滤器)
  </filter-class>
 </filter>
 <filter-mapping>(加载struts2)
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!--  配置Spring  -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener(spring监听器)
  </listener-class>
 </listener>
 <!--  查找spring配置文件 -->
 <context-param>(程序从此处开始加载.xml 文件,并执行)
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:*.xml</param-value>
 </context-param>
3.在src根目录下创建struts.xml文件
  <include file="struts-default.xml"/>固定格式,用到此包
4.在src根目录 创建hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<hibernate-configuration>
<session-factory>
 <!-- 基本配置 -->
 <property name="dialect">
  org.hibernate.dialect.Oracle9Dialect
 </property>
 <property name="show_sql">false</property>
 <property name="format_sql">true</property>
 <property name="bytecode.use_reflection_optimizer">true</property>
 <property name="max_fetch_depth">2</property>
 <property name="default_batch_fetch_size">8</property>
 <property name="jdbc.batch_size">20</property>

</session-factory>
</hibernate-configuration>
系统给的文件 只需要拷贝,都是一些数据库库的基本配置

5.在src根目录 拷贝struts.properties文件

struts.objectFactory = spring   该项是告诉系统 用spring来 控制hibernate和Action
struts.action.extension=action  告诉系统访问时以什么结尾。可以改为:  do  例如:login.do   ,此处为:login.action
6.在src根目录下 创建applicationContext.xml
 配置数据源
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/> <!--jtds<property name="driverClassName" value ="net.sourceforge.jtds.jdbc.Driver"/>-->
    <property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>  <!--<property name="url" value="jdbc:jtds:sqlserver://localhost:1455/demo"></property>-->
    <property name="username" value ="test"/>
    <property name="password" value ="test"/>
   
   </bean>

 创建sessionFactory 工厂
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
     <ref local="dataSource"/>
    </property>
    <property name="configLocation">
     <value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
    </property>
   </bean>
 考来用

7.创建 实体类
 就是写一个bean 含有get/set 方法
8.在实体类所在的目录下,写一个 类名.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="com.zx.test.model.User" table="LB_USER">
   <id name="id" type="java.lang.Long">主键的类型为Long
       <column name="ID"/>
       <generator class="sequence">
         <param name="sequence">SEQ_LB_USER</param>指定主键生成方式,必须在pl/sql创建主键生成方式
       </generator>
   </id>
   <property name="name" type="java.lang.String">
       <column name="name" length="20"/>
   </property>
   <property name="age" type="java.lang.Integer">
       <column name="age" length="2"/>
   </property>
   <property name="password" type="java.lang.String">
       <column name="password" length="20"/>
   </property>
   所有属性的name 必须与 bean中的set后的名称一样
      
      </class>
  </hibernate-mapping>

9.创建一个 Dao 实现操作方法
 
10 创建 service  调用  Dao中的方法
 
11.创建  Action   将service的得到的user 加到Action中

 
12.在applicationContext.xml 中配置
 将userDao注入到sessionFactory中
 将userService 注入到UserDao中
 将userAction 注入到 userService中
13.在struts.xml中 配置Action
   <package name="user" extends="struts-default(固定与include对应)" namespace="/use(访问时路径)r">
    <action name="userAdd(访问时路径)" class="userAction" method="userAdd">
       <result name="success">/userShow.jsp</result>
    </action>
  </package>

 此处配置了访问时的路径:/user/userAdd

14.将8中的配置文件 加入到 4 中
 <mapping resource="com/zx/test/model/User.hbm.xml" />
 引用配置文件

15.写jsp页面 表单提交到 Action

9.10.11:是 Action 调用 Service ,Service调用 Dao 也是分层的体现

Web.xml 是web工程的配置文件

ApplicationContext  是 spring的配置文件

Hibernate.hbm.xml 是hibernate的配置文件


建表是 注意创建主键时 name 为 表明

项目目录:



 

User.java

package com.zx.test.model;

public class User {

 private Long id;
 
 private String name;
 
 private Integer age;
 
 private String password;

   //   get/set方法
 
}

User.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="com.zx.test.model.User" table="LB_USER">
        <id name="id" type="java.lang.Long">
            <column name="ID"/>
            <generator class="sequence">
              <param name="sequence">SEQ_LB_USER</param>
            </generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20"/>
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age" length="2"/>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="20"/>
        </property>
    
    </class>
</hibernate-mapping>

UserDao.java

package com.zx.test.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zx.test.model.User;

public class UserDao extends HibernateDaoSupport {
 
 /**
  * 保存user
  * @param user
  */
 public void saveUser(User user){
//  spring自带方法 保存
  this.getHibernateTemplate().save(user);
 }

//update, delete ,findAll 方法同上,省略!

}

UserService.java

package com.zx.test.service;

import com.zx.test.dao.UserDao;
import com.zx.test.model.User;

public class UserService {

 private UserDao userDao;
 
 public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
 }


 public void saveUser(User user){
  userDao.saveUser(user);
 }

// 在此处添加业务逻辑方法;
}

UserAction.java

package com.zx.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zx.test.model.User;
import com.zx.test.service.UserService;

public class UserAction extends ActionSupport {

 private User user;
  
 public User getUser() {
  return user;
 }

 public void setUser(User user) {
  this.user = user;
 }
 
 
 private UserService userService ;

 public void setUserService(UserService userService) {
  this.userService = userService;
 }
 
 
 public String userAdd(){
  
  this.userService.saveUser(user);
  
  return SUCCESS;
 }

//添加action方法

}

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>
  <include file="struts-default.xml"/>
  <package name="user" extends="struts-default" namespace="/user">
    <action name="userAdd" class="userAction" method="userAdd">
       <result name="success">/userShow.jsp</result>
    </action>
  </package>
</struts>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<hibernate-configuration>
<session-factory>
 <!-- 基本配置 -->
 <property name="dialect">
  org.hibernate.dialect.Oracle9Dialect
 </property>
 <property name="show_sql">false</property>
 <property name="format_sql">true</property>
 <property name="bytecode.use_reflection_optimizer">true</property>
 <property name="max_fetch_depth">2</property>
 <property name="default_batch_fetch_size">8</property>
 <property name="jdbc.batch_size">20</property>
 
 
 <mapping resource="com/zx/test/model/User.hbm.xml" />

</session-factory>
</hibernate-configuration>

applicationContext.xml


   
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>
    <property name="username" value ="test"/>
    <property name="password" value ="test"/>
   
   </bean>
   
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
     <ref local="dataSource"/>
    </property>
    <property name="configLocation">
     <value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
    </property>
   </bean>
   
   <bean id="userDao" class="com.zx.test.dao.UserDao">
     <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   
   <bean id="userService" class="com.zx.test.service.UserService">
     <property name="userDao" ref="userDao"></property>
   </bean>
   
   <bean id="userAction" class="com.zx.test.action.UserAction" scope="prototype">
     <property name="userService" ref="userService"></property>
   </bean>

web.xml:

<!-- 配置Struts2 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>

<init-param>
 <param-name>config</param-name>
 <param-value>/WEB-INF/struts.xml </param-value>
 
 </init-param>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!--  配置Spring  -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener  </listener-class>
 </listener>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:*.xml</param-value>
 </context-param>

index.jsp:

<form action="<%=request.getContextPath() %>/user/userAdd.action" name="form1" method="post">
   
       userName: <input type ="text" name="user.name"><br>
       password:<input type="password" name="user.password"><br>
       age:<input type="text" name="user.age"><br>
       <input type="submit" value="submit">&nbsp;<input type="reset" value="reset">
    </form>

userShow.jsp

在第一行添加 struts2 的标签

即:<%@ taglib prefix="s" uri="/struts-tags" %>

<body>
 
       userName: <s:property value="user.name"/><br>
       password:<s:property value="user.password"/><br>
       age:<s:property value="user.age"/>
   
  </body>


struts.properties 文件可要可无!(可以自己配置)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值