首页 > 代码库 > hibernate_06_单表操作_增删改操作

hibernate_06_单表操作_增删改操作

首先,创建类对象

技术分享
 1 package com.imooc.hibernate;
 2 
 3 public class Address {
 4 
 5     private String postcode; //邮编
 6     private String phone; //电话
 7     private String address; //地址
 8     
 9     public Address() {};
10     
11     public String getPostcode() {
12         return postcode;
13     }
14     public void setPostcode(String postcode) {
15         this.postcode = postcode;
16     }
17     public String getPhone() {
18         return phone;
19     }
20     public void setPhone(String phone) {
21         this.phone = phone;
22     }
23     public String getAddress() {
24         return address;
25     }
26     public void setAddress(String address) {
27         this.address = address;
28     }
29     public Address(String postcode, String phone, String address) {
30 //        super();
31         this.postcode = postcode;
32         this.phone = phone;
33         this.address = address;
34     }
35     @Override
36     public String toString() {
37         return "Address [postcode=" + postcode + ", phone=" + phone + ", address=" + address + "]";
38     }
39     
40 }
View Code
技术分享
 1 package com.imooc.hibernate;
 2 
 3 import java.sql.Blob;
 4 import java.util.Date;
 5 
 6 public class Students {
 7 
 8     private int sid;
 9     private String sname;
10     private String gender;
11     private Date birthday;
12     private Address address;
13     private Blob picture;
14     
15     public Blob getPicture() {
16         return picture;
17     }
18 
19     public void setPicture(Blob picture) {
20         this.picture = picture;
21     }
22 
23     public Students() {}
24 
25     public Students(int sid, String sname, String gender, Date birthday, Address address, Blob picture) {
26         super();
27         this.sid = sid;
28         this.sname = sname;
29         this.gender = gender;
30         this.birthday = birthday;
31         this.address = address;
32         this.picture = picture;
33     }
34 
35     @Override
36     public String toString() {
37         return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
38                 + ", address=" + address + ", picture=" + picture + "]";
39     }
40 
41     public Address getAddress() {
42         return address;
43     }
44 
45     public void setAddress(Address address) {
46         this.address = address;
47     }
48 
49     public int getSid() {
50         return sid;
51     }
52 
53     public void setSid(int sid) {
54         this.sid = sid;
55     }
56 
57     public String getSname() {
58         return sname;
59     }
60 
61     public void setSname(String sname) {
62         this.sname = sname;
63     }
64 
65     public String getGender() {
66         return gender;
67     }
68 
69     public void setGender(String gender) {
70         this.gender = gender;
71     }
72 
73     public Date getBirthday() {
74         return birthday;
75     }
76 
77     public void setBirthday(Date birthday) {
78         this.birthday = birthday;
79     }
80 
81 }
View Code

创建类对象的配置文件

技术分享
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.imooc.hibernate.Students" table="STUDENTS">
 7         <id name="sid" type="int">
 8             <column name="SID" />
 9             <generator class="native" />
10         </id>
11         <property name="sname" type="java.lang.String">
12             <column name="SNAME" />
13         </property>
14         <property name="gender" type="java.lang.String">
15             <column name="GENDER" />
16         </property>
17         <property name="birthday" type="java.util.Date">
18             <column name="BIRTHDAY" />
19         </property>
20         <property name="picture" type="java.sql.Blob">
21             <column name="PICTURE" />
22         </property>
23         
24         <component name="address" class="com.imooc.hibernate.Address">
25             <property name="postcode" column="POSTCODE"/>
26             <property name="phone" column="PHONE"/>
27             <property name="address" column="ADDRESS"/>
28         </component>
29     </class>
30 </hibernate-mapping>
View Code

创建hibernate配置文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.username">root</property>
 8         <property name="connection.password">root</property>
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12         <property name="show_sql">true</property>
13         <property name="format_sql">true</property>
14         <property name="hbm2ddl.auto">update</property>
15         
16         <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>
17     </session-factory>
18 </hibernate-configuration>
View Code

创建测试类

技术分享
 1 package com.icoom.test;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.sql.Blob;
 8 import java.util.Date;
 9 
10 import org.hibernate.Hibernate;
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.Transaction;
14 import org.hibernate.cfg.Configuration;
15 import org.hibernate.service.ServiceRegistry;
16 import org.hibernate.service.ServiceRegistryBuilder;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 
21 import com.imooc.hibernate.Address;
22 import com.imooc.hibernate.Students;
23 
24 public class StudentsTest {
25     
26     private SessionFactory sessionFactory;
27     private Session session;
28     private Transaction transaction;
29 
30     @Before
31     public void init() {
32         // 1.创建配置对象
33         Configuration config = new Configuration().configure();
34         // 2.创建服务注册对象
35         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
36         // 3.创建会话工厂对象
37         sessionFactory = config.buildSessionFactory(serviceRegistry);
38         // 会话对象
39         session = sessionFactory.openSession();
40         // 开启事务
41         transaction = session.beginTransaction();
42     }
43     
44     @After
45     public void destory() {
46         transaction.commit(); //提交事务
47         session.close(); //关闭session
48         sessionFactory.close();//关闭会话工厂
49     }
50     
51     @Test
52     public void testSaveStudents() {
53         Students s = new Students();
54         s.setSid(1);
55         s.setSname("老张");
56         s.setGender("男");
57         s.setBirthday(new Date());
58         Address address = new Address("250000", "1310531xxxx", "山东济南");
59         s.setAddress(address);
60         session.save(s);//保存对象进入数据库
61     }
62     
63     @Test
64     public void testGetStudents() {
65         Students s = (Students) session.get(Students.class, 1);
66         System.out.println(s);
67     }
68     
69     @Test
70     public void testLoadStudents() {
71         Students s = (Students) session.load(Students.class, 1);
72         System.out.println(s);
73     }
74     
75     @Test
76     public void testUpdateStudents() {
77         Students s = (Students) session.get(Students.class, 1);
78         s.setGender("女");
79         session.update(s);
80     }
81     
82     @Test
83     public void testDeleteStudents() {
84         Students s = (Students) session.get(Students.class, 1);
85         session.delete(s);
86     }
87 }
View Code

get和load方法的区别

  1. 在不考虑缓存的情况下,get方法会在调用之后立即向数据库发出sql语句,返回持久化对象;load方法会在调用后放回一个代理对象。该代理对象只保存了实体对象的id,知道使用对象的非主键属性时才会发出sql语句。
  2. 查询数据库中不存在的数据时,get方法返回null,load方法抛出org.hibernate.ObjectNotFoundException异常。

hibernate_06_单表操作_增删改操作