logo资料库

Struts2整合Hibernate32开发注册登录系统.pdf

第1页 / 共18页
第2页 / 共18页
第3页 / 共18页
第4页 / 共18页
第5页 / 共18页
第6页 / 共18页
第7页 / 共18页
第8页 / 共18页
资料共18页,剩余部分请下载后查看
Hibernate 3.2 开发注册登录系统 Struts 2.1111 整合整合整合整合 Hibernate 3.2 Struts 2. 开发注册登录系统 开发注册登录系统 Hibernate 3.2 Struts 2. Struts 2. Hibernate 3.2 开发注册登录系统 MyEclipse 7.5 Tomcat 6.05 JDK 1.6 MySQL 5.1 — Struts 2.1 Hibernate 3.2 开发工具 开发工具: , , 开发工具开发工具 框架,大家可 开发准备 开发准备::::下载 开发准备开发准备 网站下载这两个框架。我们先来总览一下开发完成后的包 类图: , 和 Struts Hibernate ; 和 的官方 NEW Web Project "LoginSystem" lib Struts 2.1 Jar Hibernate 3.2 Jar src "hibernate.cfg.xml" 1 2 8 ,名为 ,在 包,然后按下面的步骤来: 全部代码如下: 目录下加入 6 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 9 root 10 11 jdbc:mysql://localhost:3306/LoginSystem 12 13 14 org.hibernate.dialect.MySQLDialect 15 16 17 LoginSystem 18 19 123 20
21 com.mysql.jdbc.Driver 22 23 true 24 25 "User.hbm.xml" 两者都放在 2 3 4 8 9 12 15 2 "com.rong.ORM" 1 package com.rong.ORM; 26 27 5 private int id; // ID 6 private String name; // 7 private String pwd; // "User.java" "User.java" public class User { 类映射配置 及其映射文件 、建立实体类 包下。其中 的代码如下: 主键 用户名 密码 方法 方法与 public void setName(String name) { public String getName() { /* 10 * Getter Setter 11 */ 13 return name; 14 } 16 this.name = name; 17 } 18 public String getPwd() { 19 return pwd; 20 } 21 public void setPwd(String pwd) { 22 this.pwd = pwd; 23 } 24 public int getId() { 25 return id; 26 } 27 public void setId(int id) { 28 this.id = id; 29 } 30 } User.hbm.xml 1 2 其中, 的代码如下:
//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 6 7 8 9 10 11 12 13 3 "ExportDB.java" User MySQL "LoginSystem" 类导入数据库转 的数 类: 2 5 10 */ 9 * , POJO , 1 package tool; 7 8 /* 6 public class ExportDB { 11 public static void main(String[] args){ 3 import org.hibernate.cfg.Configuration; 4 import org.hibernate.tool.hbm2ddl.SchemaExport; 12 Configuration cfg = new Configuration().configure(); 、建立 工具类,我们执行如下代码,就能轻松将 变成数据库中的表。不过,前提是我们已经在 中建立了一个名为 据库。 生成数据库的表 类和配置文件 运行此类 的 和管理 、建立获取 13 SchemaExport export = new SchemaExport(cfg); 通过 3 import org.hibernate.HibernateException; 6 import org.hibernate.cfg.Configuration; 5 import org.hibernate.SessionFactory; 14 export.create(true, true); 4 import org.hibernate.Session; 1 package com.rong.hibernate; 16 } 17 4 15 } 8 public class HibernateUtil { 2 7 9 SessionFactory Session HibernateUtil.java
10 // Hibernate 11 private static String configFile = "/hibernate.cfg.xml"; 13 private static Configuration configuration = new Configuration(); 15 private static SessionFactory sessionFactory = null; SessionFactory ( ) 25 sessionFactory = configuration.buildSessionFactory(); 26 System.out.println("[ SessionFactory"); 27 }catch(Exception e){ 28 System.out.println("[ SessionFactory 对象 线程安全 异常原因如 时发生异常 , 12 // 14 // 16 17 /** 18 * 19 */ 20 21 static { try{ 22 // 24 // , , Session Configuration hibernate.cfg.xml 配置文件所在的路径 声明 对象 建 工厂对象 建 只产生一个 只初始化一次 单例模式 配置数据库连接 通过 建立一个 工厂 初始化 标记 异常 创建 Session ] ] 23 configuration.configure(configFile); 29 e.printStackTrace(); :"); 30 } 31 } 32 33 /** 下 37 */ 34 * getSession() 35 * @return Session 36 * @throws HibernateException 38 public Session getSession(){ 39 Session session = null; 40 try{ 42 }catch(Exception e){ 43 System.out.println("[ ] 44 e.printStackTrace(); 方法 对象 45 } 46 return session; 47 } 48 49 /** 50 * closeSession() 51 * @param session Session 52 */ 41 session = sessionFactory.openSession(); 异常 方法 要关闭的 开启 :"); 时发生异常 , 异常原因如下 Session 对象
53 54 public void closeSession(Session session){ try{ 55 if(null != session) 56 session.close(); 57 }catch(Exception e){ 58 System.out.println("[ ] Session , :"); 时发生异常 关闭 异常原因如下 包下建立两个类,一个是接口,一个是实现类,其 59 e.printStackTrace(); 60 } 61 } 62 63 64 66 } 67 异常 5 DAO "com.rong.DAO" UserDao.java 1 package com.rong.DAO; 10 * @param user 12 public void add(User user); 3 import java.util.List; 4 import com.rong.ORM.User; public interface UserDao { 、 2 5 中 6 8 7 /** 9 * 11 */ 13 14 /** 15 * 19 */ 21 } 2 层设计:在 代码如下: 增加用户 要增加的用户 登录验证 不存在用户名 的代码如下: UserDaoImpl.java 1 package com.rong.DAO; 3 import java.util.Iterator; 4 import java.util.List; 16 * @param name 17 * @param password 18 * @return -1: ; -2: ; >0: 20 public int isExist(String name,String password); 密码不正确 登录成功 ID) ( 即返回该记录
5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.Transaction; 8 import com.rong.ORM.User; 9 import com.rong.hibernate.HibernateUtil; 10 11 public class UserDaoImpl implements UserDao { 12 13 HibernateUtil util = new HibernateUtil(); 14 15 /** 16 * 17 * @param user 18 */ 19 public void add(User user){ 20 Session session = util.getSession(); 21 Transaction ts = null; 22 try{ 23 ts = session.beginTransaction(); 增加用户 要增加的用户 24 session.save(user); 25 ts.commit(); 26 }catch(Exception e){ 28 e.printStackTrace(); 29 }finally{ 30 util.closeSession(session); 31 } 32 } 33 34 /** 35 * 36 * @param name 37 * @param password 登录验证 43 try{ ); 方法发生异常 即返回该记录 登录成功 ( 27 System.out.println("UserDaoImpl.add() :"); 38 * @return -1: ; -2: ; >0: ID) 39 */ 40 public int isExist(String name,String password){ 41 //int state = 0 ; // 42 Session session = util.getSession(); 不存在用户名 密码不正确 初始化状态变量 45 query.setString(0, name); 46 List list = query.list(); 47 if(null == list || 0 == list.size()){ 44 Query query = session.createQuery("from User u where u.name = ?"
48 return -1 ; // 49 } and u.pwd = ?"); 50 Query query2 = session.createQuery("from User u where u.name = ? 51 query2.setString(0, name); 52 query2.setString(1, password); 53 List list2 = query.list(); 54 if(null == list2){ 55 return -2 ; // 56 } 57 Iterator it = list.iterator(); 58 User user = (User)it.next(); 59 return user.getId(); // , ID 60 61 }catch(Exception e){ 62 System.out.println("UserDaoImpl.isExist() 63 e.printStackTrace(); 64 return 0; // 65 }finally{ 66 util.closeSession(session); 取 值 用户名不存在 密码不正确 验证成功 异常时返回 支持: --> 0 6 web.xml Struts 2.1 1 2 :"); 方法发生异常 67 } 68 } 69 } 70 6 7