编写Hibernate5的配置文件hibernate.cfg.xml,编写加载配置文件的类HibernateSessionFactory,编写持久化类Teachinfo和映射文件Teachinfo.hbm.xml,编写一个类名为TeacherDao的JavaBean,该类封装了对教师信息的增删改查操作。
时间: 2023-06-24 09:06:15 浏览: 141
以下是Hibernate5的配置文件hibernate.cfg.xml:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
```
接下来是加载配置文件的类HibernateSessionFactory:
```java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory;
private HibernateSessionFactory() {
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
}
return sessionFactory;
}
}
```
然后是持久化类Teachinfo和映射文件Teachinfo.hbm.xml:
Teachinfo.java
```java
public class Teachinfo {
private int id;
private String name;
private int age;
private String sex;
public Teachinfo() {
}
public Teachinfo(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
// 省略getter和setter方法
}
```
Teachinfo.hbm.xml
```xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Teachinfo" table="teachinfo">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
<property name="sex" column="sex"/>
</class>
</hibernate-mapping>
```
最后是TeacherDao类,封装了对教师信息的增删改查操作:
```java
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TeacherDao {
public void add(Teachinfo teachinfo) {
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(teachinfo);
tx.commit();
}
public void delete(int id) {
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Teachinfo teachinfo = (Teachinfo) session.load(Teachinfo.class, id);
session.delete(teachinfo);
tx.commit();
}
public void update(Teachinfo teachinfo) {
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.update(teachinfo);
tx.commit();
}
public Teachinfo getById(int id) {
Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Teachinfo teachinfo = (Teachinfo) session.get(Teachinfo.class, id);
tx.commit();
return teachinfo;
}
}
```
阅读全文