Hibernate的入门(增删改查):
Posted glacial_water
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate的入门(增删改查):相关的知识,希望对你有一定的参考价值。
注意:本次的记录是在上一篇Hibernate入门的基础上应用的
1.目录
2.实体类修改
1 package com.itheima.domain; 2 3 /* 4 * 客户的javaBean 5 * @author chenyanlong 6 */ 7 public class Customer { 8 private Long cust_id; 9 private String cust_name; 10 private Long cust_user_id; 11 private Long cust_create_id; 12 private String cust_source; 13 private String cust_industry; 14 private String cust_level; 15 private String cust_linkman; 16 private String cust_phone; 17 private String cust_mobile; 18 public Long getCust_id() { 19 return cust_id; 20 } 21 public void setCust_id(Long cust_id) { 22 this.cust_id = cust_id; 23 } 24 public String getCust_name() { 25 return cust_name; 26 } 27 public void setCust_name(String cust_name) { 28 this.cust_name = cust_name; 29 } 30 public Long getCust_user_id() { 31 return cust_user_id; 32 } 33 public void setCust_user_id(Long cust_user_id) { 34 this.cust_user_id = cust_user_id; 35 } 36 public Long getCust_create_id() { 37 return cust_create_id; 38 } 39 public void setCust_create_id(Long cust_create_id) { 40 this.cust_create_id = cust_create_id; 41 } 42 public String getCust_source() { 43 return cust_source; 44 } 45 public void setCust_source(String cust_source) { 46 this.cust_source = cust_source; 47 } 48 public String getCust_industry() { 49 return cust_industry; 50 } 51 public void setCust_industry(String cust_industry) { 52 this.cust_industry = cust_industry; 53 } 54 public String getCust_level() { 55 return cust_level; 56 } 57 public void setCust_level(String cust_level) { 58 this.cust_level = cust_level; 59 } 60 public String getCust_linkman() { 61 return cust_linkman; 62 } 63 public void setCust_linkman(String cust_linkman) { 64 this.cust_linkman = cust_linkman; 65 } 66 public String getCust_phone() { 67 return cust_phone; 68 } 69 public void setCust_phone(String cust_phone) { 70 this.cust_phone = cust_phone; 71 } 72 public String getCust_mobile() { 73 return cust_mobile; 74 } 75 public void setCust_mobile(String cust_mobile) { 76 this.cust_mobile = cust_mobile; 77 } 78 @Override 79 public String toString() { 80 return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id 81 + ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry=" 82 + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone=" 83 + cust_phone + ", cust_mobile=" + cust_mobile + "]"; 84 } 85 86 87 }
3.HibernateUtils.java
1 package com.itheima.utils; 2 3 import javax.servlet.jsp.jstl.core.Config; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.cfg.Configuration; 8 9 /* 10 * Hibernate框架的工具 11 * @author chenyanlong 12 */ 13 public class HibernateUtils { 14 15 //Curl+shift+x 16 private static final Configuration CONFIG; 17 private static final SessionFactory FACTORY; 18 19 //编写静态代码 20 static { 21 //加载配置文件 22 CONFIG =new Configuration().configure(); 23 //构造工厂 24 FACTORY=CONFIG.buildSessionFactory(); 25 } 26 27 /* 28 * 从工厂获取Session对象 29 * @return 30 */ 31 public static Session getSession() { 32 return FACTORY.openSession(); 33 34 } 35 }
4. 保存数据
1 /* 2 * 1 保存数据 3 */ 4 @Test 5 public void testSave(){ 6 /* 7 * 1.先加载配置文件 8 * 2.创建SessionFactory对象,生成Session对象 9 * 3.创建Sesison对象 10 * 4.开启事务 11 * 5.编写保存代码 12 * 6.提交事务 13 * 7.释放资源 14 */ 15 16 //1.加载配置文件 17 Configuration config=new Configuration(); 18 //默认加载src目录下hibernate.cfg.xml的配置文件 19 config.configure(); 20 //2.创建SessionFactory对象 21 SessionFactory factory=config.buildSessionFactory(); 22 //3.创建session对象 23 Session session=factory.openSession(); 24 //4.开启事务 25 Transaction tr= session.beginTransaction(); 26 27 //5.编写保存代码 28 Customer customer = new Customer(); 29 customer.setCust_name("小李"); 30 customer.setCust_source("小广告"); 31 32 33 session.save(customer); 34 //6.提交事务 35 tr.commit(); 36 37 //7.释放资源 38 session.close(); 39 factory.close(); 40 } 41
5.测试工具类
1 /* 2 * 2 测试工具类 3 */ 4 @Test 5 public void testSave2(){ 6 //原来:加载配置文件,获取Factory对象,获取Session 7 Session session=HibernateUtils.getSession(); 8 Transaction tr=session.beginTransaction(); 9 Customer c=new Customer(); 10 c.setCust_name("小陈"); 11 session.save(c); 12 13 //提交事务 14 tr.commit(); 15 //释放资源 16 session.close(); 17 18 }
6.测试get()方法
1 /* 2 * 3 测试get()方法,获取查询,通过主键查询一条记录 3 */ 4 @Test 5 public void testSave3(){ 6 //原来:加载配置文件,获取Factory对象,获取Session 7 Session session=HibernateUtils.getSession(); 8 Transaction tr=session.beginTransaction(); 9 10 //测试查询的两个参数,arg0查询javaBean的class对象,arg1主键的值 11 Customer c=session.get(Customer.class, 95L); 12 System.out.println(c); 13 14 15 //提交事务 16 tr.commit(); 17 //释放资源 18 session.close(); 19 }
7.删除方法
1 /* 2 * 4 测试删除的方法 3 * 注意:删除或者修改,先查询再删除或者修改 4 */ 5 @Test 6 public void testDel(){ 7 //原来:加载配置文件,获取Factory对象,获取Session 8 Session session=HibernateUtils.getSession(); 9 Transaction tr=session.beginTransaction(); 10 11 //测试查询的两个参数,arg0查询javaBean的class对象,arg1主键的值 12 Customer c=session.get(Customer.class, 95L); 13 System.out.println(c); 14 15 //删除客户 16 session.delete(c); 17 18 //提交事务 19 tr.commit(); 20 //释放资源 21 session.close(); 22 } 23
8.测试修改
1 /* 2 *5 测试修改 3 */ 4 @Test 5 public void testUpdate(){ 6 //原来:加载配置文件,获取Factory对象,获取Session 7 Session session=HibernateUtils.getSession(); 8 Transaction tr=session.beginTransaction(); 9 10 //测试查询的两个参数,arg0查询javaBean的class对象,arg1主键的值 11 Customer c=session.get(Customer.class, 94L); 12 System.out.println(c); 13 14 //设置客户信息 15 c.setCust_name("小龙虾"); 16 17 //修改或是更新 18 session.update(c); 19 20 //session.saveOrUpdate(c); 添加或是修改 21 //提交事务 22 tr.commit(); 23 //释放资源 24 session.close(); 25 }
9.测试查询方法
1 /* 2 * 测试查询方法 3 */ 4 @Test 5 public void testSel() { 6 //加载配置文件 7 Session session =HibernateUtils.getSession(); 8 Transaction tr=session.beginTransaction(); 9 10 //创建查询的接口 11 Query query=session.createQuery("from Customer"); 12 13 //查询所有的数据 14 List<Customer> list=query.list(); 15 for(Customer customer:list) { 16 System.out.println(customer); 17 } 18 19 //提交事务 20 tr.commit(); 21 22 //释放资源 23 session.close(); 24 25 }
以上是关于Hibernate的入门(增删改查):的主要内容,如果未能解决你的问题,请参考以下文章