Customer.java
public class Customer {
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(String cust_name, String cust_source) {
super();
this.cust_name = cust_name;
this.cust_source = cust_source;
}
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
//通过 ORM方式表示:一个客户对应的多个联系人
//防止的多的一方的集合。Hibernate默认使用的是Set集合
private Set<LinkMan> linkMans=new HashSet<LinkMan>();
//省略get和set方法
}
LinkMan.java
public class LinkMan {
private Long lkm_id;
private String lkm_name;
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;
//通过ORM方式表示:一个联系人只能属于一个客户
//放置的是一的一方的对象
private Customer customer;
//省略get和set方法
}
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>
<class name="com.hibernate.domain.Customer" table="cst_customer">
<!-- 建立OID与主键映射 -->
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<!-- 建立普通属性和表的字段的对应 -->
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
<!-- 配置一对多的映射:放置的多的一方的集合 -->
<!--
set标签
* name :多的一方的对象集合的属性名称
* cascade:级联
* inverse:放弃`外键`维护权 默认false:不放弃
-->
<set name="linkMans">
<!--
key标签
* column:多的一方的外键的名称
-->
<key column="lkm_cust_id"/>
<!--
one-to-many标签
* class :多的一方的全路径
-->
<one-to-many class="com.hibernate.domain.LinkMan"/>
</set>
</class>
</hibernate-mapping>
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>
<class name="com.hibernate.domain.LinkMan" table="cst_linkman">
<!-- 建立OID与主键映射 -->
<id name="lkm_id" column="lkm_id">
<generator class="native"/>
</id>
<!-- 建立普通属性与表字段映射 -->
<property name="lkm_name"/>
<property name="lkm_gender"/>
<property name="lkm_phone"/>
<property name="lkm_mobile"/>
<property name="lkm_email"/>
<property name="lkm_qq"/>
<property name="lkm_position"/>
<property name="lkm_memo"/>
<!-- 配置多对一的关系:放置的是一的一方的对象 -->
<!--
many-to-one标签
* name :一的一方的对象的属性名称
* class :一的一方的全路径
* column :在多的一方的表的外键的名称
-->
<many-to-one name="customer" cascade="save-update,delete" class="com.hibernate.domain.Customer" column="lkm_cust_id"/>
</class>
</hibernate-mapping>
src下的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>
<!-- 连接数据库的基本参数 -->
<!-- hibernate-release-5.0.7.Final\project\etc\hibernate.properties -->
<!-- localhost:3306 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day04</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!-- 配置Hibernate的方言 -->
<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>
<!-- 自动建表create update -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 设置事务隔离级别 -->
<!--
1 read uncommitted :脏读、不可重复读、虚读都会发生
2 read committed :解决脏读 (oracle默认)
4 repeatable read :解决脏读和不可重复读(mysql默认)
8 serializable :解决所有问题。串行执行、效率低
-->
<property name="hibernate.connection.isolation">4</property>
<!-- 配置当前线程绑定的Session -->
<!-- 不用session.close() -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 引入映射 -->
<mapping resource="com/hibernate/domain/Customer.hbm.xml"/>
<mapping resource="com/hibernate/domain/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.Test;
import com.hibernate.domain.Customer;
import com.hibernate.domain.LinkMan;
import com.hibernate.utils.HibernateUtils;
/**
* HQL的查询放置的测试类
*
* @author zhang
*
*/
public class HibernateDemo1 {
@Test
/**
* 初始化数据
*/
public void demo1() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 创建一个客户
Customer customer = new Customer();
customer.setCust_name("李向文");
for (int i = 1; i <= 10; i++) {
LinkMan linkMan = new LinkMan();
linkMan.setLkm_name("王东" + i);
linkMan.setCustomer(customer);
customer.getLinkMans().add(linkMan);
session.save(linkMan);
}
session.save(customer);
tx.commit();
}
@Test
/**
* HQL的简单查询
*/
public void demo2() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 简单的查询
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
// sql中支持*号的写法:select * from cst_customer;但是HQL中不支持*号的写法
/*
* Query query=session.createQuery("select * from cst_customer");//报错
* List<Customer> list=query.list();
*/
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
@Test
/**
* 别名查询
*/
public void demo3() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 别名查询
/*Query query = session.createQuery("from Customer c");
List<Customer> list = query.list();*/
Query query =session.createQuery("select c from Customer c");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
@Test
/**
* 排序查询
*/
public void demo4() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 排序查询
//默认情况
// List<Customer> list= session.createQuery("from Customer order by cust_id").list();
//设置排序降序 升序asc
List<Customer> list= session.createQuery("from Customer order by cust_id desc").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
@Test
/**
* 条件查询
*/
public void demo5() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 条件查询
//一、按位置绑定:根据参数的位置进行绑定
//一个条件
/*Query query = session.createQuery("from Customer where cust_name=?");
query.setParameter(0, "李兵");
List<Customer> list=query.list();*/
//多个条件
/*Query query = session.createQuery("from Customer where cust_name like ? and cust_source=?");
query.setParameter(0, "李%");
query.setParameter(1, "计算机");
List<Customer> list=query.list();*/
//二、按名称绑定
Query query = session.createQuery("from Customer where cust_source = :aaa and cust_name like :bbb");
//设置参数:
query.setParameter("aaa", "计算机");
query.setParameter("bbb", "李%");
List<Customer> list=query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
@Test
/**
* 投影查询
*/
public void demo6() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//投影查询
//单个属性
/*List<Object> list=session.createQuery("select c.cust_name from Customer c").list();
for (Object object : list) {
System.out.println(object);
}*/
//多个属性:
/*List<Object[]> list=session.createQuery("select c.cust_name,c.cust_source from Customer c").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
//查询多个属性,但是我想封装到对象中
List<Customer> list=session.createQuery("select new Customer(cust_name,cust_source) from Customer").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
@Test
/**
* 分页查询
*/
public void demo7() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//分页查询
Query query=session.createQuery("from LinkMan");
query.setFirstResult(10);
query.setMaxResults(10);
List<LinkMan> list=query.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
tx.commit();
}
@Test
/**
* 分组统计查询
*/
public void demo8() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//聚合函数的使用:count(),max(),min(),avg(),sum()
/*Object object=session.createQuery("select count(*) from Customer").uniqueResult();//只能查到一个结果
System.out.println(object);*/
//分组统计
List<Object[]> list=session.createQuery("select cust_source,count(*) from Customer group by cust_source").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
tx.commit();
}
@Test
/**
* HQL的多表查询
*/
public void demo9() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//SQL:select * from cst_customer c inner join cst_linkman l on c.cust_id=l.lkm_cust_id
//HQL:内连接from Customer c inner join c.linkMans
/*List<Object[]> list=session.createQuery("from Customer c inner join c.linkMans").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
//HQL:迫切内连接 其实就是在普通的内连接inner join后添加一个关键字fetch
//通知hibernate,将另一个对象的数据封装到该对象中
List<Customer> list=session.createQuery("select distinct c from Customer c inner join fetch c.linkMans").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
}
感觉HQL是和SQL比较相似的,基本上把表明换成类的属性名,总之还有细微的差别-----------一个当成对象操作映射操作表,另一个直接操作表。