Hibernate是ssh框架中的数据库部分,它采用的是ORM的思想即通过操作实体类对象去操作数据库。
PART_ONE:
记录下配置Hibernate的步骤:
1.下载Hibernate的jar包
这个可以自行到Hibernate官网中下载最新完整jar包
2.编写需要操作的实体类,我这里是客户类(Customer.class)
Customer.class:
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String gender;
private Integer age;
private String level;
private Set orders = new HashSet();
private Set roles = new HashSet();
private Card card;
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", gender=" + gender
+ ", age=" + age + ", level=" + level + "]";
}
}
3.创建客户类对应的hibernte对应数据库信息的xml(Customer.hbm.xml),这个文件与实体类处于同一位置下。
Customer.hbm.xml:
4.创建Hibernate的核心配置文件(hibernate.cfg.xml),这个文件放置在项目的src下。
hibernate.cfg.xml:
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernatelearn
root
org.hibernate.connection.C3P0ConnectionProvider
10
20
org.hibernate.dialect.MySQLDialect
true
true
update
5.将初始化的工作写成一个Hibernate的工具类(HibernateUtil.class)
HibernateUtil.class:
package Util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static Configuration cfg = null;
public static SessionFactory factory = null;
static {
cfg = new Configuration();
cfg.configure();
factory = cfg.buildSessionFactory();
}
public static Session getSession() {
return factory.openSession();
}
}
6.可以进行对数据库的操作,在test类中测试。
@Test
public void testSaveOrder() {
Customer test = new Customer();
test.setName("我是个测试);
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(test);
tx.commit();
session.close();
}
PART_TWO:
对数据进行CRUD的操作,Hibernate提供了三种方式分别是Hql,criteria,Sql.
Hql方式的演示:
package doTest;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.Test;
import Util.HibernateUtil;
import caixiaohao.entiy.Customer;
public class HqlTest {
//全表查询
@Test
public void test1() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
List customers = query.list();
for(Customer c : customers) {
System.out.println(c);
}
tx.commit();
session.close();
}
//条件查询
@Test
public void test2() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer where c_name like '%胖%' ");
List customers = query.list();
for(Customer c : customers) {
System.out.println(c);
}
tx.commit();
session.close();
}
//条件查询2
@Test
public void test3() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer where c_name like ? ");
query.setParameter(0, "%胖%");
List customers = query.list();
for(Customer c : customers) {
System.out.println(c);
}
tx.commit();
session.close();
}
//条件查询3
@Test
public void test4() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer where c_name like :name ");
query.setParameter("name", "%豪%");
List customers = query.list();
for(Customer c : customers) {
System.out.println(c);
}
tx.commit();
session.close();
}
//分页查询
@Test
public void test5() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
query.setFirstResult(0);
query.setMaxResults(4);
List customers = query.list();
for(Customer c : customers) {
System.out.println(c);
}
tx.commit();
session.close();
}
//聚合查询
@Test
public void test6() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select count(*) from Customer");
Long count = (Long) query.uniqueResult();
System.out.println(count);
// List customers = query.list();
// for(Customer c : customers) {
// System.out.println(c);
// }
tx.commit();
session.close();
}
//投影查询
@Test
public void test7() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select name from Customer");
List
Criteria演示:
package doTest;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.junit.jupiter.api.Test;
import Util.HibernateUtil;
import caixiaohao.entiy.Customer;
public class CriteriaTest {
//全表查询
@Test
public void test1() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria ce = session.createCriteria(Customer.class);
List list = ce.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
//条件查询
@Test
public void test2() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria ce = session.createCriteria(Customer.class);
ce.add(Restrictions.like("name", "%胖%"));
List list = ce.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
//分页查询
@Test
public void test3() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria ce = session.createCriteria(Customer.class);
ce.setFirstResult(2);
ce.setMaxResults(3);
List list = ce.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
//聚合查询
@Test
public void test4() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria ce = session.createCriteria(Customer.class);
ce.setProjection(Projections.count("id"));
Integer count = (Integer)ce.uniqueResult();
System.out.println(count);
// List list = ce.list();
// for (Customer customer : list) {
// System.out.println(customer);
// }
tx.commit();
session.close();
}
//投影查询
@Test
public void test5() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria ce = session.createCriteria(Customer.class);
ProjectionList plist = Projections.projectionList();
plist.add(Property.forName("id"));
plist.add(Property.forName("name"));
ce.setProjection(plist);
List
Sql方式的演示:
package doTest;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.Test;
import Util.HibernateUtil;
public class SqlTest {
@Test
public void test1() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
SQLQuery sqlQuery = session.createSQLQuery("select * from t_order");
List
PART_THREE
Hibernate整合C3P0:
1.在下好的Hibernate的jar中有C3P0的依赖jar
2.在hibernat.cfg.xml加入C3P0的配置
org.hibernate.connection.C3P0ConnectionProvider
10
20
3.测试C3P0
package doTest;
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.jdbc.Work;
import Util.HibernateUtil;
import caixiaohao.entiy.Customer;
import caixiaohao.entiy.Order;
public class test {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();
session.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
System.out.println(connection);
}
});
session.close();
}
}
结果
PART_FOUR
hibernate的配置就到此,它还有相关概念需要自行去深入了解,例如一级缓存,二级缓存,快照,session的瞬时态,持久态,脱管态,lazy加载还有其注释的方式去进行配置等等。