Hibernate学习六----------CRUD
© 版权声明:本文为博主原创文章,转载请注明出处
实例
1.项目结构

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate</groupId>
<artifactId>Hibernate-CRUD</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Hibernate-CRUD Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.1.6.Final</hibernate.version>
</properties>
<dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
<build>
<finalName>Hibernate-CRUD</finalName>
</build>
</project>
3.Student.java
package org.hibernate.model;
import java.util.Date;
public class Student {
private long sid;// 学号
private String sname;// 姓名
private String gender;// 性别
private Date birthday;// 生日
private String address;// 地址
public Student() {
}
public Student(String sname, String gender, Date birthday, String address) {
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public long getSid() {
return sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
}
4.Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.model.Student" table="STUDENT">
<id name="sid" type="java.lang.Long">
<column name="SID"/>
<generator class="native"/>
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"/>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"/>
</property>
<property name="birthday" type="date">
<column name="BIRTHDAY"/>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"/>
</property>
</class>
</hibernate-mapping>
5.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- SessionFactory相关配置 --> <session-factory> <!-- 数据库连接相关配置 --> <property name="connection.username">root</property> <property name="connection.password">***</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url"> jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8 </property> <!-- 常用配置 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 引入映射文件 --> <mapping resource="hbm/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
6.TestCRUD.java
package org.hibernate.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.model.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestCRUD {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before() {
Configuration config = new Configuration().configure();// 加载配置信息
sessionFactory = config.buildSessionFactory();// 创建SessionFactory对象
session = sessionFactory.openSession();// 创建Session对象
transaction = session.beginTransaction();// 开启事务
}
@After
public void after() {
transaction.commit();// 提交事务
session.close();// 关闭Session
sessionFactory.close();// 关闭SessionFactory
}
@Test
public void testCreate() {
Student student = new Student("张三", "男", new Date(), "北京市");// 创建Student对象
session.save(student);// 保存对象
}
@Test
public void testRetrieveByGet() {
Student student = session.get(Student.class, 1L);// 通过get查询信息
System.out.println(student);
}
@Test
public void testRetrieveByLoad() {
Student student = session.load(Student.class, 1L);// 通过load查询信息
System.out.println(student);
}
@Test
public void testUpdate() {
Student student = session.load(Student.class, 1L);// 通过load查询信息
student.setAddress("上海市");// 修改信息
session.update(student);// 保存修改信息
}
@Test
public void testDelete() {
Student student = session.load(Student.class, 1L);// 通过load查询信息
session.delete(student);// 删除信息
}
}
7.效果预览
7.1 执行testCreate()方法


7.2 执行testRetrieveByGet()方法(带输出语句)

7.3 执行testRetrieveByGet()方法(不带输出语句)

7.4 执行testRetrieveByGet()方法(将主键由1L改为2L)
@Test
public void testRetrieveByGet() {
Student student = session.get(Student.class, 2L);// 通过get查询信息
System.out.println(student);
}

7.5 执行testRetrieveByLoad()方法(带输出语句)

7.6 执行testRetrieveByLoad()方法(不带输出语句)

7.7 执行testRetrieveByLoad()方法(将主键由1L改为2L)
@Test
public void testRetrieveByLoad() {
Student student = session.load(Student.class, 2L);// 通过load查询信息
System.out.println(student);
}

7.8 执行testUpdate()方法


7.9 执行testDelete()方法


8.总结
- get和load的区别
1.在不考虑缓存的情况下,get方法会在调用之后立即向数据库发出SQL语句,返回持久化对象;load方法会在调用后返回一个代理对象,该代理对象只保存了实体对象的id,直到使用对象的非主键属性时才会发出SQL语句
2. 查询数据不存在的数据时,get方法返回null,load方法抛出异常org.hibernate.ObjectNotFoundException
© 版权声明:本文为博主原创文章,转载请注明出处

浙公网安备 33010602011771号