日期:2014-05-16 浏览次数:20553 次
/* written by Wooce Yang,2009/10/09 * 执行依据数据库主键的update操作 * */ public void updateByID(String id, String setFieldStr) { createQuery("update " + entityClass.getSimpleName() + " set " + setFieldStr + " where " + BeanUtil.getFieldByAnnotation(entityClass, Id.class).getName() + "=" + id + ")").executeUpdate(); } /* written by Wooce Yang,2009/10/09 * 执行依据数据库主键的批处理update操作 * Entity Class须在主键字段处加上@Id的annotation * */ public void updateByIDs(String[] ids, String setFieldStr) { String idsStr = ""; for (int i = 0; i < ids.length; i++) { if (i > 0) idsStr += ","; idsStr += ids[i]; } createQuery("update " + entityClass.getSimpleName() + " set " + setFieldStr + " where " + BeanUtil.getFieldByAnnotation(entityClass, Id.class).getName() + " in (" + idsStr + ")").executeUpdate(); }
import java.lang.reflect.Field; /** * Author: Wooce Yang * Date: 2009-10-9 * Time: 15:43:33 */ public class BeanUtil { public static Field getFieldByAnnotation(Class beanClass, Class annotationClass) { Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(annotationClass)) { return field; } } return null; } }
public class Project { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long projectId;
public void deleteProject(String id) { projectDao.updateByID(id, "deleted=1"); } public void deleteProjects(String[] ids) { projectDao.updateByIDs(ids, "deleted=1"); }