一、先在官网 https://sourceforge.net/projects/hibernate/ 下载最新版本的开发包
下载解压后
二、新建一个 Java Project 即可
三、导入项目所需的jar包
四、配置Hibernate4框架各个层
创建数据库和表
CREATE DATABASE hibernate;
USE hibernate;
DROP TABLE IF EXISTS `t_class`;
CREATE TABLE `t_class` (
`classId` bigint(20) NOT NULL AUTO_INCREMENT,
`className` varchar(255) DEFAULT NULL,
PRIMARY KEY (`classId`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
`stuId` bigint(20) NOT NULL AUTO_INCREMENT,
`stuName` varchar(255) DEFAULT NULL,
`classId` bigint(20) DEFAULT NULL,
PRIMARY KEY (`stuId`),
KEY `FK_frxpowmeqnmf3sflhwgq3x0ub` (`classId`),
CONSTRAINT `FK_frxpowmeqnmf3sflhwgq3x0ub` FOREIGN KEY (`classId`) REFERENCES `t_class` (`classId`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate //hibernate为数据库名
root
123456 //数据库密码
org.hibernate.dialect.MySQL5Dialect
true
update
Student.hbm.xml
创建实体类
hsx.com.model.Student.java
package hsx.com.model;
public class Student {
private long id;
private String name;
private Class c;
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;
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
生成Session工厂
hsx.com.util.HibernateUtil.java
package hsx.com.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();
/* 获取Session工厂 */
return configuration.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
创建测试类
hsx.com.service.StudentTest.java
package hsx.com.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import hsx.com.model.Student;
import hsx.com.util.HibernateUtil;
/*XML版CRUD实现*/
public class StudentTest {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
public static void main(String[] args) {
StudentTest studentTest = new StudentTest();
studentTest.add();
// studentTest.delete();
// studentTest.update();
// studentTest.getAllStudent();
}
private void add() {
/* 生成一个session */
Session session = sessionFactory.openSession();
/* 开启事务 */
session.beginTransaction();
Student s = new Student();
s.setName("周杰伦");
session.save(s);
/* 提交事务 */
session.getTransaction().commit();
/* 关闭session */
session.close();
}
private void delete() {
/* 生成一个session */
Session session = sessionFactory.openSession();
/* 开启事务 */
session.beginTransaction();
Student student = (Student) session.get(Student.class, Long.valueOf(1));
session.delete(student);
/* 提交事务 */
session.getTransaction().commit();
/* 关闭session */
session.close();
}
private void update() {
/* 生成一个session */
Session session = sessionFactory.openSession();
/* 开启事务 */
session.beginTransaction();
Student student = (Student) session.get(Student.class, Long.valueOf(2));
student.setName("张三2");
session.save(student);
/* 提交事务 */
session.getTransaction().commit();
/* 关闭session */
session.close();
}
private void getAllStudent() {
/* 生成一个session */
Session session = sessionFactory.openSession();
/* 开启事务 */
session.beginTransaction();
String hql = "from Student";
Query query = session.createQuery(hql);
@SuppressWarnings("unchecked")
List studentList = query.list();
for (Student student : studentList) {
System.out.println(student);
}
/* 提交事务 */
session.getTransaction().commit();
/* 关闭session */
session.close();
}
}