狂淘

www.kuangtao.net

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  6 随笔 :: 185 文章 :: 68 评论 :: 0 Trackbacks

项目目录:



 

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>
 </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 文件可要可无!(可以自己配置)

posted on 2009-09-16 15:50 狂淘 阅读(1132) 评论(0)  编辑  收藏 所属分类: struts2.0 spring hiberate

只有注册用户登录后才能发表评论。


网站导航: