Struts+Hibernate:一个简单的注册界面

本文档详细介绍了如何利用Struts 1.x和Hibernate框架,在Web应用中实现用户注册功能。通过创建项目,添加相关库,编写持久化类、映射文件、配置文件以及Action和ActionForm,最终将用户输入的学号、用户名和密码存储到MySQL数据库中。
摘要由CSDN通过智能技术生成

实验目的:利用Struts 1.x和Hibernate框架,实现注册功能。即在Web界面上输入学号,用户名和密码,存入MySQL数据库中。

注:此实验是基于struts1.x框架下(含有Action,ActionForm和jsp)

使用软件:MySQL 8.0及以上版本,Navicat 12,MyEclipse2017

相关参数

  • 数据库名:login_database
  • 表名:people
  • 字段名:id,stdnumber,name,password

具体流程

1、新建Web Project项目,并在该项目新建Struts1.x和Hibernate

2、添加Hibernate相关jar包和MySQL连接jar包

在这里插入图片描述

3、编写Hibernate工具包函数HibernateSessionFactory.java和hibernateUtils.java

HibernateSessionFactory.java:

package hibernate;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateSessionFactory {
   
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	private static org.hibernate.SessionFactory sessionFactory;
 
	private static Configuration configuration = new Configuration();
	private static ServiceRegistry serviceRegistry;
 
	public HibernateSessionFactory() {
   
	}
	
	static {
   
		try {
   
			configuration.configure();
			serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
			try {
   
				sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
			} catch (Exception e) {
   
				StandardServiceRegistryBuilder.destroy(serviceRegistry);
				e.printStackTrace();
			}
		} catch (Exception e) {
   
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}
 
	
	public static Session getSession() throws HibernateException {
   
		Session session = (Session) threadLocal.get();
 
		if (session == null || !session.isOpen()) {
   
			if (sessionFactory == null) {
   
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession() : null;
			threadLocal.set(session);
		}
 
		return session;
	}
 
	public static void rebuildSessionFactory() {
   
		try {
   
			configuration.configure();
			serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
			try {
   
				sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
			} catch (Exception e) {
   
				StandardServiceRegistryBuilder.destroy(serviceRegistry);
				e.printStackTrace();
			}
		} catch (Exception e) {
   
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}
 
	public static void closeSession() throws HibernateException {
   
		Session session = (Session) threadLocal.get();
		threadLocal.set(null);
 
		if (session != null) {
   
			session.close();
		}
	}
 
	public static org.hibernate.SessionFactory getSessionFactory() {
   
		return ses
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值