`
斌强-朱
  • 浏览: 51437 次
社区版块
存档分类
最新评论

Struts2+Hibernate

 
阅读更多

由MyEclipse下导入的Hibernate的jar包


Struts2基本jar包和MySql驱动jar包


源代码文件


数据库脚本

create database hibernate;

use hibernate;

create table if not exists user (
    id    int    primary key,
    name   varchar(20),
    pass     varchar(20)
};

web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

struts2的配置文件 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>
	<package name="struts2" extends="struts-default">
		<action name="submitAction" class="org.zbq.struts.SubmitAction" method="save">
			<result name="success">/list.jsp</result>
		</action>
	</package>
</struts>


Hibernate配置文件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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
    	<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
    	<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    	<property name="connection.username">root</property>
    	<property name="connection.password">mysqladmin</property>
    	<property name="show_sql">true</property>
    	<mapping resource="org/zbq/bean/User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


User类

package org.zbq.bean;

public class User {
	private int id;
	private String name;
	private String pass;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}


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="org.zbq.bean.User" table="user">
		<id name="id" column="id" type="int">
			<generator class="increment"></generator>
		</id>
		<property name="name" column="name" type="string"/>
		<property name="pass" column="pass" type="string"/>
	</class>
</hibernate-mapping>


SubmitActionl.java

package org.zbq.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.zbq.bean.User;
import org.zbq.util.HibernateUtil;

import com.opensymphony.xwork2.Action;

public class SubmitAction implements Action {
	private User user;
	
	public User getUser() {
		return user;
	}

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

	public String save() throws Exception {
		HibernateUtil.save(user);
		List<User> list = HibernateUtil.list();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("list", list);
		return SUCCESS;
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}

}


HibernateUtil.java

package org.zbq.util;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.zbq.bean.User;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	static {
		try {
			Configuration conf = new Configuration().configure();
//			conf.addClass(User.class);
			sessionFactory =  conf.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void save(User user) throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			tx.begin();
			session.save(user);
			tx.commit();
		} catch (Exception e) {
			if (null != tx){
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static List<User> list() throws Exception{
		List<User> list = new ArrayList<User>();
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			tx.begin();
			Query query = session.createQuery("from User");
			list = query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return list;
	}
}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
    <form action="submitAction">
    	Name:<input type="text" name="user.name"/><br/>
    	Pass:<input type="text" name="user.pass"/><br/>
    	<input type="submit" value="submit">
    </form>
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  	<table border="1">
  		<tr>
  			<td>Name</td>
  			<td>Pass</td>
  		</tr>
	  	<s:iterator value="#request.list" id="user">
  		<tr>
  			<td><s:property value="name"/></td>
  			<td><s:property value="pass"/></td>
  		</tr>
  		</s:iterator>
  	</table>
  </body>
</html>











分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics