HQL数据查询基础(二)

2023-05-16

        继上回( HQL数据查询基础(一) )说到的例子——网上商店,来继续完善持久化类和配置文件的创建

       上回 在 com.imooc.model 包中创建 Seller.java 持久化类,这一次同样在 com.imooc.model 包中创建各个表对应的持久化类。

商品类 Commodity.java

<span style="font-size:18px;">package com.imooc.model;

import java.io.Serializable;

/**
 * Created by DreamBoy on 2016/5/19.
 */

/**
 * 商品信息持久化类
 */
public class Commodity implements Serializable {
    private Long id; //主键
    private String name; //名称
    private Double price; //价格
    private String unit; //单位
    private String category; //类别
    private String description; //简介
    private Seller seller; //商家

    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 Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Seller getSeller() {
        return seller;
    }

    public void setSeller(Seller seller) {
        this.seller = seller;
    }
}
</span>

Commodity.hbm.xml

<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.imooc.model.Commodity" table="COMMODITY">
		<id name="id" type="java.lang.Long">
			<column name="ID" />
			<generator class="increment" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="NAME" />
		</property>
		<property name="price" type="java.lang.Double">
			<column name="PRICE" />
		</property>
		<property name="unit" type="java.lang.String">
			<column name="UNIT" />
		</property>
		<property name="category" type="java.lang.String">
			<column name="CATEGORY" />
		</property>
		<property name="description" type="java.lang.String">
			<column name="DESCRIPTION" />
		</property>
		<many-to-one name="seller" class="com.imooc.model.Seller"
			fetch="join" >
			<column name="SELLER" />
		</many-to-one>
	</class>
</hibernate-mapping>
</span>

客户类 Customer.java

<span style="font-size:18px;">package com.imooc.model;

import java.io.Serializable;
import java.util.Date;

/**
 * 客户信息持久化类
 * 
 * @author Administrator
 * 
 */
public class Customer implements Serializable {

	private Long id;// 主键
	private String name;// 姓名
	private String tel;// 电话
	private String address;// 地址
	private String email;// 电子邮箱
	private String sex;// 性别
	private String description;// 自我介绍
	private Integer age;// 年龄
	private Date birthday;// 出生日期

	public Customer() {

	}

	public Customer(Long id, String name, String tel, String sex, Integer age,
			String email, String address) {
		this.id = id;
		this.name = name;
		this.tel = tel;
		this.sex = sex;
		this.age = age;
		this.email = email;
		this.address = address;

	}

	public Customer(Long id, String name) {
		this.id = id;
		this.name = name;
	}

	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 String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String toString() {
		return "主键:" + this.getId() + "    |    姓名:" + this.getName()
				+ "    |     电话:" + this.getTel() + "    |   性别 :"
				+ this.getSex() + "    |   年龄:" + this.getAge()
				+ "    |   出生日期:" + this.getBirthday() + "    |    地址:"
				+ this.getAddress() + "    |   电子邮箱:" + this.getEmail()
				+ "    |   自我介绍:" + this.getDescription();
	}

}
</span>

Customer.hbm.xml

<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.imooc.model.Customer" table="CUSTOMER">
		<id name="id" type="java.lang.Long">
			<column name="ID" />
			<generator class="increment" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="NAME" />
		</property>
		<property name="tel" type="java.lang.String">
			<column name="TEL" />
		</property>
		<property name="address" type="java.lang.String">
			<column name="ADDRESS" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="EMAIL" />
		</property>
		<property name="sex" type="java.lang.String">
			<column name="SEX" />
		</property>
		<property name="description" type="java.lang.String">
			<column name="DESCRIPTION" />
		</property>
		<property name="age" type="java.lang.Integer">
			<column name="AGE" />
		</property>
		<property name="birthday" type="java.util.Date">
			<column name="BIRTHDAY" />
		</property>
	</class>
</hibernate-mapping>
</span>


订单类 Order.java

<span style="font-size:18px;">package com.imooc.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 订 单信息持久化类
 * 
 * @author Administrator
 * 
 */
public class Order implements Serializable {
	private Long id;// 主键
	private Customer customer;// 客户
	private Date tradeDate;// 交易日期
	private String status;// 订单状态
	private Double amount;// 订单金额
	private Set<OrderItem> orderItems;// 订单明细

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Date getTradeDate() {
		return tradeDate;
	}

	public void setTradeDate(Date tradeDate) {
		this.tradeDate = tradeDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Set<OrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Set<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}

	public String toString() {
		return "订单主键:" + this.getId() + "    |    客户:"
				+ this.getCustomer().getName() + "    |     交易日期:"
				+ this.getTradeDate() + "    |   订单状态 :" + this.getStatus()
				+ "    |   订单金额:" + this.getAmount();
	}

}
</span>


Order.hbm.xml

