java 泛型的 设置值、取值

public abstract class DaoSupportImpl<T> implements DaoSupport<T> {

	@Resource
	private SessionFactory sessionFactory;
	private Class<T> clazz;

	public DaoSupportImpl() {
		// 使用反射技术得到T的真实类型
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
		this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
		System.out.println("clazz ---> " + clazz);
	}

	/**
	 * 获取当前可用的Session
	 * 
	 * @return
	 */
	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}


	@Override
	public void moveUp(Long id,Long parentId) {
		try {
				T curT = getById(id); // 当前要移动的对象
				Class curClass = curT.getClass(); 
				Field curField= curClass.getDeclaredField("orderFlag");//得到移动标示这个字段
				curField.setAccessible(true); //设置些属性是可以访问的
				Integer curOrderFlag = Integer.parseInt(curField.get(curT).toString());//得到当前移动标示的值   
				T otherT=null;
				if(parentId!=null){
				otherT = (T) getSession().createQuery(// 我上面的那个对象
						"FROM "+clazz.getSimpleName()+" obj WHERE obj.orderFlag<? and obj.parent.id=? ORDER BY obj.orderFlag DESC")//
						.setParameter(0, curOrderFlag)//
						.setParameter(1, parentId)//
						.setFirstResult(0)//
						.setMaxResults(1)//
						.uniqueResult();
				}else{
					otherT = (T) getSession().createQuery(// 我上面的那个对象
							"FROM "+clazz.getSimpleName()+" obj WHERE obj.orderFlag<? and obj.parent.id=? ORDER BY obj.orderFlag DESC")//
							.setParameter(0, curOrderFlag)//
							.setParameter(1, null)//
							.setFirstResult(0)//
							.setMaxResults(1)//
							.uniqueResult();
				}
				// 最上面的不能上移
				if (otherT == null) {
					return;
				}
				Class otherClass=otherT.getClass();
				Field otherField=otherClass.getDeclaredField("orderFlag");
				otherField.setAccessible(true); //设置些属性是可以访问的
				Integer otherOrderFlag = Integer.parseInt(otherField.get(otherT).toString());//得到要移动标示的值   
				
				// 交换OrderFlag的值
				Integer temp = curOrderFlag;
				curField.set(curT, otherOrderFlag);
				otherField.set(otherT, temp);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
	}
	@Override
	public void moveDown(Long id,Long parentId) {
		try {
			T curT = getById(id); // 当前要移动的对象
			Class curClass = curT.getClass(); 
			Field curField= curClass.getDeclaredField("orderFlag");//得到移动标示这个字段
			curField.setAccessible(true); //设置些属性是可以访问的
			Integer curOrderFlag = Integer.parseInt(curField.get(curT).toString());//得到当前移动标示的值   
			T otherT=null;
			if(parentId!=null){
			otherT = (T) getSession().createQuery(// 我上面的那个对象
					"FROM "+clazz.getSimpleName()+" obj WHERE obj.orderFlag>? and obj.parent.id=? ORDER BY obj.orderFlag ASC")//
					.setParameter(0, curOrderFlag)//
					.setParameter(1, parentId)//
					.setFirstResult(0)//
					.setMaxResults(1)//
					.uniqueResult();
			}else{
				otherT = (T) getSession().createQuery(// 我上面的那个对象
						"FROM "+clazz.getSimpleName()+" obj WHERE obj.orderFlag>? and obj.parent.id=? ORDER BY obj.orderFlag ASC")//
						.setParameter(0, curOrderFlag)//
						.setParameter(1, null)//
						.setFirstResult(0)//
						.setMaxResults(1)//
						.uniqueResult();
			}
			// 最上面的不能上移
			if (otherT == null) {
				return;
			}
			Class otherClass=otherT.getClass();
			Field otherField=otherClass.getDeclaredField("orderFlag");
			otherField.setAccessible(true); //设置些属性是可以访问的
			Integer otherOrderFlag = Integer.parseInt(otherField.get(otherT).toString());//得到要移动标示的值   
			
			// 交换OrderFlag的值
			Integer temp = curOrderFlag;
			curField.set(curT, otherOrderFlag);
			otherField.set(otherT, temp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 

	}
	
	

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值