用泛型技术对业务管理Bean抽象_实现_测试(代码示例)
2009-03-09 14:49
766 查看
DAO.java
package com.itcast.service.base;
public interface DAO {
/**
* 保存实体
* @param entity
*/
public void save(Object entity);
/**
* 更新实体
* @param entity
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityid 实体id
*/
public <T> void delete(Class<T> entityClass, Object entityid);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public <T> void delete(Class<T> entityClass, Object[] entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public <T> T find(Class<T> entityClass, Object entityId);
}
DaoSupport.java
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;
/**这个类的所有业务方法都会引用事务注解定义的默认行为,默认会开启和提交事物**/
@Transactional
public abstract class DaoSupport implements DAO {
/**
* 通过spring注入一个实体管理器
* JPA是通过 EnityManager进行数据库的操作,所以必须注入 EnityManager
* @PersistenceContext 注入EnityManager
*/
@PersistenceContext protected EntityManager em;
/**
* 1 AOP
* 2 EntityManager em = 实体赋值;
* 3 在调用此方法前,spring容器会根据Transactional属性默认行为方法开启一个事务
* 4 打开事务
*/
@Override
public <T> void delete(Class<T> entityClass, Object entityid) {
/**getReference取得实体的引用**/
delete(entityClass, new Object[]{entityid});
}
/** 5 关闭事务**/
@Override
public <T> void delete(Class<T> entityClass,Object[] entityids) {
for(Object id : entityids){
em.remove(em.getReference(entityClass, id));
}
}
/** 一般查询不需要事务处理,propagation=Propagation.NOT_SUPPORTED指示此方法不开启事务 **/
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
@Override
public <T> T find(Class<T> entityClass, Object entityId) {
return em.find(entityClass, entityId);
}
@Override
public void save(Object entity) {
em.persist(entity);
}
/** 一般在我们的实体bean变成游离状态的时候,调用此方法,对实体bean进行更改操作 **/
@Override
public void update(Object entity) {
//把实体bean数据从新同步到数据库
em.merge(entity);
}
}
ProductTypeService.java
package com.itcast.service.product;
import com.itcast.service.base.DAO;
public interface ProductTypeService extends DAO{
}
ProductTypeServiceBean.java
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;
//为了让bean纳入spring容器管理
@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {
//重载业务管理Bean的删除方法
@Override
public <T> void delete(Class<T> entityClass, Object[] entityids) {
StringBuffer jpql = new StringBuffer();
if(entityids!=null && entityids.length>0){
for(int i=0;i<entityids.length;i++){
jpql.append("?").append(i+2).append(",");
}
jpql.deleteCharAt(jpql.length()-1);
//createQuery()是JPQL查询的方法,不是本地查询(sql语句),JPQL语句是面向对象,jpa规范的查询语言
Query 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();
}
}
}
ProductTest.java
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;
import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
public class ProductTest {
private static ApplicationContext cxt;
private static ProductTypeService productTypeService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
/** 启动spring容器 **/
cxt = new ClassPathXmlApplicationContext("beans.xml");
/** 用接口去引用spring给我们返回的对象 **/
productTypeService = (ProductTypeService)cxt.getBean("productTypeServiceBean");
} catch (Exception e){
e.printStackTrace();
}
}
/*
* 添加单元测试点
* (@Test)
*/
@Test
public void testSave(){
ProductType type = new ProductType();
type.setName("篮球用品");
type.setNote("..产品");
productTypeService.save(type);
}
@Test
public void testFind(){
ProductType type = productTypeService.find(ProductType.class, 2);
org.junit.Assert.assertNotNull("获取不到id为2的记录", type);
}
@Test
public void testUpdate(){
/** 返回一个游离状态,因为业务逻辑方法执行完后,会关闭事务 **/
ProductType type = productTypeService.find(ProductType.class, 5);
type.setName("足球用品");
type.setNote("sd产品");
productTypeService.update(type);
}
@Test
public void testDelete(){
productTypeService.delete(ProductType.class, 2);
}
}
package com.itcast.service.base;
public interface DAO {
/**
* 保存实体
* @param entity
*/
public void save(Object entity);
/**
* 更新实体
* @param entity
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityid 实体id
*/
public <T> void delete(Class<T> entityClass, Object entityid);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public <T> void delete(Class<T> entityClass, Object[] entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public <T> T find(Class<T> entityClass, Object entityId);
}
DaoSupport.java
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;
/**这个类的所有业务方法都会引用事务注解定义的默认行为,默认会开启和提交事物**/
@Transactional
public abstract class DaoSupport implements DAO {
/**
* 通过spring注入一个实体管理器
* JPA是通过 EnityManager进行数据库的操作,所以必须注入 EnityManager
* @PersistenceContext 注入EnityManager
*/
@PersistenceContext protected EntityManager em;
/**
* 1 AOP
* 2 EntityManager em = 实体赋值;
* 3 在调用此方法前,spring容器会根据Transactional属性默认行为方法开启一个事务
* 4 打开事务
*/
@Override
public <T> void delete(Class<T> entityClass, Object entityid) {
/**getReference取得实体的引用**/
delete(entityClass, new Object[]{entityid});
}
/** 5 关闭事务**/
@Override
public <T> void delete(Class<T> entityClass,Object[] entityids) {
for(Object id : entityids){
em.remove(em.getReference(entityClass, id));
}
}
/** 一般查询不需要事务处理,propagation=Propagation.NOT_SUPPORTED指示此方法不开启事务 **/
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
@Override
public <T> T find(Class<T> entityClass, Object entityId) {
return em.find(entityClass, entityId);
}
@Override
public void save(Object entity) {
em.persist(entity);
}
/** 一般在我们的实体bean变成游离状态的时候,调用此方法,对实体bean进行更改操作 **/
@Override
public void update(Object entity) {
//把实体bean数据从新同步到数据库
em.merge(entity);
}
}
ProductTypeService.java
package com.itcast.service.product;
import com.itcast.service.base.DAO;
public interface ProductTypeService extends DAO{
}
ProductTypeServiceBean.java
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;
//为了让bean纳入spring容器管理
@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {
//重载业务管理Bean的删除方法
@Override
public <T> void delete(Class<T> entityClass, Object[] entityids) {
StringBuffer jpql = new StringBuffer();
if(entityids!=null && entityids.length>0){
for(int i=0;i<entityids.length;i++){
jpql.append("?").append(i+2).append(",");
}
jpql.deleteCharAt(jpql.length()-1);
//createQuery()是JPQL查询的方法,不是本地查询(sql语句),JPQL语句是面向对象,jpa规范的查询语言
Query 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();
}
}
}
ProductTest.java
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;
import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
public class ProductTest {
private static ApplicationContext cxt;
private static ProductTypeService productTypeService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
/** 启动spring容器 **/
cxt = new ClassPathXmlApplicationContext("beans.xml");
/** 用接口去引用spring给我们返回的对象 **/
productTypeService = (ProductTypeService)cxt.getBean("productTypeServiceBean");
} catch (Exception e){
e.printStackTrace();
}
}
/*
* 添加单元测试点
* (@Test)
*/
@Test
public void testSave(){
ProductType type = new ProductType();
type.setName("篮球用品");
type.setNote("..产品");
productTypeService.save(type);
}
@Test
public void testFind(){
ProductType type = productTypeService.find(ProductType.class, 2);
org.junit.Assert.assertNotNull("获取不到id为2的记录", type);
}
@Test
public void testUpdate(){
/** 返回一个游离状态,因为业务逻辑方法执行完后,会关闭事务 **/
ProductType type = productTypeService.find(ProductType.class, 5);
type.setName("足球用品");
type.setNote("sd产品");
productTypeService.update(type);
}
@Test
public void testDelete(){
productTypeService.delete(ProductType.class, 2);
}
}
相关文章推荐
- 巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
- 巴巴运动网学习笔记三之产品分类实体对象基本属性的JPA映射,用泛型技术对产品分类的业务管理Bean抽象,单元测试产品分类的业务管理Bean.,重载业务管理Bean的删除方法,
- 巴巴运动网-用泛型技术对产品分类的业务管理Bean抽象
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载).NET技术
- 抽象工厂的代码实现示例
- 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有测试代码(何志雄)
- 配置gitlab环境实现代码管理及Web Hook测试和ldap认证 推荐
- Spring中bean的继承与抽象代码示例
- SAE 平台代码实现数据库定时备份以及同步到本地 2014/09/11 09:39:01 分类: 技术随笔 1人评论 次浏览 SAE 只允许用户通过phpMyAdmin管理远程数据库,
- 【荐】JavaScript+CSS技术实现能覆盖SELECT的图片放大(放大镜)示例代码
- 【代码篇】JBPM4.4开发流程节点(动态代理实现流程管理业务)
- 使用事务管理抽象API进行事务界定的代码示例
- linux系统管理客户端2--串口实现测试代码编写
- 权限管理系统(用户信息管理模块业务组件实现代码,带注解)
- 关闭 晓K的专栏 我的学习历程 目录视图摘要视图订阅 赠书 | 异步2周年,技术图书免费选 每周荐书:渗透测试、K8s、架构(评论送书) 项目管理+代码托管+文档协作,开发更
- 用泛型与反射技术封装分页功能(示例代码)
- 关闭 晓K的专栏 我的学习历程 目录视图摘要视图订阅 赠书 | 异步2周年,技术图书免费选 每周荐书:渗透测试、K8s、架构(评论送书) 项目管理+代码托管+文档协作,开发更
- 4.1信息管理的设计和实现(利用泛型减少重复代码)
- 用RAII技术管理资源及其泛型实现