通过xml方式实现学生的增删改查
package com.java1234.model;
public class Student {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.java1234.model">
<class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
package com.java1234.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
private static SessionFactory buildSessionFactory(){
Configuration configuration=new Configuration().configure(); // 实例化配置文件
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
package com.java1234.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.java1234.model.Student;
import com.java1234.util.HibernateUtil;
public class StudentTest {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private void add(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Student s=new Student();
s.setName("张三");
session.save(s);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
private void delete(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Student student=(Student)session.get(Student.class, Long.valueOf(1));
session.delete(student);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
private void update(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Student student=(Student)session.get(Student.class, Long.valueOf(2));
student.setName("张三2");
session.save(student);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
private void getAllStudent(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
String hql="from Student";
Query query=session.createQuery(hql);
List<Student> studentList=query.list();
for(Student student:studentList){
System.out.println(student);
}
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
public static void main(String[] args) {
StudentTest studentTest=new StudentTest();
// studentTest.add();
// studentTest.delete();
// studentTest.update();
studentTest.getAllStudent();
}
}
通过注解方式实现教师的增删改查
package com.java1234.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="t_teacher")
public class Teacher {
private long id;
private String name;
@Id
@GeneratedValue(generator="_native")
@GenericGenerator(name="_native",strategy="native")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
}