1、创建一个项目,导入hibernate所需要的jar包
官网下载的hibernate包(hibernate-release-4.3.5.Final)的hibernate-release-4.3.5.Final\lib\required路径下
然后Build Path,添加到项目里面
2、还需要数据库驱动包->Build Path
3、导入hibernate.cfg.xml在src目录下
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate
root
123456
org.hibernate.dialect.MySQL5Dialect
true
update
4、Hibernate是对象关系映射,先建立一个对象
Student.java
在model下创建配置文件:类名.hbm.xml(Student.hbm.xml),复制以下信息
开始写映射信息
5、在hibernate.cfg.xml导入映射文件
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate
root
123456
org.hibernate.dialect.MySQL5Dialect
true
update
6、创建数据库。名称:hibernate
7、写测试类StudentTest.class
基本模板
package com.java.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class StudentTest {
public static void main(String[] args) {
//1.实例化(加载classpath下的)配置文件hibernate.cfg.xml
Configuration configuration=new Configuration().configure();
//2.实例化服务登记
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
//3.通过serviceRegistry创建session工厂
SessionFactory sessionFactory= configuration.buildSessionFactory(serviceRegistry);
//通过sessionFactory创建的session来实现对象操作数据库(持久化)
Session session=sessionFactory.openSession();
session.beginTransaction();//开启事务
session.getTransaction().commit();//提交事务
session.close();//关闭session
sessionFactory.close();//关闭session工厂
}
}
运行项目,查看数据库是否自动生成表和字段
8、对象数据持久化
package com.java.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.java.model.Student;
public class StudentTest {
public static void main(String[] args) {
//1.实例化(加载classpath下的)配置文件hibernate.cfg.xml
Configuration configuration=new Configuration().configure();
//2.实例化服务登记
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
//3.通过serviceRegistry创建session工厂
SessionFactory sessionFactory= configuration.buildSessionFactory(serviceRegistry);
//通过sessionFactory创建的session来实现对象操作数据库(持久化)
Session session=sessionFactory.openSession();
session.beginTransaction();//开启事务
//写数据操作
Student s=new Student();
s.setName("张三");
session.save(s);
session.getTransaction().commit();//提交事务
session.close();//关闭session
sessionFactory.close();//关闭session工厂
}
}
结果:数据库插入一条新的数据
9、HibernateUtil封装
因为每次写增删查改要写
封装
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class hibernateUtil {
public static final SessionFactory sessionFactory=bulidSessionFactory();
public static SessionFactory bulidSessionFactory(){
//1.实例化(加载classpath下的)配置文件hibernate.cfg.xml
Configuration configuration=new Configuration().configure();
//2.实例化服务登记
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
//3.通过serviceRegistry创建session工厂并返回
return configuration.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
在StudentTest使用HibernateUtil
package com.java.service;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.java.model.Student;
import com.java.util.hibernateUtil;
public class StudentTest {
public static void main(String[] args) {
SessionFactory sessionFactory= hibernateUtil.getSessionFactory();
//通过sessionFactory创建的session来实现对象操作数据库(持久化)
Session session=sessionFactory.openSession();
session.beginTransaction();//开启事务
//写数据操作
Student s=new Student();
s.setName("张三");
session.save(s);
session.getTransaction().commit();//提交事务
session.close();//关闭session
sessionFactory.close();//关闭session工厂
}
}
10、CRUD操作(增删查改)
StudentTest.java
package com.java.service;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.java.model.Student;
import com.java.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.class, Long.valueOf(1)==实体类名.class,主键数据类型.valueOf(主键值)
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 studentList=query.list();
//打印要在model里的Student.java重写public String toString()
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();
}
}
转载于:https://my.oschina.net/u/3848699/blog/2222338