1、实体类Customer.java
package model;
import java.util.HashSet;
import java.util.Set;
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//set集合表示多个联系人
private Set<LinkMan> linkMans = new HashSet<>();
public Customer() {
// TODO 自动生成的构造函数存根
}
public Customer(String cust_name, String cust_level) {
this.cust_name = cust_name;
this.cust_level = cust_level;
}
public Set<LinkMan> getLinkMans() {
return linkMans;
}
public void setLinkMans(Set<LinkMan> linkMans) {
this.linkMans = linkMans;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
2、实体类LinkMan.java
package model;
public class LinkMan {
private Integer lkm_id;
private String lkm_name;
//lkm_cust_id 外键,不用写
private String lkm_gender;
private String lkm_phone;
private String lkm_mobile;
private String lkm_email;
private String lkm_qq;
private String lkm_position;
private String lkm_memo;
//客户类
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Integer getLkm_id() {
return lkm_id;
}
public void setLkm_id(Integer lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public String getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(String lkm_gender) {
this.lkm_gender = lkm_gender;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
public String getLkm_mobile() {
return lkm_mobile;
}
public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
}
public String getLkm_email() {
return lkm_email;
}
public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
}
public String getLkm_qq() {
return lkm_qq;
}
public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
}
public String getLkm_position() {
return lkm_position;
}
public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
}
public String getLkm_memo() {
return lkm_memo;
}
public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
}
}
3、实体类映射文件Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- javabean与表之间的对应关系 -->
<class name="model.Customer" table="cst_customer">
<!-- 主键对应 -->
<id name="cust_id" column="cust_id">
<!-- 主键策略(与自增长相关) -->
<!-- hibernate框架来产生主键 整形 -->
<!-- increment 在多线程情况下不能用这种方式 -->
<!-- identity 主键由数据库产生,适用于mysql -->
<!-- oracle 主键由数据库产生,适用于oracle -->
<!-- uuid 产生32位随机数,String类型,可以在多线程情况下使用-->
<!-- assigned:唯一的自然主键生成方式,需要手动去设置主键-->
<!-- 默认:native -->
<generator class="native"></generator>
</id>
<!-- 其他字段 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!-- 配置一对多 -->
<!-- 配置级联保存:让瞬时态对象变成持久态 cascade="save-update"-->
<!-- 级联删除:cascade="delete"配置在一方-->
<!-- 配置孤儿删除:cascade="delete-orphan" -->
<!-- inverse="true"放弃外键维护 -->
<!-- cascade用来级联操作(保存、修改和删除)在一方设置 -->
<!-- inverse用来维护外键的 在一方设置 -->
<set name="linkMans" inverse="true">
<!-- 外键 -->
<key column="lkm_cust_id"></key>
<!-- 一对多关系 -->
<one-to-many class="model.LinkMan"/>
</set>
</class>
</hibernate-mapping>
4、实体类映射文件LinkMan.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- javabean与表之间的对应关系 -->
<class name="model.LinkMan" table="cst_linkman">
<!-- 主键对应 -->
<id name="lkm_id" column="lkm_id">
<!-- 主键策略(与自增长相关) -->
<!-- hibernate框架来产生主键 整形 -->
<!-- increment 在多线程情况下不能用这种方式 -->
<!-- identity 主键由数据库产生,适用于mysql -->
<!-- oracle 主键由数据库产生,适用于oracle -->
<!-- uuid 产生32位随机数,String类型,可以在多线程情况下使用-->
<!-- assigned:唯一的自然主键生成方式,需要手动去设置主键-->
<generator class="native"></generator>
</id>
<!-- 其他字段 -->
<property name="lkm_name" column="lkm_name"></property>
<property name="lkm_gender" column="lkm_gender"></property>
<property name="lkm_phone" column="lkm_phone"></property>
<property name="lkm_mobile" column="lkm_mobile"></property>
<property name="lkm_email" column="lkm_email"></property>
<property name="lkm_qq" column="lkm_qq"></property>
<property name="lkm_position" column="lkm_position"></property>
<property name="lkm_memo" column="lkm_memo"></property>
<!-- 多方配置 -->
<!-- name:LinkMan javabean中的客户属性 -->
<!-- class:一方的javabean -->
<!-- column:外键 -->
<!-- 配置级联保存,配置在多方有利于提升效率 cascade="save-update" 把瞬时态客户变为持久态 -->
<!-- 配置删除:cascade="delete" -->
<many-to-one name="customer" class="model.Customer" column="lkm_cust_id" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
5、hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//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/20171217hibernatetest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">你的密码</property>
<!-- 数据库方言 使用的数据库类型 -->
<property name="hibernate.dialect org.hibernate.dialect.MySQLDialect"></property>
<!-- 可选配置 -->
<!-- 显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 配置c3p0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最大空闲连接120秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 自动建表 删除,创建,删除:creat-drop 创建:creat 如果没有表就创建,有就同步:update(一般使用) 验证类和数据表结构是否一致:validate-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 把session绑定到当前线程,使用getCurrentSession获取当前session -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 映射文件 -->
<mapping resource="model/Customer.hbm.xml"/>
<mapping resource="model/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、工具类HibernateUtils.java
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
//使用一个静态块创建SessionFactory
private static Configuration CONFIG;
private static SessionFactory FACTORY;
static {
CONFIG = new Configuration().configure();
FACTORY = CONFIG.buildSessionFactory();
}
//从连接池中获取连接
public static Session openSession() {
return FACTORY.openSession();
}
//session绑定到线程
public static Session getCurrentSession() {
return FACTORY.getCurrentSession();
}
}
7、测试类TestHQL.java
package test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.junit.Before;
import org.junit.Test;
import model.Customer;
import model.LinkMan;
import util.HibernateUtils;
public class TestHQL {
private Session session;
private Transaction tr;
private String hql;
private List<Customer> list;
@Before
public void init() {
session = HibernateUtils.getCurrentSession();
tr = session.beginTransaction();
}
// 1、HQL基本的查询格式
@Test
public void testHql1() {
//必须写实体类,不能写表名
hql = "from Customer";
print();
}
// 2、使用别名的方式
//可以单独查询某几个字段
@Test
public void testHql2() {
//必须写实体类,不能写表名
hql = "from Customer c";
print();
}
// 3、排序查询
@Test
public void testHql3() {
//必须写实体类,不能写表名
//顺序的:hql = "from Customer order by cust_id";
//倒序
hql = "from Customer order by cust_id desc";
print();
}
// 4、分页查询
@Test
public void testHql4() {
hql = "from Customer";
Query query = session.createQuery(hql);
//设置分页
//设置起始索引
query.setFirstResult(1);//从第二条记录开始
//设置每页数据个数
query.setMaxResults(3);
print1(query);
}
// 5、带条件的查询--找出所有姓哈的
@Test
public void testHql5() {
hql = "from Customer where cust_name like ?";
Query query = session.createQuery(hql);
//设置?占位符的值
//第一个参数?的索引,从0开始
//第二个参数?对应的值
query.setParameter(0, "哈%");
print1(query);
}
@Test
public void testHql51() {
hql = "from Customer where cust_name like :name";
Query query = session.createQuery(hql);
//设置?占位符的值
//第一个参数具体替换占位值
//第二个参数替换的内容
query.setParameter("name", "哈%");
print1(query);
}
// 6、HQL的投影查询--查找所有的名字和级别
@Test
public void testHql6() {
hql = "select c.cust_name,c.cust_level from Customer c";
Query query = session.createQuery(hql);
List<Object[]> data = query.list();
for(Object[] objects : data) {
System.out.println(objects[0]+"-->"+objects[1]);
}
}
// 7、投影查询:如果查询两个字段,也可以把这两个字段封装到对象中
@Test
public void testHql7() {
hql = "select new Customer(c.cust_name,c.cust_level) from Customer c";
Query query = session.createQuery(hql);
print1(query);
}
// 8、聚合函数查询--获取总的记录数
@Test
public void testHql8() {
hql = "select count(c) from Customer c";
Query query = session.createQuery(hql);
Long o = (Long)query.uniqueResult();
System.out.println(o);
}
// 9、聚合函数查询--获取某一列数据的和
@Test
public void testHql9() {
hql = "select sum(c.cust_id) from Customer c";
Query query = session.createQuery(hql);
Long o = (Long)query.uniqueResult();
System.out.println(o);
}
// 10、分组的查询--查询所有客户及对应的联系人
@Test
public void testHql10() {
hql = "select c.cust_level from Customer c group by c.cust_level";
Query query = session.createQuery(hql);
List<Object[]> data = query.list();
System.out.println(data);
}
// 11、内连接查询--查询所有客户及对应的联系人
@Test
public void testHql11() {
hql = "from Customer c inner join c.linkMans";
Query query = session.createQuery(hql);
List<Object[]> data = query.list();
for(Object[] objects : data) {
Customer c = (Customer)objects[0];
LinkMan m = (LinkMan)objects[1];
System.out.println(c.getCust_name()+"-->"+m.getLkm_name());
}
}
// 12、被迫内连接
@Test
public void testHql12() {
hql = "from Customer c inner join fetch c.linkMans";
Query query = session.createQuery(hql);
List<Customer> data = query.list();
for(Customer customer : data) {
System.out.println(customer.getCust_name()+"-->"+customer.getLinkMans().size());
}
}
// 13、左外连接
@Test
public void testHql13() {
hql = "from Customer c left join c.linkMans";
Query query = session.createQuery(hql);
List<Object[]> data = query.list();
for(Object[] objects : data) {
Customer c = (Customer)objects[0];
LinkMan m = (LinkMan)objects[1];
System.out.println(c.getCust_name()+"-->"+m.getLkm_name());
}
}
// 14、被迫左外链接
@Test
public void testHql14() {
hql = "from Customer c left join fetch c.linkMans";
Query query = session.createQuery(hql);
List<Customer> data = query.list();
for(Customer customer : data) {
System.out.println(customer.getCust_name()+"-->"+customer.getLinkMans().size());
}
}
private void print1(Query query) {
list = query.list();
for(Customer customer : list){
System.out.println(customer.getCust_id()+"-->"+customer.getCust_name()+"-->"+customer.getCust_level());
}
}
private void print() {
Query query = session.createQuery(hql);
print1(query);
}
}