继上回( 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(使用前将#替换为@)