All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wuzh.commons.dbutils.repository.CurdRepository Maven / Gradle / Ivy

/*
 * Copyright 2015-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wuzh.commons.dbutils.repository;

import com.wuzh.commons.dbutils.Sql;
import com.wuzh.commons.dbutils.annotation.BeanAnnotationUtil;
import com.wuzh.commons.dbutils.entity.AbstractEntity;
import com.wuzh.commons.dbutils.vo.AbstractVo;
import com.wuzh.commons.pager.PaginationObject;
import com.wuzh.commons.pager.PaginationParameter;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类CURDRepository.java的实现描述:基于Java Bean对象的数据操作
 *
 * @author 伍章红 2016年12月28日 下午7:58:57
 * @version v1.0.0
 * @since JDK 1.7
 */
public class CurdRepository extends PaginationRepository {
    private final Log logger = LogFactory.getLog(getClass());

    /**
     * 获取实体类的表名
     *
     * 
     * 注:实体类<E>上必须存在@table元数据标注,否则返回为null.
     *
     * 例子:
     * @Table(name = "SECURITY_USER")
     * public class UserPo implements Serializable {
     *     private static final long serialVersionUID = 1L;
     *
     *     @Column(name = "ID")
     *     private String id;
     *
     *     // 省略getter、setter方法...
     * }
     * 
* * @return 实体类E的表名 */ public String getTableName() { Class entityClass = getEntityClass(); if (null == entityClass) { return null; } return BeanAnnotationUtil.getTableName(entityClass); } /** * 取得查询SQL语句中column字段在实体类<E>中@Column对应的field字段。当column中已经存在as关键字时,则过滤此column; * 如果实体类<E>中存在@Column对应的field字段,则用此field做别名;否则采用此column的小写作为别名。格式为:COLUMN AS field * *
     * 例1(column中包含as关键字):传入column为ID as id,CODE,NAME,返回则是ID as id,CODE AS code,NAME AS name
     * 例2(实体类<E>中存在@Column对应的field字段):传入column为ID,CODE,NAME,返回则是ID AS userId,CODE AS code,NAME AS name
     * 例3:传入column为ID,CODE,NAME,返回则是ID AS id,CODE AS code,NAME AS name
     *
     * 例子:
     * @Table(name = "SECURITY_USER")
     * public class UserPo implements Serializable {
     *     private static final long serialVersionUID = 1L;
     *
     *     @Column(name = "ID")
     *     private String userId;
     *
     *     // 省略getter、setter方法...
     * }
     * 
* * @param columns 查询SQL的column字段 * @return 实体类中field字段对应的SQL字段 */ public String mappingQueryColumns(String columns) { if (StringUtils.isEmpty(columns)) { return ""; } StringBuffer columnBuffer = new StringBuffer(); String[] columnArr = columns.split(","); // 取得实体类中表字段映射数据:key=column,value=field Map columnsMap = BeanAnnotationUtil.getColumnsMap(getEntityClass()); for (int i = 0; i < columnArr.length; i++) { if (i > 0) { columnBuffer.append(","); } String column = columnArr[i]; if (StringUtils.containsIgnoreCase(column, Sql.QUERY_AS)) { columnBuffer.append(column); continue; } String field = null; if (columnsMap.containsKey(column.toUpperCase())) { field = columnsMap.get(column.toUpperCase()); } if (StringUtils.isEmpty(field)) { field = column.toLowerCase(); } columnBuffer.append(column).append(Sql.QUERY_AS).append(field); } return columnBuffer.toString(); } /** * 获取实体类<E>中查询column * * @return 实体类E中@Column映射的表字段 */ public String getQueryColumns() { return getQueryColumns(getEntityClass()); } /** * 获取指定实体类clazz中查询column * *
     * 实体类:
     * public class JobDetailPo implements Serializable {
     *     private static final long serialVersionUID = 1L;
     *
     *     @Column(name = "JOB_KEY_ID")
     *     private String jobKeyId;
     *
     *     @Column(name = "JOB_CLASS")
     *     private String jobClass;
     *
     *     @Column(name = "IS_DELETE")
     *     private String isDelete;
     *
     *     @Column(name = "DESCRIPTION")
     *     private String description;
     *
     *     // 省略getter、setter方法...
     * }
     *
     * 对应的查询SQL column:
     * KEY_NAME AS keyName,CREATE_TIME AS createTime,DESCRIPTION AS description,CREATE_USER AS createUser,KEY_GROUP AS keyGroup
     * 
* * @param clazz 指定实体类 * @return Class类中@Column映射的表字段 */ public String getQueryColumns(Class clazz) { Map columnsMap = BeanAnnotationUtil.getColumnsMap(clazz); if (columnsMap == null || columnsMap.size() == 0) { return ""; } StringBuffer columnBuffer = new StringBuffer(); for (String column : columnsMap.keySet()) { if (columnBuffer.length() > 0) { columnBuffer.append(","); } columnBuffer.append(column).append(Sql.QUERY_AS).append(columnsMap.get(column)); } return columnBuffer.toString(); } /** * 获取实体类中的字段数据。key为field,value为value * * @param entity 实体类 * @param hasSerial 是否含有序列化serialVersionUID字段 * @return 返回包含filed字段及其value值的Map集合 */ public Map getFields(T entity, boolean hasSerial) { if (null == entity) { return null; } Map fieldMap = new HashMap(); List allFieldsList = FieldUtils.getAllFieldsList(entity.getClass()); for (Field field : allFieldsList) { if (!hasSerial) { if (StringUtils.equalsIgnoreCase("serialVersionUID", field.getName())) { continue; } } field.setAccessible(true); try { fieldMap.put(field.getName(), field.get(entity)); } catch (IllegalArgumentException e) { e.printStackTrace(); logger.error(e); } catch (IllegalAccessException e) { e.printStackTrace(); logger.error(e); } } return fieldMap; } /** * 新增数据 * * @param entity 实体类对象 * @return 返回null表示新增失败,否则新增成功 * @throws SQLException */ public E insert(E entity) throws SQLException { Map columnsMap = BeanAnnotationUtil.getColumnsMap(getEntityClass()); String[] columns = new String[columnsMap.size()]; Object[] values = new Object[columnsMap.size()]; int index = 0; for (String column : columnsMap.keySet()) { columns[index] = column; Object value = null; try { value = PropertyUtils.getProperty(entity, columnsMap.get(column)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } values[index] = value; index++; } if (this.insert(getTableName(), columns, values) > 0) { return entity; } return null; } /** * 批量新增数据 * * @param list 实体类对象集合 * @return 返回null表示新增失败,否则新增成功 * @throws SQLException */ public int[] batchInsert(List list) throws SQLException { Map columnsMap = BeanAnnotationUtil.getColumnsMap(getEntityClass()); String[] columns = new String[columnsMap.size()]; Object[][] valueslist = new Object[list.size()][]; for (int i = 0; i < list.size(); i++) { Object[] values = new Object[columnsMap.size()]; int index = 0; for (String column : columnsMap.keySet()) { columns[index] = column; Object value = null; try { value = PropertyUtils.getProperty(list.get(i), columnsMap.get(column)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } values[index] = value; index++; } valueslist[i] = values; } return this.batchInsert(getTableName(), columns, valueslist); } /** * 修改数据 * * @param entity 实体类对象 * @return 返回null表示更新失败,否则更新成功 * @throws SQLException */ public E update(E entity) throws SQLException { Map columnsMap = BeanAnnotationUtil.getColumnsMap(entity.getClass()); String[] columns = new String[columnsMap.size()]; Object[] values = new Object[columnsMap.size()]; String[] conditionColumns = { "ID" }; Object[] conditionObjs = { entity.getId() }; int index = 0; for (String column : columnsMap.keySet()) { columns[index] = column; Object value = null; try { value = PropertyUtils.getProperty(entity, columnsMap.get(column)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } values[index] = value; index++; } if (this.update(getTableName(), columns, values, conditionColumns, conditionObjs) > 0) { return entity; } return null; } /** * 根据主键id删除数据 * * @param id 主键id * @return 返回1表示删除成功,否则删除失败 * @throws SQLException */ public int delete(String id) throws SQLException { return this.delete(getTableName(), " AND ID=?", new Object[]{ id }); } /** * 批量删除数据 * * @param ids 主键id集合 * @return 返回1表示删除成功,否则删除失败 * @throws SQLException */ public int[] batchDelete(String[] ids) throws SQLException { String sql = MessageFormat.format(Sql.DELETE, getTableName(), " AND ID=?"); Object[][] params = new Object[ids.length][]; for (int i = 0; i < ids.length; i++) { params[i] = new Object[]{ ids[i] }; } return this.batchExecute(sql, params); } /** * 根据主键id查询实体类对应的表数据信息 * * @param id 主键id * @return 返回主键id对应的实体类 * @throws SQLException */ public E get(String id) throws SQLException { return this.get(getTableName(), getQueryColumns(), id); } /** * 根据条件查询实体表数据列表 * * @param vo 查询条件 * @return 返回查询结果数据列表 * @throws SQLException */ public List queryEntityList(V vo) throws SQLException { // 取得vo类的查询字段数据 Map fields = getFields(vo, false); // 取得po类的字段映射数据 Map fieldsMap = BeanAnnotationUtil.getFieldsMap(getEntityClass()); List conditionColumns = new ArrayList(); List conditionObjs = new ArrayList(); for (String field : fields.keySet()) { Object value = fields.get(field); if (null == value) { continue; } if (value instanceof String) { if (StringUtils.isBlank(value.toString())) { continue; } } // 条件字段 conditionColumns.add(fieldsMap.get(field)); // 条件 conditionObjs.add(value); } return this.queryForList(getTableName(), getQueryColumns(), conditionColumns.toArray(new String[0]), conditionObjs.toArray()); } /** * 根据条件查询实体表数据列表总数 * * @param vo 查询条件 * @return 返回查询结果数据列表总数 * @throws SQLException */ public long queryEntityTotal(V vo) throws SQLException { // 取得vo类的查询字段数据 Map fields = getFields(vo, false); // 取得po类的字段映射数据 Map fieldsMap = BeanAnnotationUtil.getFieldsMap(getEntityClass()); List conditionColumns = new ArrayList(); List conditionObjs = new ArrayList(); for (String field : fields.keySet()) { Object value = fields.get(field); if (null == value) { continue; } if (value instanceof String) { if (StringUtils.isBlank(value.toString())) { continue; } } // 条件字段 conditionColumns.add(fieldsMap.get(field)); // 条件 conditionObjs.add(value); } return this.queryForLong(getTableName(), "COUNT(1)", conditionColumns.toArray(new String[0]), conditionObjs.toArray()); } /** * 根据条件查询分页数据 * * @param paginationParamter 分页参数 * @return 返回分页数据信息 * @throws SQLException */ public PaginationObject queryForPager(PaginationParameter paginationParamter) throws SQLException { // 取得vo类的查询字段数据 Map fields = getFields(paginationParamter.getVo(), false); // 取得po类的字段映射数据 Map fieldsMap = BeanAnnotationUtil.getFieldsMap(getEntityClass()); List conditionColumns = new ArrayList(); List conditionObjs = new ArrayList(); for (String field : fields.keySet()) { Object value = fields.get(field); if (null == value) { continue; } if (value instanceof String) { if (StringUtils.isBlank(value.toString())) { continue; } } // 条件字段 conditionColumns.add(fieldsMap.get(field)); // 条件 conditionObjs.add(value); } // 查询数据总数 Long rowCount = this.queryForLong(getTableName(), "COUNT(1)", conditionColumns.toArray(new String[0]), conditionObjs.toArray()); List resultList = new ArrayList(); if (rowCount > 0) { // 查询数据 resultList = this.queryForList(getTableName(), getQueryColumns(), conditionColumns.toArray(new String[0]), conditionObjs.toArray(), paginationParamter.getPageNo(), paginationParamter.getPageSize()); } PaginationObject paginationObject = new PaginationObject(rowCount, resultList); paginationObject.setPageNo(paginationParamter.getPageNo()); paginationObject.setPageSize(paginationParamter.getPageSize()); paginationObject.setVo(paginationParamter.getVo()); return paginationObject; } }