Package com.quickorm.entity

Examples of com.quickorm.entity.EntityMetaData


        return this.getDataSource().getConnection();
    }

    //得到实体类的元数据
    public EntityMetaData getEntityMetaData(Class entityClass) {
        EntityMetaData rtnEMD = null;
        if (entityClassMetaDataMap.containsKey(entityClass)) {
            rtnEMD = entityClassMetaDataMap.get(entityClass);
        } else {
            rtnEMD = new EntityMetaData(entityClass);
            entityClassMetaDataMap.put(entityClass, rtnEMD);
        }
        return rtnEMD;
    }
View Full Code Here


    }

    //设置对象字段的值
    private void setObjectFieldValue(Object target, String columnName, Object value) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        //得到实体元数据
        EntityMetaData entityMetaData = this.getEntityMetaData(target.getClass());
        //要设置的字段
        Field toSetField = entityMetaData.getFieldByDataBaseColumnName(columnName);

        //如果没有此字段
        if (toSetField == null) {
            //如果包含下划线
            if (columnName.contains("_")) {
                String[] columnNameArray = columnName.split("_");
                String firstColumnName = columnNameArray[0];

                toSetField = entityMetaData.getFieldByDataBaseColumnName(firstColumnName);
                if (toSetField == null) {
                    return;
                }
                Class fieldClass = toSetField.getType();
                Object fieldObject = toSetField.get(target);
View Full Code Here

     * @return
     */
    @Override
    public <T> T get(Class<T> entityClass, Object id) {
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);

        String sql = "SELECT * FROM " + this.dialect.addIdentifierSeparatingChar(entityMetaData.getTableName());

        //准备条件Map
        Map<String, Object> conditionArgumentMap = new LinkedHashMap<String, Object>();
        if (id == null) {
            id = DBNull.value;
        }
        conditionArgumentMap.put(dialect.addIdentifierSeparatingChar(entityMetaData.getPrimaryKey()) + " = ?", id);

        //执行查询
        List<T> tList = this.executeQuery(sql, null, conditionArgumentMap, null, entityClass);
        if (tList == null || tList.isEmpty()) {
            return null;
View Full Code Here

    @Override
    public void save(Object t) {
        //实体类
        Class entityClass = t.getClass();
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);
        //得到SQL语句与参数
        SqlAndArgumentsData sqlAndArgumentsData = entityMetaData.getInsertSqlAndArgumentData(t, dialect);
        int rowCount = this.executeUpdate(sqlAndArgumentsData.getSql(), sqlAndArgumentsData.getArgumentList(), null);
        if (rowCount <= 0) {
            throw new DataAccessException("更改的数据行数为0!");
        }
    }
View Full Code Here

        }

        //实体类
        Class entityClass = tList.get(0).getClass();
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);
        //得到SQL语句与参数
        SqlAndArgumentsData sqlAndArgumentsData = entityMetaData.getBatchInsertSqlAndArgumentData(tList, dialect);
        int[] rowCountArray = this.executeBatchUpdate(sqlAndArgumentsData.getSql(), sqlAndArgumentsData.getArgumentListList());
        for (int i = 0; i <= rowCountArray.length - 1; i++) {
            int rowCount = rowCountArray[i];
            if (rowCount <= 0) {
                throw new DataAccessException(String.format("第%s行更改的数据行数为0!", i));
View Full Code Here

    @Override
    public void update(Object t) {
        //实体类
        Class entityClass = t.getClass();
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);
        //列名字段对应Map
        Map<String, Field> columnFieldMap = entityMetaData.getColumnNameFieldMap();


        //参数列表
        List<Object> argumentList = new LinkedList<Object>();
        //有值的字段名称列表(不包括主键)
        List<String> hasValueColumnNameList = new LinkedList<String>();

        for (String columnName : columnFieldMap.keySet()) {
            //如果是主键
            if (columnName.equals(entityMetaData.getPrimaryKey())) {
                continue;
            }
            Field field = columnFieldMap.get(columnName);
            try {
                Object value = field.get(t);
                if (value == null) {
                    continue;
                }
                hasValueColumnNameList.add(columnName);
                argumentList.add(value);
            } catch (Exception ex) {
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE ").append(dialect.addIdentifierSeparatingChar(entityMetaData.getTableName())).append(" SET ");

        for (int i = 0; i <= hasValueColumnNameList.size() - 1; i++) {
            String columnName = hasValueColumnNameList.get(i);
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(dialect.addIdentifierSeparatingChar(columnName)).append(" = ?");
        }
        sb.append(" WHERE ").append(dialect.addIdentifierSeparatingChar(entityMetaData.getPrimaryKey())).append(" = ?;");
        //添加主键的值
        argumentList.add(entityMetaData.getPrimaryKeyValue(t));

        String sql = sb.toString();
        int rowCount = this.executeUpdate(sql, argumentList, null);
        if (rowCount <= 0) {
            throw new DataAccessException("更改的数据行数为0!");
View Full Code Here

    public void delete(Class entityClass, Object id) {
        if (id == null) {
            HandleException(new SQLException("主键的值不能为空!"), null);
        }
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);

        String sql = String.format("DELETE FROM %s WHERE %s = ?", dialect.addIdentifierSeparatingChar(entityMetaData.getTableName()), dialect.addIdentifierSeparatingChar(entityMetaData.getPrimaryKey()));
        //准备参数
        List<Object> argumentList = new LinkedList<Object>();
        argumentList.add(id);

        int rowCount = this.executeUpdate(sql, argumentList, null);
View Full Code Here

    public void deleteList(Class entityClass, List<Object> idList) {
        if (idList == null || idList.isEmpty()) {
            return;
        }
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("DELETE FROM %s WHERE %s IN (", dialect.addIdentifierSeparatingChar(entityMetaData.getTableName()), dialect.addIdentifierSeparatingChar(entityMetaData.getPrimaryKey())));
        for (int i = 0; i <= idList.size() - 1; i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append("?");
View Full Code Here

     * @return
     */
    @Override
    public <T> List<T> getList(Class<T> entityClass, Map<String, Object> conditionArgumentMap, String sqlSuffix) {
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);

        String sql = String.format("SELECT * FROM %s", dialect.addIdentifierSeparatingChar(entityMetaData.getTableName()));
        //执行查询
        return this.executeQuery(sql, null, conditionArgumentMap, sqlSuffix, entityClass);
    }
View Full Code Here

     * @return
     */
    @Override
    public <T> PagedList<T> getList(Class<T> entityClass, Map<String, Object> conditionArgumentMap, String sqlSuffix, int pageIndex, int pageSize) {
        //实体元数据
        EntityMetaData entityMetaData = getEntityMetaData(entityClass);

        String sql = String.format("SELECT * FROM %s", dialect.addIdentifierSeparatingChar(entityMetaData.getTableName()));
        //执行查询
        return this.executePagedQuery(sql, null, conditionArgumentMap, sqlSuffix, entityClass, pageIndex, pageSize);
    }
View Full Code Here

TOP

Related Classes of com.quickorm.entity.EntityMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.