Hibernate学习(第一天)--CURD

Hibernate实现对象的增删改查

   Hibernate是一个面向Java环境的对象/关系数据库映射工具,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。

   Hibernate的优势:开源免费;轻量级封装;可拓展性;稳定发展。

   使用步骤:

1,新建Java/WEB工程.

    new-->Web Project -->填写工程名.

2,在工程上右键-->myEclipse-->Add Hibernate Capabilities --> 选择Hibernate版本号 --> Next --> 选择数据库类型 --> Next --> 选择JavaPackage --> Finish.(生成两个与Hibernate相关的文件:hibernate.cfg.xml,HibernateSessionFactory.java)

3,生成映射类和映射文件:

切换到DB,选择表,右键 --> Hibernate Reverse Engineering.. --> 选择需要生成的文件(实体类,映射文件,dao) --> Generation Id 选择native-->Finish.

4,自己写Dao方法.

代码如下,

接口类:BaseDao

package com.justplay.dao;
import java.util.List;
public interface BaseDao {
 public void save(Object o);
 public void update(Object o);
 public void delete(Object o);
 public Object get(Class<?> clazz, long id);
 public List<?> list(String hql);
}

实现类:BaseDaoImpl

package com.justplay.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.justplay.dao.BaseDao;
import com.justplay.util.HibernateSessionFactory;
public class BaseDaoImpl implements BaseDao {
 public void save(Object o) {
  Session session = HibernateSessionFactory.getSession();
  Transaction tr = session.beginTransaction();
  try {
   session.save(o);
   tr.commit();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
 }
 public void update(Object o) {
  Session session = HibernateSessionFactory.getSession();
  Transaction tr = session.beginTransaction();
  try {
   session.update(o);
   tr.commit();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
 }
 public void delete(Object o) {
  Session session = HibernateSessionFactory.getSession();
  Transaction tr = session.beginTransaction();
  try {
   session.delete(o);
   tr.commit();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
 }
 public Object get(Class<?> clazz, long id) {
  Session session = HibernateSessionFactory.getSession();
  try {
   return session.get(clazz, id);
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
  return null;
 }
 public List<?> list(String hql) {
  Session session = HibernateSessionFactory.getSession();
  try {
   return session.createQuery(hql).list();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
  return null;
 }
}

测试类TestBaseDaoImpl:

package test;
import java.util.List;
import junit.framework.TestCase;
import com.justplay.dao.BaseDao;
import com.justplay.po.User;
public class TestBaseDaoImpl extends TestCase {
 BaseDao dao;
 protected void setUp() throws Exception {
  dao = new com.justplay.dao.impl.BaseDaoImpl();
 }
 protected void tearDown() throws Exception {
  super.tearDown();
 }
 public void testSave() {
  User u = new User();
  u.setUsername("Tom");
  u.setPassword("1234");
  dao.save(u);
 }
 public void testUpdate() {
  User u = (User) dao.get(User.class, 2);
  u.setUsername("Lucy");
  u.setPassword("2345");
  dao.update(u);
 }
 public void testDelete() {
 }
 public void testGet() {
  System.out.println(dao.get(User.class, 1));
 }
 public void testList() {
  List<?> list = dao.list("from User");
  System.out.println(list.size());
 }
}

给出实体类吧:

User

package com.justplay.po;
/**
 * User entity. @author MyEclipse Persistence Tools
 */
public class User implements java.io.Serializable {
 // Fields
 private static final long serialVersionUID = 1L;
 private Long id;
 private String password;
 private String username;
 // Constructors
 /** default constructor */
 public User() {
 }
 /** full constructor */
 public User(String password, String username) {
  this.password = password;
  this.username = username;
 }
 // Property accessors
 public Long getId() {
  return this.id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getPassword() {
  return this.password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return this.username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 @Override
 public String toString() {// 方便测试
  return "User [id=" + id + ", password=" + password + ", username="
    + username + "]";
 }
}

如果觉得 BaseDao中的方法太多,可以换种方式: UserDaoImpl

package com.justplay.dao.impl;
import com.justplay.dao.UserDao;
import com.justplay.po.User;

// 继承BaseDaoImpl(可以不用继续写实现方法了),实现UserDao
 public class UserDaoImpl extends BaseDaoImpl implements UserDao {
 public void save(User u) {
  super.save(u);
 }
}

UserDao

package com.justplay.dao;
import com.justplay.po.User;
public interface UserDao {
 public void save(User u);
}

 测试类TestUserDaoImpl:

package test;
import junit.framework.TestCase;
import com.justplay.dao.UserDao;
import com.justplay.dao.impl.UserDaoImpl;
import com.justplay.po.User;
public class TestUserDaoImpl extends TestCase {
 UserDao dao;
 protected void setUp() throws Exception {
  dao = new UserDaoImpl();
 }
 protected void tearDown() throws Exception {
  super.tearDown();
 }
 public void testSaveUser() {
  User u = new User();
  u.setPassword("1234");
  u.setUsername("Jack");
  dao.save(u);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值