步骤:
创建Maven项目
步骤2:编辑pom.xml
添加Maven编译器插件
添加休眠核,Lombok,MySQL-Connector-Java Maven依赖项
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Hibernate6CRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hibernate6CRUD</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
步骤3:目录结构
步骤4:创建实体
Student.java
package com.test.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "sttab")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer sid;
private String sname;
private String sadd;
}
step5:hibernateutil.java
package com.test.util;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import com.test.entity.Student;
public class HibernateUtil {
static SessionFactory factory = null;
static {
Configuration cfg = new Configuration();
Properties props = new Properties();
props.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "root");
props.put(Environment.SHOW_SQL, true);
props.put(Environment.FORMAT_SQL, true);
props.put(Environment.HBM2DDL_AUTO, "update");
cfg.setProperties(props);
cfg.addAnnotatedClass(Student.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
步骤6:insert.java
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class Insert {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin the transaction
ses.beginTransaction();
//create student objs
Student st1 = new Student("sam","mumbai");
Student st2 = new Student("jhon","pune");
Student st3 = new Student("brock","nagpur");
//save
ses.persist(st1);
ses.persist(st2);
ses.persist(st3);
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
exceute insert.java
您将在下面的控制台中找到输出
表将在MySQL新模式中创建,如下
步骤7:retrive.java
通过ID检索学生
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class Retrive {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
Student st = ses.get(Student.class, 1);
System.out.println("emp with id 1 info "+st);
}catch(Exception e) {
e.printStackTrace();
}
}
}
步骤8:retriveall.java
package com.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class RetriveAll {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//select * from sttab
List<Student> list= ses.createQuery("from Student",Student.class).getResultList();
list.forEach(System.out::println);
}catch(Exception e) {
e.printStackTrace();
}
}
}
控制台输出
步骤9:update.java
更新一个记录
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class Update {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin treansaction
ses.beginTransaction();
Student st = ses.get(Student.class, 1);
st.setSadd("Delhi");
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
更新之前
更新后
控制台输出
步骤10:delete.java
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class Delete {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin transaction
ses.beginTransaction();
//get the student
Student st = ses.get(Student.class, 3);
//delete student
ses.remove(st);
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
删除之前的表
删除后