📄 testmain.java
字号:
package com.lideedu.yame.test;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.lideedu.yame.tree.dao.ProductCategoryDAO;
import com.lideedu.yame.tree.dao.ProductDAO;
import com.lideedu.yame.tree.db.HibernateSessionFactory;
import com.lideedu.yame.tree.pojos.Product;
import com.lideedu.yame.tree.pojos.ProductCategory;
public class TestMain extends TestCase {
private Session session = null;
private Transaction tx = null;
@Override
protected void setUp() throws Exception {
super.setUp();
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
}
@SuppressWarnings("unchecked")
public void testInsert1(){
try {
/**
* 具体商品
*/
// 居家床品套件类具体商品
Product p1 = new Product();
p1.setProductName("摩登女孩-单人三件套");
p1.setProductPrice(103.5);
Product p2 = new Product();
p2.setProductName("床品斜纹四件套--香水百合");
p2.setProductPrice(200.6);
Product p3 = new Product();
p3.setProductName("出口丹麦超柔软纯棉高密斜纹2件套");
p3.setProductPrice(333.3);
// 儿童床品类具体商品
Product p4 = new Product();
p4.setProductName("迪斯尼顶级豪华公主床品四件套");
p4.setProductPrice(188.0);
// 烹饪锅具类具体商品
Product p5 = new Product();
p5.setProductName("心型煎蛋盘");
p5.setProductPrice(103.5);
Product p6 = new Product();
p6.setProductName("振能20CM奥尼复底汤锅");
p6.setProductPrice(80.5);
// 厨房刀具类具体商品
Product p7 = new Product();
p7.setProductName("砧板菜刀组合7件套");
p7.setProductPrice(46.8);
/**
* 商品类别
*/
// parent1_家居类
ProductCategory parent1 = new ProductCategory();
parent1.setCategoryName("家居");
// 家居类的子类-床上用品类
ProductCategory son1 = new ProductCategory();
son1.setCategoryName("床上用品");
// 床上用品类的子类-居家床品套件类
ProductCategory grandson1 = new ProductCategory();
grandson1.setCategoryName("居家床品套件");
// 床上用品类的子类-儿童床品类
ProductCategory grandson2 = new ProductCategory();
grandson2.setCategoryName("儿童床品");
// 家居类的子类-厨房及餐饮用品类
ProductCategory son2 = new ProductCategory();
son2.setCategoryName("厨房及餐饮用品");
// 厨房及餐饮用品类的子类-烹饪锅具类
ProductCategory grandson3 = new ProductCategory();
grandson3.setCategoryName("烹饪锅具");
// 厨房及餐饮用品类的子类-厨房刀具类
ProductCategory grandson4 = new ProductCategory();
grandson4.setCategoryName("厨房刀具");
/**
* 建立类别之间关系
*/
son1.setParentCategory(parent1);
son2.setParentCategory(parent1);
grandson1.setParentCategory(son1);
grandson2.setParentCategory(son1);
grandson3.setParentCategory(son2);
grandson4.setParentCategory(son2);
/**
* 商品归类
*/
grandson1.getProducts().add(p1);
p1.setProductCategory(grandson1);
grandson1.getProducts().add(p2);
p2.setProductCategory(grandson1);
grandson1.getProducts().add(p3);
p3.setProductCategory(grandson1);
grandson2.getProducts().add(p4);
p4.setProductCategory(grandson2);
grandson3.getProducts().add(p5);
p5.setProductCategory(grandson3);
grandson3.getProducts().add(p6);
p6.setProductCategory(grandson3);
grandson4.getProducts().add(p7);
p7.setProductCategory(grandson4);
// 持久化
session.save(grandson1);
session.save(grandson2);
session.save(grandson3);
session.save(grandson4);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if(tx != null)
tx.rollback();
}
}
@SuppressWarnings("unchecked")
public void testInsert2(){
try {
/**
* 具体商品
*/
// 武侠类具体商品
Product p1 = new Product();
p1.setProductName("诛仙6");
p1.setProductPrice(28.6);
Product p2 = new Product();
p2.setProductName("射雕英雄传(全四册)");
p2.setProductPrice(20.5);
Product p3 = new Product();
p3.setProductName("昆仑·天机卷");
p3.setProductPrice(30.8);
// 世界名著类具体商品
Product p4 = new Product();
p4.setProductName("麦田里的守望者");
p4.setProductPrice(10.0);
Product p5 = new Product();
p5.setProductName("少年维特之烦恼");
p5.setProductPrice(12.0);
// Java/J2EE类具体商品
Product p6 = new Product();
p6.setProductName("Java编程思想.第3版");
p6.setProductPrice(75.5);
Product p7 = new Product();
p7.setProductName("精通Struts:基于MVC的Java Web设计与开发");
p7.setProductPrice(39.5);
// ASP/ASP.NET类具体商品
Product p8 = new Product();
p8.setProductName("ASP.NET 2.0开发指南");
p8.setProductPrice(46.8);
Product p9 = new Product();
p9.setProductName("精通ASP.NET2.0企业级项目开发");
p9.setProductPrice(44.4);
/**
* 商品类别
*/
// parent1_图书类
ProductCategory parent1 = new ProductCategory();
parent1.setCategoryName("图书");
// 图书类的子类-小说类
ProductCategory son1 = new ProductCategory();
son1.setCategoryName("小说");
// 小说类的子类-武侠类
ProductCategory grandson1 = new ProductCategory();
grandson1.setCategoryName("武侠");
// 小说类的子类-世界名著类
ProductCategory grandson2 = new ProductCategory();
grandson2.setCategoryName("世界名著");
// 图书类的子类-计算机类
ProductCategory son2 = new ProductCategory();
son2.setCategoryName("计算机");
// 计算机类的子类-Java/J2EE类
ProductCategory grandson3 = new ProductCategory();
grandson3.setCategoryName("Java/J2EE");
// 计算机类的子类-ASP/ASP.NET类
ProductCategory grandson4 = new ProductCategory();
grandson4.setCategoryName("ASP/ASP.NET");
/**
* 建立类别之间关系
*/
son1.setParentCategory(parent1);
son2.setParentCategory(parent1);
grandson1.setParentCategory(son1);
grandson2.setParentCategory(son1);
grandson3.setParentCategory(son2);
grandson4.setParentCategory(son2);
/**
* 商品归类
*/
grandson1.getProducts().add(p1);
p1.setProductCategory(grandson1);
grandson1.getProducts().add(p2);
p2.setProductCategory(grandson1);
grandson1.getProducts().add(p3);
p3.setProductCategory(grandson1);
grandson2.getProducts().add(p4);
p4.setProductCategory(grandson2);
grandson2.getProducts().add(p5);
p5.setProductCategory(grandson2);
grandson3.getProducts().add(p6);
p6.setProductCategory(grandson3);
grandson3.getProducts().add(p7);
p7.setProductCategory(grandson3);
grandson4.getProducts().add(p8);
p8.setProductCategory(grandson4);
grandson4.getProducts().add(p9);
p9.setProductCategory(grandson4);
// 持久化
session.save(grandson1);
session.save(grandson2);
session.save(grandson3);
session.save(grandson4);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if(tx != null)
tx.rollback();
}
}
// 递归
private void printAllCategory(ProductCategory pc){
ProductDAO productDAO = new ProductDAO();
if(pc.getSubCategories().size()>0){
System.out.println(pc.getCategoryName()+":");
Set set = pc.getSubCategories();
for (Iterator iter = set.iterator(); iter.hasNext();) {
ProductCategory element = (ProductCategory) iter.next();
printAllCategory(element);
}
}else if(productDAO.queryByCategoryId(pc.getCategoryId()) != null){
List list = productDAO.queryByCategoryId(pc.getCategoryId());
System.out.println("\t"+pc.getCategoryName()+":");
for (Iterator iter = list.iterator(); iter.hasNext();) {
Product element = (Product) iter.next();
System.out.println("\t\t"+element.getProductName());
}
}else
System.out.println("\t"+pc.getCategoryName());
}
public void testQuery(){
ProductCategoryDAO pcDAO = new ProductCategoryDAO();
List list = pcDAO.queryAll();
for (Iterator iter = list.iterator(); iter.hasNext();) {
ProductCategory element = (ProductCategory) iter.next();
if(element.getParentCategory() == null){
printAllCategory(element);
}
}
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
HibernateSessionFactory.closeSession();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -