Hibernate完成CRUD

准备:新建工程,导入Hibernate需要的jar包和数据库连接用的jar包。mysql

1、创建实体类

public class User {
 private int id;
 private String name;
 private Date birthday;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
}

2、创建实体类的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 package = "domain">
 <class name = "User">
  <id name = "id">
   <generator class = "native"></generator>
  </id>
  <property name = "name"/>
  <property name = "birthday"/>
 </class>
</hibernate-mapping>

3、创建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 = "connection.url">jdbc:mysql://localhost:3306/xxxxxx</property>
  <property name = "connection.username">root</property>
  <property name = "connection.password">xxxxxx</property>
  <property name = "connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name = "hibernate.show_sql">true</property>
  <property name = "current_session_context_class">thread</property>
  <property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name = "hbm2ddl.auto">create</property>
  <mapping resource = "domain/User.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

4、写HibernateUtil工具类

public class HibernateUtil {

 private static SessionFactory factory;
 static {
  Configuration cf = new Configuration();
  cf.configure();
  factory = cf.buildSessionFactory();
 }
 private static Session getSession(){
  return factory.openSession();
 }
 public static void add(Object obj){
  Session session = null;
  Transaction tr = null;
  try {
   session = HibernateUtil.getSession();
   tr = session.beginTransaction();
   session.save(obj);
   tr.commit();
  } finally {
   if(session != null){
    session.close();
   }
  }
 }
 public static void delete(Object obj){
  Session session = null;
  Transaction tr = null;
  try {
   session = HibernateUtil.getSession();
   tr = session.beginTransaction();
   session.delete(obj);
   tr.commit();
  } finally {
   if(session != null){
    session.close();
   }
  }
 }
 public static void update(Object obj){
  Session session = null;
  Transaction tr = null;
  try {
   session = HibernateUtil.getSession();
   tr = session.beginTransaction();
   session.update(obj);
   tr.commit();
  } finally {
   if(session != null){
    session.close();
   }
  }
 }
 public static List<User> query(String name){
  List<User> list = null;
  String hql = "from User as user where user.name = :name";
  Session session = null;
  try {
   session = HibernateUtil.getSession();
   Query query = session.createQuery(hql);
   query.setString("name", name);
   list = query.list();
   return list;
  } finally {
   if(session != null){
    session.close();
   }
  }
 }
 public static List<User> cretiria(String name){
  List<User> list = null;
  Session session = null;
  try {
   session = HibernateUtil.getSession();
   Criteria cr = session.createCriteria(User.class);
   cr.add(Restrictions.eq("name", name));
   list = cr.list();
   return list;
  } finally {
   if(session != null){
    session.close();
   }
  }
 }
}

5、写DAO接口

public interface UserDao {

 void add(User user);
 void delete(User user);
 void update(User user);
 List<User> queryByName(String name);
 List<User> cretiriaByName(String name);
}

6、写DAO实现类

public class UserDaoImpl implements UserDao {

 @Override
 public void add(User user) {
  HibernateUtil.add(user);
 }

 @Override
 public void delete(User user) {
  HibernateUtil.delete(user);
 }

 @Override
 public void update(User user) {
  HibernateUtil.update(user);
 }

 @Override
 public List<User> queryByName(String name) {
  return HibernateUtil.query(name);
 }

 @Override
 public List<User> cretiriaByName(String name) {
  return HibernateUtil.cretiria(name);
 }

}

7、写测试类

public class Test {
 /**
  * @param args
  */
 public static void main(String[] args) {
  UserDaoImpl impl = new UserDaoImpl();
  User user = new User();
  user.setName("maomao");
  user.setBirthday(new Date());
  impl.add(user);
  impl.queryByName(user.getName());
  impl.cretiriaByName(user.getName());
  impl.update(user);
  user.setId(1);
  impl.delete(user);
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值