新建Java工程,目录结构如下:其中jar包是目录中所有jar包和
驱动包两部分组成,别忘了最后buildpath一下
其中Student.java代码如下(就是一个简单java类)
package beans;
public class Student {
private Integer id;
private String name;
private int age;
private double score;
public Student() {
super();
}
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
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="beans.Student" table="t_student">
<id name="id" column="tid">
<generator class="native"/>
</id>
<property name="name" column="tname"/>
<property name="age" column="tage"/>
<property name="score" column="tscore"/>
</class>
</hibernate-mapping>
<?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="beans.Student" table="t_student">
<id name="id" column="tid">
<generator class="native"/>
</id>
<property name="name" column="tname"/>
<property name="age" column="tage"/>
<property name="score" column="tscore"/>
</class>
</hibernate-mapping>
hibernate.cfg.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="beans.Student" table="t_student">
<id name="id" column="tid">
<generator class="native"/>
</id>
<property name="name" column="tname"/>
<property name="age" column="tage"/>
<property name="score" column="tscore"/>
</class>
</hibernate-mapping>
MyTest.java代码如下:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.jupiter.api.Test;
import beans.Student;
public class MyTest {
@Test
public void testSave(){
//1.加载主配制文件
// Configuration configure = new Configuration().configure();
Configuration configure = new Configuration().configure().addClass(Student.class);
//2.创建session工厂
// SessionFactory sessionFactory = configure.buildSessionFactory();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
//3.获取session
Session session = sessionFactory.openSession();
try{
//4.开启事务
Transaction t = session.beginTransaction();
//5.操作
Student student = new Student("张三",23,93.5);
session.save(student);
//6.事务提交
t.commit();
Student student2 = session.get(Student.class, 1);
System.out.println(student2);
}catch(Exception e){
e.printStackTrace();
//7.事务回滚
session.getTransaction().markRollbackOnly();
}finally {
session.close();
sessionFactory.close();
}
}
}