<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.imooc.model.Order" table="ORDERFORM">
		<id name="id" type="java.lang.Long">
			<column name="ID" />
			<generator class="increment" />
		</id>
		<many-to-one name="customer" class="com.imooc.model.Customer"
			fetch="join">
			<column name="CUSTOMER" />
		</many-to-one>
		<property name="tradeDate" type="java.util.Date">
			<column name="TRADEDATE" />
		</property>
		<property name="status" type="java.lang.String">
			<column name="STATUS" />
		</property>
		<property name="amount" type="java.lang.Double">
			<column name="AMOUNT" />
		</property>
		  
		<set name="orderItems" inverse="true"  cascade="all" lazy="false">
			<key>
				<column name="ORDERID" />
			</key>
			<one-to-many class="com.imooc.model.OrderItem" />
		</set>
	</class>
</hibernate-mapping>
</span>


订单明细类 OrderItem.java

<span style="font-size:18px;">package com.imooc.model;

import java.io.Serializable;

/**
 * 订单明细信息持久化类
 * 
 * @author Administrator
 * 
 */
public class OrderItem implements Serializable {
	private Long id;// 主键
	private Order order;
	private Commodity commodity;// 订单商品
	private Double discount;// 折扣
	private Double actPrice;// 价格
	private Double amount;// 数量
	private Integer position;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}

	public Double getDiscount() {
		return discount;
	}

	public void setDiscount(Double discount) {
		this.discount = discount;
	}

	public Double getActPrice() {
		return actPrice;
	}

	public void setActPrice(Double actPrice) {
		this.actPrice = actPrice;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String toString() {
		return "订单明细主键:" + this.getId() + "    |    商品:"
				+ this.getCommodity().getName() + "    |     折扣:"
				+ this.getDiscount() + "    |   价格 :" + this.getActPrice()
				+ "    |   数量:" + this.getAmount();
	}

}
</span>


OrderItem.hbm.xml

<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.imooc.model.OrderItem" table="ORDERITEM">
		<id name="id" type="java.lang.Long">
			<column name="ID" />
			<generator class="increment" />
		</id>
		<many-to-one name="commodity" class="com.imooc.model.Commodity" fetch="join">
			<column name="COMMODITY" />
		</many-to-one>
		<many-to-one name="order" class="com.imooc.model.Order" fetch="join">
			<column name="ORDERID" />
		</many-to-one>
		<property name="discount" type="java.lang.Double">
			<column name="DISCOUNT" />
		</property>
		<property name="actPrice" type="java.lang.Double">
			<column name="ACTPRICE" />
		</property>
		<property name="amount" type="java.lang.Double">
			<column name="AMOUNT" />
		</property>
	</class>
</hibernate-mapping>
</span>


hibernate.cfg.xml 中增加 mapping 配置:

<span style="font-size:18px;">        <mapping resource="com/imooc/model/Seller.hbm.xml"/>
        <mapping resource="com/imooc/model/Customer.hbm.xml"/>
        <mapping resource="com/imooc/model/Commodity.hbm.xml"/>
        <mapping resource="com/imooc/model/Order.hbm.xml"/>
        <mapping resource="com/imooc/model/OrderItem.hbm.xml"/></span>

在目录 test 下的 com.imooc.model 包中创建新的测试类 : MyTest.java,如下:

<span style="font-size:18px;">package com.imooc.model;

import com.imooc.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by DreamBoy on 2016/5/19.
 */
public class MyTest {
    private Session session = null;

    @Before
    public void setUp() throws Exception {
        session = HibernateUtil.getSession();
    }

    @After
    public void tearDown() throws Exception {
        HibernateUtil.closeSession();
    }

    @Test
    public void testSeller() {
        String hql = " from Seller ";
        //创建Query实例对象
        Query query = session.createQuery(hql);
        List<Seller> sellers = query.list();

        for(Seller seller : sellers) {
            System.out.println(seller);
        }
    }
}
</span>







本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HQL数据查询基础(二) 的相关文章

