hibernate创建student类实现crud
时间: 2023-08-16 21:08:46 浏览: 117
Java框架篇?hibernate之CRUD操作
好的,以下是基于 Hibernate 框架创建 Student 类实现 CRUD:
1. 首先创建一个 Student 类,包含 id、name 和 age 三个属性,并在类上添加 `@Entity` 注解,用于将该类映射为数据库表。
```java
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int age;
// 构造方法、getter 和 setter 方法省略
}
```
2. 在 `hibernate.cfg.xml` 配置文件中配置数据库连接信息和 Hibernate 相关属性。
```xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
```
3. 创建一个 DAO(数据访问对象)类,用于对 Student 对象进行 CRUD 操作。在该类中通过 `SessionFactory` 获取 `Session` 对象,然后通过 `Session` 对象进行数据库操作。
```java
public class StudentDAO {
private final SessionFactory sessionFactory;
public StudentDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(Student student) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
public Student get(int id) {
Session session = sessionFactory.openSession();
Student student = null;
try {
student = session.get(Student.class, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return student;
}
public void update(Student student) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.update(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
public void delete(int id) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Student student = session.get(Student.class, id);
session.delete(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
}
```
4. 在应用程序中使用 StudentDAO 类进行 CRUD 操作。
```java
public class Application {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
StudentDAO studentDAO = new StudentDAO(sessionFactory);
// 创建 Student 对象并保存到数据库
Student student = new Student("Tom", 18);
studentDAO.save(student);
// 根据 id 获取 Student 对象并更新其 age 属性
student = studentDAO.get(1);
student.setAge(19);
studentDAO.update(student);
// 根据 id 删除 Student 对象
studentDAO.delete(1);
}
}
```
以上就是使用 Hibernate 框架创建 Student 类实现 CRUD 的过程。
阅读全文