您的位置:首页 > 编程语言 > Java开发

一个测试jpa和Spring集成的文件

2009-09-29 22:29 417 查看
package junit.test;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.sql.DataSource;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
import com.itcast.util.QueryResult;

public class ProductTest extends TestCase {

/**
* jpa未和spring集成整合之前的测试方法,会扫描所有的实体类,生成所有的数据表(标记为entity的类)
*/
public void test(){
Persistence.createEntityManagerFactory("itcast");
}
/**
* jpa未和spring集成整合之前的测试方法,该方法将生成一条记录
*/
public void test1(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new ProductType());
em.getTransaction().commit();
em.close();
factory.close();
}

/**
* jpa未和spring集成整合之后的测试方法
*/
public void test2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
ProductType type = new ProductType();
type.setName("瑜伽用品");
type.setNote("好产品");
productService.save(type);
}

//测试spring 和 jpa是否集成好了
public void test3(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
DataSource dataSource = (DataSource)ctx.getBean("dataSource");
System.out.println("================="+ dataSource);
}

public void test4(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//这里的bean productService在beans.xml文件是不存在的,但是通过ProductServiceBean.java里的@Service注释也是可以的
ProductTypeService productService = (ProductTypeService)ctx.getBean("productServiceBean");
productService.save(new ProductType());
}

public void testSave(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
for(int i =1;i<20;i++){
ProductType type = new ProductType();
type.setName("足球"+i);
type.setNote("好产品");
type.setVisible(true);
productService.save(type);
}

}
public void testFind(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
ProductType type = productService.find(ProductType.class, 1);
Assert.assertNotNull("获取不到id为1的记录",type);//当type为Null时,打印出“获取不到id为1的记录”这句话
System.out.println("================typeid:=" + type.getTypeid() +"============type.name :" + type.getName());
}
public void testUpdate(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
ProductType type = productService.find(ProductType.class, 1);
type.setName("篮球");
productService.update(type);
}

public void testDel(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
productService.delete(ProductType.class,new Object[]{3,4,5});
}

public void testQueryResult(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");

QueryResult<ProductType> qr = productService.getScrollData(ProductType.class,3,6);
List<ProductType> list = qr.getResultList();
for(Iterator iter = list.iterator();iter.hasNext();){
ProductType pt = (ProductType)iter.next();
System.out.println("=======typeid: " + pt.getTypeid() + "===========name: " + pt.getName());
}
}

public void testQueryResultOrderby(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeService productService = (ProductTypeService)ctx.getBean("productTypeServiceBean");
LinkedHashMap<String,String> lhmap = new LinkedHashMap<String,String> ();
lhmap.put("visible", "asc");
//QueryResult<ProductType> qr = productService.getScrollData(ProductType.class,0,4);
QueryResult<ProductType> qr = productService.getScrollData(ProductType.class,"o.visible=?1",
new Object[]{true},0,10,lhmap);

List<ProductType> list = qr.getResultList();
System.out.println("=======count is:" + qr.getTotalrecord());

for(Iterator iter = list.iterator();iter.hasNext();){
ProductType pt = (ProductType)iter.next();
System.out.println("=======visible: " + pt.getVisible() + "===========name: " + pt.getName());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: