1 package com.itheima.domain; 2 3 import java.io.Serializable; 4 /** 5 * 客户的实体类 6 * @author zhang 7 * 8 */ 9 public class Customer implements Serializable { 10 11 private Long custId; 12 private String custName; 13 private String custSource; 14 private String custIndustry; 15 private String custLevel; 16 private String custAddress; 17 private String custPhone; 18 public Long getCustId() { 19 return custId; 20 } 21 public void setCustId(Long custId) { 22 this.custId = custId; 23 } 24 public String getCustName() { 25 return custName; 26 } 27 public void setCustName(String custName) { 28 this.custName = custName; 29 } 30 public String getCustSource() { 31 return custSource; 32 } 33 public void setCustSource(String custSource) { 34 this.custSource = custSource; 35 } 36 public String getCustIndustry() { 37 return custIndustry; 38 } 39 public void setCustIndustry(String custIndustry) { 40 this.custIndustry = custIndustry; 41 } 42 public String getCustLevel() { 43 return custLevel; 44 } 45 public void setCustLevel(String custLevel) { 46 this.custLevel = custLevel; 47 } 48 public String getCustAddress() { 49 return custAddress; 50 } 51 public void setCustAddress(String custAddress) { 52 this.custAddress = custAddress; 53 } 54 public String getCustPhone() { 55 return custPhone; 56 } 57 public void setCustPhone(String custPhone) { 58 this.custPhone = custPhone; 59 } 60 @Override 61 public String toString() { 62 return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource 63 + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress 64 + ", custPhone=" + custPhone + "]"; 65 } 66 67 68 69 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 在实体类所在的包下,创建一个xml文件。该文件建议名称为:实体类名称+.hbm+.xml 3 导入约束:dtd约束 4 --> 5 <!DOCTYPE hibernate-mapping PUBLIC 6 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 7 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 8 <hibernate-mapping package="com.itheima.domain"> 9 <class name="Customer" table="Customer"> 10 <id name="custId" column="cust_id"> 11 <!-- generator:是指定主键的生成方式。取值是固定的几个之中选一个 12 native是使用本地数据库的自动增长能力。 13 --> 14 <generator class="native"></generator> 15 </id> 16 <property name="custName" column="cust_name"></property> 17 <property name="custSource" column="cust_source"></property> 18 <property name="custIndustry" column="cust_industry"></property> 19 <property name="custLevel" column="cust_level"></property> 20 <property name="custAddress" column="cust_address"></property> 21 <property name="custPhone" column="cust_phone"></property> 22 </class> 23 </hibernate-mapping>
1 package com.itheima.utils; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 /** 8 * 抽取hibernate的工具类 9 * @author zhy 10 * 11 */ 12 public class HibernateUtil { 13 14 private static SessionFactory factory; 15 16 //了解:hibernate把可以预见的异常都转成了运行时异常 17 static{ 18 try { 19 Configuration cfg = new Configuration(); 20 cfg.configure(); 21 factory = cfg.buildSessionFactory(); 22 //System.out.println(factory); 23 } catch (ExceptionInInitializerError e) { 24 throw new ExceptionInInitializerError("初始化SessionFactory失败,请检查配置文件"); 25 } 26 } 27 28 /** 29 * 获取一个新的Session对象 30 * @return 31 */ 32 public static Session openSession(){ 33 return factory.openSession(); 34 } 35 }
1 package com.itheima.test; 2 3 import java.util.List; 4 5 import org.hibernate.SQLQuery; 6 import org.hibernate.Session; 7 import org.hibernate.Transaction; 8 import org.junit.Test; 9 10 import com.itheima.domain.Customer; 11 import com.itheima.utils.HibernateUtil; 12 13 /** 14 * hibernate的CRUD操作 15 * 16 * 17 * 18 */ 19 public class Demo { 20 21 //关于事务的回滚问题 22 @Test 23 public void testSave1(){//向数据库中添加信息 24 Customer c = new Customer(); 25 c.setCustName("测试保存功能1"); 26 c.setCustAddress("铁道大学"); 27 c.setCustIndustry("教学"); 28 c.setCustLevel("一般"); 29 c.setCustPhone("12452335232"); 30 c.setCustSource("河北省"); 31 32 Transaction tx = null; 33 Session s = null; 34 try{ 35 s = HibernateUtil.openSession(); 36 tx = s.beginTransaction(); 37 //保存客户 38 s.save(c); 39 tx.commit(); 40 }catch(Exception e){ 41 tx.rollback(); 42 }finally{ 43 s.close(); 44 } 45 } 46 47 48 49 50 51 @Test 52 public void testFindOne(){//查找 53 Session s = HibernateUtil.openSession(); 54 Transaction tx = s.beginTransaction(); 55 //查询id为5的客户 56 Customer c = s.get(Customer.class, 3L); 57 System.out.println(c); 58 tx.commit(); 59 s.close(); 60 } 61 62 63 @Test 64 public void testUpdate(){//数据更新操作 65 Session s = HibernateUtil.openSession(); 66 Transaction tx = s.beginTransaction(); 67 //查询id为5的客户 68 Customer c = s.get(Customer.class, 3L); 69 //修改客户的地址为:北京市顺义区 70 c.setCustAddress("石家庄铁道大学"); 71 c.setCustAddress("铁道大学更新"); 72 c.setCustIndustry("教学更新"); 73 c.setCustLevel("一般更新"); 74 c.setCustPhone("12452335232更新"); 75 c.setCustSource("河北省更新"); 76 //执行更新 77 s.update(c); 78 tx.commit(); 79 s.close(); 80 } 81 82 @Test 83 public void testDelete(){//删除数据记录 84 Session s = HibernateUtil.openSession(); 85 Transaction tx = s.beginTransaction(); 86 //查询id为4的客户 87 Customer c = s.get(Customer.class, 4L); 88 //删除id为4 89 s.delete(c); 90 tx.commit(); 91 s.close(); 92 } 93 94 @Test 95 public void testFindAll(){//按条件查找信息 96 Session s = HibernateUtil.openSession(); 97 Transaction tx = s.beginTransaction(); 98 //使用session对象,获取一个查询对象Query 99 SQLQuery sqlquery = s.createSQLQuery("select * from demo where cust_id= ? and cust_source= ? "); 100 //使用sqlquery对象获取结果集 101 //sqlquery.setParameter(0, "河北省"); 102 sqlquery.setParameter(0, 6l); 103 sqlquery.setParameter(1, "河北省"); 104 List<Object[]> list = sqlquery.list(); 105 for(Object[] os : list){ 106 System.out.println("------------数组中的内容-----------"); 107 for(Object o : os){ 108 System.out.println(o); 109 } 110 } 111 tx.commit(); 112 s.close(); 113 } 114 }