在hibernate中创建一个java项目,加入hibernate支持。在创建java项目中,生成一个student类,该属性包括(id name gender number age description)。完成四个方法,分别是,savedelete update find。用于对学生对象的crud操作,在main方法中调用4个方法。
时间: 2024-03-24 12:39:35 浏览: 76
好的,我可以提供一个示例代码,展示如何在Hibernate中创建Java项目,并实现对Student对象的CRUD操作。
首先,您需要按照上面提到的步骤创建Java项目,并添加Hibernate依赖。然后,您需要在项目中创建Student类,并使用Hibernate注解指定映射关系。代码如下:
```java
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "gender")
private String gender;
@Column(name = "number")
private String number;
@Column(name = "age")
private Integer age;
@Column(name = "description")
private String description;
// getters and setters
}
```
接下来,您需要创建一个名为“HibernateUtil”的类,用于创建和管理Hibernate SessionFactory。代码如下:
```java
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
return configuration.buildSessionFactory(serviceRegistryBuilder.build());
} catch (Exception e) {
System.err.println("SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
```
然后,您需要创建一个名为“StudentDAO”的类,用于执行CRUD操作。代码如下:
```java
public class StudentDAO {
private SessionFactory sessionFactory;
public StudentDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(Student student) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
}
public void update(Student student) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.update(student);
session.getTransaction().commit();
}
public void delete(Student student) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.delete(student);
session.getTransaction().commit();
}
public Student findById(Long id) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student student = session.get(Student.class, id);
session.getTransaction().commit();
return student;
}
}
```
最后,您可以在main方法中调用四个方法,如下所示:
```java
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
StudentDAO studentDAO = new StudentDAO(sessionFactory);
// create a new student
Student student = new Student();
student.setName("John Doe");
student.setGender("Male");
student.setNumber("123456789");
student.setAge(20);
student.setDescription("A good student");
studentDAO.save(student);
// find the student by id
Student foundStudent = studentDAO.findById(student.getId());
System.out.println(foundStudent.getName());
// update the student
foundStudent.setName("Jane Doe");
studentDAO.update(foundStudent);
// delete the student
studentDAO.delete(foundStudent);
}
```
这样,就完成了对Student对象的CRUD操作。希望这个示例对您有所帮助!
阅读全文