首页 > 代码库 > Hibernate框架增删改查
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 //方法返回session14 public static Session getsession(){15 return factory.openSession();16 }17 //关闭session18 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 @Override13 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 Administrator13 *14 */15 public class addTest {16 @Test17 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 Administrator12 *13 */14 public class delTest {15 @Test16 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 Administrator12 *13 */14 public class getTest {15 @Test16 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框架增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。