Hibernate框架增删改查
Posted 0A1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate框架增删改查相关的知识,希望对你有一定的参考价值。
1 package cn.happy.util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 /** 8 * 1.1用于生产session对象的工具类 9 */ 10 public class HibernateUtil { 11 private static Configuration cfg=new Configuration().configure(); 12 private static SessionFactory factory=cfg.buildSessionFactory(); 13 //方法返回session 14 public static Session getsession(){ 15 return factory.openSession(); 16 } 17 //关闭session 18 public static void closeSession(){ 19 getsession().close(); 20 } 21 22 }
1 package cn.happy.entity; 2 /** 3 * 实体类 4 * @author Administrator 5 * 6 */ 7 public class Student { 8 private Integer id; 9 private String name; 10 private Integer age; 11 12 @Override 13 public String toString() { 14 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; 15 } 16 public Integer getId() { 17 return id; 18 } 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public Integer getAge() { 29 return age; 30 } 31 public void setAge(Integer age) { 32 this.age = age; 33 } 34 35 }
1 package cn.happy.test; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.Transaction; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.classic.Session; 7 import org.junit.Test; 8 9 import cn.happy.entity.Student; 10 /** 11 * 增加 12 * @author Administrator 13 * 14 */ 15 public class addTest { 16 @Test 17 public void addtest(){ 18 Student stu=new Student(); 19 stu.setId(1); 20 stu.setName("张三"); 21 stu.setAge(18); 22 Configuration cf=new Configuration().configure("hibernate.cfg.xml"); 23 SessionFactory factory = cf.buildSessionFactory(); 24 Session session = factory.openSession(); 25 Transaction tx = session.beginTransaction(); 26 //保存 27 session.save(stu); 28 tx.commit(); 29 session.close(); 30 } 31 32 33 }
1 package cn.happy.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.junit.Test; 6 7 import cn.happy.entity.Student; 8 import cn.happy.util.HibernateUtil; 9 /** 10 * 删除 11 * @author Administrator 12 * 13 */ 14 public class delTest { 15 @Test 16 public void deltest(){ 17 Session session = HibernateUtil.getsession(); 18 Student stu=new Student(); 19 stu.setId(1); 20 Transaction tx = session.beginTransaction(); 21 session.delete(stu); 22 tx.commit(); 23 HibernateUtil.closeSession(); 24 System.out.println("del ok!"); 25 } 26 27 }
1 package cn.happy.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.junit.Test; 6 7 import cn.happy.entity.Student; 8 import cn.happy.util.HibernateUtil; 9 /** 10 * 查询 11 * @author Administrator 12 * 13 */ 14 public class getTest { 15 @Test 16 public void gettest() 17 { 18 Session session = HibernateUtil.getsession(); 19 Student stu=(Student)session.get(Student.class, 1); 20 Transaction tx=session.beginTransaction(); 21 System.out.println(stu); 22 } 23 24 }
package cn.happy.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.happy.entity.Student; import cn.happy.util.HibernateUtil; /** * 修改 * @author Administrator * */ public class updateTest { @Test public void updatetest(){ Session session = HibernateUtil.getsession(); Student stu=(Student)session.load(Student.class,1); stu.setName("李短"); Transaction tx=session.beginTransaction(); session.update(stu); tx.commit(); HibernateUtil.closeSession(); System.out.println("ok"); } }
以上是关于Hibernate框架增删改查的主要内容,如果未能解决你的问题,请参考以下文章