会员管理
Users.java
public class Users implements Serializable
{
private Integer id;
private String username;
private String password;
private Date birthday;
private String phone;
private String address;
private String email;
private Date regdate;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone = phone;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getRegdate()
{
return regdate;
}
public void setRegdate(Date regdate)
{
this.regdate = regdate;
}
}
数据库 users表
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="cn.com.widemex.users.Users" table="users">
<id name="id" column="id" type="integer">
<generator class="increment"/>
</id>
<property name="username" column="username" type="string"/>
<property name="password" column="password" type="string"/>
<property name="birthday" column="birthday" type="date"/>
<property name="phone" column="phone" type="string"/>
<property name="address" column="address" type="string"/>
<property name="email" column="email" type="string"/>
<property name="regdate" column="regdate" type="timestamp"/>
</class>
</hibernate-mapping>
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="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="cn/com/widemex/users/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
MysessionFactory.java
public class MysessionFactory
{
//定义一个静态字符串变量存放Hibernate的配置文件名
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
//定义一个线程局部变量对象
private static final ThreadLocal threadLocal = new ThreadLocal();
//创建一个静态的Configuration对象
private static final Configuration cfg = new Configuration();
//定义一个静态的SessionFactory对象
private static SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException
{
Session session = (Session)threadLocal.get();
if(session==null)
{
if(sessionFactory==null)
{
try{
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory=cfg.buildSessionFactory();
}
catch (Exception e) {
System.out.println("【系统错误】创建SessionFactory对象时出错 ,原因是:");
e.printStackTrace();
}
}
//通过SessionFactory对象创建Session对象
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
//关闭一个Session对象
public static void closeSession()
{
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if(session!=null)
{
session.close();
}
}
//构造方法
private MysessionFactory()
{
}
}
MemberDaoImpl.java
public class MemberDaoImpl implements MemberDao
{
//新增会员
/* (non-Javadoc)
* @see cn.com.widemex.Dao.MemberDao#addMember(cn.com.widemex.users.Users)
*/
public void addMember(Users user)
{
//创建一个Session对象
Session session = MysessionFactory.currentSession();
//定义一个Transaction对象
Transaction ts = null;
try
{
//创建一个Transaction对象
ts=session.beginTransaction();
//调用Session对象的save方法将持久化对象保存到数据库中
session.save(user);
//提交事务
ts.commit();
}
catch (Exception ex)
{
//回滚事务
if(ts!=null)ts.rollback();
System.out.println("【系统错误】 在保持持久化对象是出错,原因是:");
ex.printStackTrace();
}
finally
{
//关闭Session对象
MysessionFactory.closeSession();
}
}
//浏览会员
/* (non-Javadoc)
* @see cn.com.widemex.Dao.MemberDao#allMember()
*/
public List allMember()
{
List members=new ArrayList();
//创建一个Session对象
Session session = MysessionFactory.currentSession();
//定义一个Transaction对象
Transaction ts = null;
try
{
String HQL="select a from users as a order by a.id";
//创建一个Query查询对象
Query query = session.createQuery(HQL);
//创建一个Transaction
ts=session.beginTransaction();
//执行查询,取得查询结果
members=query.list();
//提交事务
ts.commit();
if(!Hibernate.isInitialized(members))Hibernate.initialize(members);
}
catch (Exception ex)
{
//回滚事务
if(ts!=null)ts.rollback();
System.out.println("【系统错误】 在保持持久化对象是出错,原因是:");
ex.printStackTrace();
}
finally
{
//关闭Session对象
MysessionFactory.closeSession();
}
return members;
}
//删除会员
/* (non-Javadoc)
* @see cn.com.widemex.Dao.MemberDao#delMember(java.lang.Integer)
*/
public void delMember(Integer id)
{
//创建一个Session对象
Session session = MysessionFactory.currentSession();
//定义一个Transaction对象
Transaction ts = null;
try
{
//创建一个Transaction对象
ts=session.beginTransaction();
//调用Session对象的load方法装载指定ID的会员对象到内存中
Users user = (Users)session.load(Users.class, id);
//调用session对象的delete方法将持久化对象删除
session.delete(user);
//提交事务
ts.commit();
}
catch (Exception ex)
{
//回滚事务
if(ts!=null)ts.rollback();
System.out.println("【系统错误】 在保持持久化对象是出错,原因是:");
ex.printStackTrace();
}
finally
{
//关闭Session对象
MysessionFactory.closeSession();
}
}
//装载会员
/* (non-Javadoc)
* @see cn.com.widemex.Dao.MemberDao#loadMember(java.lang.Integer)
*/
public Users loadMember(Integer id)
{
//创建一个Session对象
Session session = MysessionFactory.currentSession();
//定义一个Transaction对象
Transaction ts = null;
Users user = null;
try
{
//创建一个Transaction对象
ts=session.beginTransaction();
//调用Session对象的load方法装载指定ID的会员对象到内存中
user = (Users)session.get(Users.class, id);
//提交事务
ts.commit();
}
catch (Exception ex)
{
//回滚事务
if(ts!=null)ts.rollback();
System.out.println("【系统错误】 在保持持久化对象是出错,原因是:");
ex.printStackTrace();
}
finally
{
//关闭Session对象
MysessionFactory.closeSession();
}
return user;
}
//修改会员
/* (non-Javadoc)
* @see cn.com.widemex.Dao.MemberDao#modiMember(cn.com.widemex.users.Users)
*/
public void modiMember(Users user)
{
//创建一个Session对象
Session session = MysessionFactory.currentSession();
//定义一个Transaction对象
Transaction ts = null;
try
{
//创建一个Transaction对象
ts=session.beginTransaction();
//调用Session对象的upadte方法将持久化对象更新到数据库中
session.update(user);
//提交事务
ts.commit();
}
catch (Exception ex)
{
//回滚事务
if(ts!=null)ts.rollback();
System.out.println("【系统错误】 在保持持久化对象是出错,原因是:");
ex.printStackTrace();
}
finally
{
//关闭Session对象
MysessionFactory.closeSession();
}
}
}