Struts 2.0整合Hibernate 3.2开发注册登录系统
时间:2011-09-14 心梦帆影
开发工具:MyEclipse 6.0 ,Tomcat 5.5 ,JDK 1.5 ,MySQL 5.0 ;开发准备:下载Struts 2.0和 Hibernate 3.2,大家可Struts和Hibernate的官方网站下载这两个框架。我们先来总览一下开发完成后的 包—类图:
首先NEW一个Web Project ,名为"LoginSystem" ,在lib 目录下加入Struts 2.0的Jar包和Hibernate 3.2的Jar包,然后按下面的步骤来:
1、在 src 目录下建 "hibernate.cfg.xml" 全部代码如下:
<?xml version=''1.0'' encoding=''UTF-8''?>
2<!DOCTYPE hibernate- configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration- 3.0.dtd">
5
6<!-- Generated by MyEclipse Hibernate Tools. -- >
7<hibernate-configuration>
8 <session-factory>
9 <property name="connection.username">root</property>
10 <property name="connection.url">
11 jdbc:mysql://localhost:3306/LoginSystem
12 </property>
13 <property name="dialect">
14 org.hibernate.dialect.MySQLDialect
15 </property>
16 <property name="myeclipse.connection.profile">
17 LoginSystem
18 </property>
19 <property name="connection.password">root</property>
20 <property name="connection.driver_class">
21 com.mysql.jdbc.Driver
22 </property>
23 <property name="show_sql">true</property>
24 <!-- POJO类映射配置 -->
25 <mapping resource="com/rong/ORM/User.hbm.xml" />
26 </session- factory>
27</hibernate-configuration>
Struts 2.0整合Hibernate 3.2开发注册登录系统(2)
时间:2011-09-14 心梦帆影
2、建立实体类 "User.java" 及其映射文件 "User.hbm.xml" 两者都放在 "com.rong.ORM" 包下。其 中"User.java"的代码如下:
1package com.rong.ORM;
2
3public class User {
4
5 private int id; //主键ID
6 private String name; //用户名
7 private String pwd; //密码
8
9 /**//*
10 * Getter方法与Setter方法
11 */
12 public String getName() {
13 return name;
14 }
15 public void setName (String name) {
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<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3"http://hibernate.sourceforge.net/hibernate-mapping- 3.0.dtd">
4
5<hibernate-mapping&g
|