随机推荐

  • 伸缩自如的时光轴实现——改进版

    上回讲到的是时光轴 伸缩自如 的实现 xff0c 如果基于响应式制作的话 xff0c 可能存在着许多潜在的BUG 如 xff1a 窗口变化时 xff0c 时光轴的 收起 和 展开 xff0c 都发生了一些变形 为此 xff0c 对原来的 t
  • 伸缩自如的时光轴实现_样式改版

    针对前几篇文章中实现的 伸缩自如 的时光轴 xff0c 对时光轴的样式进行又一次修改 xff0c 效果如下 xff1a 点击 收起 后 xff1a 修改后的 timeline css xff0c 如下 xff1a vertical time
  • ThinkPHP中的create方法与自动令牌验证

    转载自 xff1a Thinkphp中Create方法深入探究 ThinkPHP中的create方法与自动令牌验证实例教程 Thinkphp中Create方法深入探究 由于工作原因在thinkPHP的create 方法上遇到了问题 xff0
  • web安全之token和CSRF攻击

    上文我转载了两篇关于ThinkPHP令牌验证的文章 xff08 ThinkPHP中的create方法与自动令牌验证 xff09 其中提及到了 token xff0c 这里针对 token 的作用 xff0c 转载了另外两篇文章 xff08
  • java中的==、equals和hashCode以及hashCode生成

    转载自 xff1a xff08 点击打开链接 xff09 前言 java中 61 61 equals hashCode 都和对象的比较有关 xff0c 在java中这三者各有什么用处呢 xff0c 即java中为什么需要设计这三种对象的比较
  • javascript调用微信或QQ扫一扫

    项目里为了体验做的好点 xff0c 想直接通过js调用手机的扫一扫 xff1a 服务的用户主要是通过 xff1a 微信或QQ 之前使用过 微信或QQ的分享 腾讯移动WEB开发平台的 39 对外分享组件接口文档 39 http open mo
  • Java中的反射机制

    获取类的类类型的3种方式 xff0c 以及如何通过类的类类型创建实例对象 xff1f ClassDemo1 java package com reflect public class ClassDemo1 public static voi
  • Java中的自定义注解

    自定义注解 Description java xff08 这里自定义Description注解 xff09 package com ann test import java lang annotation Documented import
  • Java中自定义注解的应用

    来自 慕课网 的学习 我们可以使用自定义注解 xff0c 实现ORM xff0c 即对象 关系的映射 通过自定义注解 xff0c 定义对象对应数据表的属性 xff0c 如表名 xff0c 表字段等 Table java xff08 Tabl
  • Intellij IDEA下的第一个Hibernate项目

    参考 xff1a intellij配置hibernate自动生成hbm xml文件 从零开始用Intellij idea14创建hibernate项目 下面我要讲的创建方式 xff0c 可能更加原生态 xff0c 更加类似于Eclipse下
  • Intellij IDEA使用注解创建Hibernate项目中的OR映射类

    上回说到 xff1a Intellij IDEA下的第一个Hibernate项目 我们需要创建 对象到关系的映射配置文件 xff0c 如 entity hbm xml xff08 其中 entity 是我们将要创建的实体 xff09 下面讲
  • Hibernate中Blob对象类型的使用

    使用Intellij IDEA创建Hibernate项目 xff0c 目录结构如下 xff1a 其中 assets app png 为将要存储的照片 xff0c src hibernate cfg xml 为Hibernate的配置文件 x
  • Hibernate组件映射

    转载自 xff1a 点击打开链接 在Hibernate中 component 是某个实体的逻辑组成部分 xff0c 它与实体的根本区别是没有oid xff08 对象标识符 xff09 xff0c component是一个被包含的对象 它作为
  • Hibernate中的单向一对多关联

    源自 imooc 中的学习 Hibernate中的单向一对多关联 xff0c 这里举例 班级对学生 的单向一对多关联 xff0c 即一个班级可以有多个学生 那么在Hibernate中实体对象间如何体现出单向一对多的关联关系呢 xff1f 如
  • Hibernate中的单向多对一关联

    继上回讲到 Hibernate中的单向一对多关联 xff0c 这次来实现一下Hibernate中的单向多对一关联 对原来的项目修改如下 xff1a Hibernate中的单向多对一关联 xff0c 需要我们在多方增加一个一方的属性 xff0
  • Hibernate中的双向多对一关联以及 inverse属性、cascade属性的用法

    上回 说了 Hibernate中的单向一对多关联 和 Hibernate中的单向多对一关联 这次针对这两个 单向 进行整合即可实现双向的多对一关联 如 xff1a 学生与班级的关系 在Grade类中需要添加 Set集合保存Student对象
  • 优化器 optimizer

    优化器 optimizer optimizer 优化器 xff0c 用来根据参数的梯度进行沿梯度下降方向进行调整模型参数 xff0c 使得模型loss不断降低 xff0c 达到全局最低 xff0c 通过不断微调模型参数 xff0c 使得模型
  • Hibernate中的多对多关联

    源自 imooc 的学习 多对多关联是一种常见的关联关系 多对多关联关系一般采用中间表的形式来实现 xff0c 即新增一张包含关联双方主键的关联表 那么 xff0c 在Hibernate中如何实现多对多的关联关系呢 xff1f 多对多关联可
  • HQL数据查询基础(一)

    源自 imooc 的学习 什么是HQL呢 xff1f HQL 是Hibernate Query Language xff0c Hibernate查询语言 xff1b 同时HQL是一种面向对象的查询语言 xff0c HQL查询的主体是映射配置
  • HQL数据查询基础(二)

    继上回 xff08 HQL数据查询基础 xff08 一 xff09 xff09 说到的例子 网上商店 xff0c 来继续完善持久化类和配置文件的创建 上回 在 com imooc model 包中创建 Seller java 持久化类 xf