MySQL Administrator 创建表
MyEclipse Database Explorer视图:
1. New
2 .Driver template: MySQL Connector/J
Driver name: 给定一个连接名称
Connection URL: jdbc:mysql://<hostname>[<:3306>]/<dbname>相应的修改
User name: MySQL连接名
Password: MySQL连接密码
Driver JARs: 添加 mysql-connector-java-5.1.7-bin.jar包
Save password + Connect to database on MyEclipse startup
3. 选择Display the seleted schemas - 添加表名
4. Finish
1. 右击表名—Hibernate Reverse Engineering...
2. Next — Id Generator:native
3. Finish
添加Struts 1.3 capabilities 和 Hibernate 3.1 capabilities
新建Struts1.3 Action: CRUD(create、read、update、delete)
例子:
create:
//创建一个HibernateCrud实体类,以供数据的填充
//HibernateCrud实体类相当于一条数据库中的记录(相当于一个bean)
HibernateCrud HibernateCrud_ref = new HibernateCrud();
HibernateCrud_ref.setUsername("大小");
HibernateCrud_ref.setAge(22);
//从HibernateSessionFactory类中的ThreadLocal类中获得一个Session对象
//这个session对象就相当于JDBC中的Connection类
Session session = HibernateSessionFactory.getSession();
//启动事务保证数据的完整性
Transaction tra = session.beginTransaction();
//保存实体类以将这个实体类通过Hibernate映射机制转换成数据库中的一条记录
session.save(HibernateCrud_ref);
//提交事务
tra.commit();
//关闭session,释放数据库的链接资源
session.close();
read:
//获得Session对象从HibernateSessionFactory类中的ThreadLocal对象中
Session session = HibernateSessionFactory.getSession();
//启动事务
Transaction tra = session.beginTransaction();
//通过Session对象的引用session的load方法来从数据库中取得一个id为1的实体类HibernateCrud.class
HibernateCrud HibernateCrud_ref = (HibernateCrud) session.load(HibernateCrud.class, new Integer(1));
//显示这个实体类的信息
System.out.println("id=" + HibernateCrud_ref.getId());
System.out.println("username=" + HibernateCrud_ref.getUsername());
System.out.println("age=" + HibernateCrud_ref.getAge());
System.out.println("registerTime=" + HibernateCrud_ref.getRegisterTime());
//提交事务
tra.commit();
//关闭session,释放数据库的连接
session.close();
update:
//取得session对象
Session session = HibernateSessionFactory.getSession();
//启动事务
Transaction tra = session.beginTransaction();
//取得旧数据并且开始更新旧数据
HibernateCrud HibernateCrud_old_ref = (HibernateCrud) session.load(HibernateCrud.class, new Integer(1));
System.out.println("id=" + HibernateCrud_old_ref.getId());
System.out.println("username="+ HibernateCrud_old_ref.getUsername());
System.out.println("age=" + HibernateCrud_old_ref.getAge());
System.out.println("registerTime=" + HibernateCrud_old_ref.getRegisterTime());
//重新对实体类的属性进行更改,然后同步更新数据库中记录的字段
HibernateCrud_old_ref.setUsername("最新版");
HibernateCrud_old_ref.setAge(100);
//同步更新到数据库中
session.update(HibernateCrud_old_ref);
//提交事务
tra.commit();
//关闭session对象,释放数据库连接
session.close();
delete:
//取得Session对象
Session session = HibernateSessionFactory.getSession();
//启动事务
Transaction tra = session.beginTransaction();
//从数据库中通过load方法取得HibernateCrud的实体类,查询条件是主键为1
HibernateCrud HibernateCrud_old_ref = (HibernateCrud) session.load(HibernateCrud.class, new Integer(1));
System.out.println("id=" + HibernateCrud_old_ref.getId());
System.out.println("username" + HibernateCrud_old_ref.getUsername());
System.out.println("age=" + HibernateCrud_old_ref.getAge());
System.out.println("registerTime=" + HibernateCrud_old_ref.getRegisterTime());
//在Hibernate中删除数据库中的记录是通过一个实体类进行删除的
//所以在删除的操作中需要先找到实体类,然后再通过Session对象的delete()方法
//来删除数据库中的记录
session.delete(HibernateCrud_old_ref);
//提交事务
tra.commit();
//关闭session对象,释放数据库的连接
session.close();
部署、测试