巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
来源:互联网 发布:前台list转json 编辑:程序博客网 时间:2024/04/30 03:36
package com.itcast.service.base;public interface DAO {/** * 保存实体 * @param entity 实体id */public void save(Object entity);/** * 更新实体 * @param entity 实体id */public void update(Object entity);/** * 删除实体 * @param entityid 实体id */public <T> void delete(Class<T> entityClass,Object entityid);/** * 删除实体 * @param entityids 实体id 数组。 */public <T> void delete(Class<T> entityClass,Object[] entityids);/** * 获取实体 * @param entityClass 实体类 * @param entityid 实体id * @return */public <T> T find(Class<T> entityClass, Object entityid);}
业务逻辑接口实现类:
package com.itcast.service.base;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;// 事务管理。 默认的事务行为。@Transactionalpublic abstract class DaoSupport implements DAO {// 注入实体管理器:@PersistenceContext protected EntityManager em;@Overridepublic <T> void delete(Class<T> entityClass,Object entityid) {delete(entityClass,new Object[]{entityid});//em.remove(em.getReference(entityClass, entityid));}@Overridepublic <T> void delete(Class<T> entityClass,Object[] entityids) {for(Object id : entityids){// 取得实体的托管对象 em.getReference(arg0, arg1)em.remove(em.getReference(entityClass, id));}}// 修改事物。1,只能读。2,修改事物传播行为:不需要开事物的。@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)public <T> T find(Class<T> entityClass, Object entityid) {return em.find(entityClass, entityid);}@Overridepublic void save(Object entity) {em.persist(entity);}@Overridepublic void update(Object entity) {// 当我们的实体bean 变成游离状态的时候,进行 更新。em.merge(entity);}}
package com.itcast.service.product;import com.itcast.service.base.DAO;public interface ProductTypeService extends DAO{}
package com.itcast.service.product.impl;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.itcast.service.base.DaoSupport;import com.itcast.service.product.ProductTypeService;@Service@Transactional public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {}
测试类:
package junit.test;import junit.framework.Assert;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itcast.bean.ProductType;import com.itcast.service.product.ProductTypeService;public class ProductTest {private static ApplicationContext cxt;private static ProductTypeService productService;@BeforeClasspublic static void setUpBeforeClass() throws Exception {try {cxt = new ClassPathXmlApplicationContext("beans.xml");productService = (ProductTypeService) cxt.getBean("productTypeServiceBean");} catch (Exception e) {e.printStackTrace();}}@Testpublic void testSave() {ProductType type = new ProductType();type.setName("ttt");type.setNote("good,ttt");productService.save(type);}@Testpublic void testFind() {ProductType type = productService.find(ProductType.class, 1);Assert.assertNotNull("获取不到id为1 的记录",type);}@Testpublic void testUpdate() {ProductType type = productService.find(ProductType.class, 1);type.setName("zuqiu");type.setNote("good,zuqiu");productService.update(type);}@Test // 物理删除。public void testDetele() {productService.delete(ProductType.class, 1);}}
经过测试,可以正常使用。
保存方法:
查找方法:
更新方法:
20节:重载业务管理Bean的删除方法----这个不是物理删除。
需知:
【1】 visible=?1 是位置参数。
【2】 .setParameter(1, false); 1为位置编号(也就是 visible=?1 的 1),false 所设定的参数值是什么。
【3】 o.typeid 为主键id
package com.itcast.service.product.impl;import javax.persistence.Query;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.itcast.service.base.DaoSupport;import com.itcast.service.product.ProductTypeService;@Service@Transactionalpublic class ProductTypeServiceBean extends DaoSupport implementsProductTypeService {@Overridepublic <T> void delete(Class<T> entityClass, Object[] entityids) {if (entityids!=null && entityids.length>0) {StringBuffer jpql = new StringBuffer();for(int i=0; i < entityids.length;i++){jpql.append("?").append(i+2).append(",");}// 组拼完会多一个逗号,所以要删除这个逗号。也就是最后一个字符。jpql.deleteCharAt(jpql.length()-1);// ?2, ?3 , ?4Query query = em.createQuery("update ProductType o set o.visible=?1 where o.typeid in("+ jpql.toString() + ")").setParameter(1, false);// 对参数进行 设定。 for(int i=0; i < entityids.length;i++){query.setParameter(i+2, entityids[i]);}query.executeUpdate(); // 更新一下。}}}
配置文件:可以参考
巴巴运动网 16.
- 巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
- 巴巴运动网-用泛型技术对产品分类的业务管理Bean抽象
- 巴巴运动网学习笔记三之产品分类实体对象基本属性的JPA映射,用泛型技术对产品分类的业务管理Bean抽象,单元测试产品分类的业务管理Bean.,重载业务管理Bean的删除方法,
- 用泛型技术对业务管理Bean抽象_实现_测试(代码示例)
- 巴巴运动网商品交易系统对用户最近浏览过的产品列表的技术方案
- 巴巴运动网 16 (产品分类实体对象基本属性的JPA映射)
- 巴巴运动网 17 无限级产品分类的双向一对多JPA映射
- 【j2ee spring】36、巴巴运动网的产品品牌图片
- 【j2ee spring】41、巴巴运动网的产品查询
- 巴巴运动网商品交易系统对图片的存放、下载以及压缩管理
- 巴巴运动网商品交易系统对分页的封装
- 巴巴运动网订单模块的锁定管理
- 【j2ee spring】34、巴巴运动网的产品类别管理
- 巴巴运动网技术点和功能
- 巴巴运动网技术点和功能
- 【j2ee spring】38、巴巴运动网的产品文件的上传
- 【j2ee spring】37、巴巴运动网的产品品牌修改和查询
- 【j2ee spring】40、巴巴运动网的产品添加修改选择
- JDBC访问所有数据库的完整步骤
- 嵌入式Linux启动过程中的问题积累
- 数据库之触发器
- 20120711讲座预告
- 算法导论 10.3-4 紧凑的多重数组
- 巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
- 常用算法思想--递归算法
- 应聘技巧回答
- 游戏王千金囊开发实录三—— 主窗体(自选试题)
- Ubuntu12.04回归gnome桌面
- Automatic Reference Counting
- Ubuntu 安装 ATI驱动 失败处理
- 架构师1
- iOS开发——keychain的使用