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

cn.featherfly.component.sorm.SimpleORM Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version

package cn.featherfly.component.sorm;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.jdbc.core.JdbcTemplate;

import cn.featherfly.common.db.builder.ConditionBuilder;
import cn.featherfly.common.db.data.Execution;
import cn.featherfly.common.db.data.SimpleExecution;
import cn.featherfly.common.db.metadata.DatabaseMetadata;
import cn.featherfly.common.db.metadata.DatabaseMetadataManager;
import cn.featherfly.common.lang.AssertIllegalArgument;
import cn.featherfly.common.lang.LangUtils;
import cn.featherfly.component.sorm.mapping.ClassMapping;
import cn.featherfly.component.sorm.operate.DeleteOperate;
import cn.featherfly.component.sorm.operate.GetOperate;
import cn.featherfly.component.sorm.operate.InsertOperate;
import cn.featherfly.component.sorm.operate.MergeOperate;
import cn.featherfly.component.sorm.operate.QueryOperate;
import cn.featherfly.component.sorm.operate.UpdateOperate;


/**
 * 

* 简单对象记录映射. *

* @param 对象类型 * @author 钟冀 * @since 1.0 * @version 1.0 */ public class SimpleORM { /** * @param type 类型 * @param jdbcTemplate jdbcTemplate */ public SimpleORM(Class type, JdbcTemplate jdbcTemplate) { init(type, jdbcTemplate, null); } /** * @param type 类型 * @param jdbcTemplate jdbcTemplate * @param dataBase 具体库 */ public SimpleORM(Class type, JdbcTemplate jdbcTemplate, String dataBase) { init(type, jdbcTemplate, dataBase); } private void init(Class type, JdbcTemplate jdbcTemplate, String dataBase) { AssertIllegalArgument.isNotNull(jdbcTemplate, "jdbcTemplate不能为空"); AssertIllegalArgument.isNotNull(jdbcTemplate.getDataSource(), "数据源(jdbcTemplate.dataSource)不能为空"); metadata = DatabaseMetadataManager.getDefaultManager().create(jdbcTemplate.getDataSource()); insertOperate = new InsertOperate(type, jdbcTemplate, dataBase); updateOperate = new UpdateOperate(type, jdbcTemplate, dataBase); deleteOperate = new DeleteOperate(type, jdbcTemplate, dataBase); mergeOperate = new MergeOperate(type, jdbcTemplate, dataBase); getOperate = new GetOperate(type, jdbcTemplate, dataBase); queryOperate = new QueryOperate(type, jdbcTemplate, dataBase); setGeneratedKey(true); } /** *

* 保存. *

* @param entity 对象 * @return 影响的数据库行数 */ public int save(T entity) { if (validator != null) { Set> cons = validator.validate(entity); if (LangUtils.isNotEmpty(cons)) { StringBuilder errorMessage = new StringBuilder(); for (ConstraintViolation constraintViolation : cons) { errorMessage.append(constraintViolation.getMessage()).append(","); } throw new SimpleORMException(errorMessage.toString()); } } return insertOperate.execute(entity); } /** *

* 更新. *

* @param entity 对象 * @return 影响的数据库行数 */ public int update(T entity) { if (validator != null) { Set> cons = validator.validate(entity); if (LangUtils.isNotEmpty(cons)) { StringBuilder errorMessage = new StringBuilder(); for (ConstraintViolation constraintViolation : cons) { errorMessage.append(constraintViolation.getMessage()).append(","); } throw new SimpleORMException(errorMessage.toString()); } } return updateOperate.execute(entity); } /** *

* 合并操作,将传入对象的非空字段更新进数据库(忽略null). *

* @param entity 对象 */ public void merge(T entity) { // YUFEI_TODO 合并可以使用外置合并策略来修改合并的行为,比如数据库列的白黑名单,对象属性白黑名单等 mergeOperate.execute(entity); } /** *

* 删除. *

* @param entity 对象 * @return 影响的数据库行数 */ public int delete(T entity) { return deleteOperate.execute(entity); } /** *

* 获取指定的对象. *

* @param id 唯一标识 * @return 指定主键值的对象 */ public T get(Serializable id) { return getOperate.get(id); } /** *

* 加载对象. *

* @param entity 对象 * @return 传入对象主键值对应的数据库对象 */ public T load(T entity) { return getOperate.get(getOperate.getIdentity(entity)); } /** *

* 根据指定条件返回唯一结果. *

* @param conditionBuilder 查询条件构建器 * @return 唯一结果 */ public T unique(ConditionBuilder conditionBuilder) { return queryOperate.unique(conditionBuilder); } /** *

* 根据查询条件返回唯一对象. *

* @param condition 查询条件SQL(不包含where) * @param params 查询参数 * @return 指定ID的对象 */ public T unique(String condition, Object...params) { return queryOperate.unique(condition, params); } /** *

* 根据指定条件返回查询结果集合. *

* @param conditionBuilder 查询条件构建器 * @return 结果集合 */ public List list(ConditionBuilder conditionBuilder) { return queryOperate.list(conditionBuilder); } /** *

* 根据指定条件返回查询结果集合. *

* @param condition 查询条件SQL(不包含where) * @param params 查询参数 * @return 结果集合 */ public List list(String condition, Object...params) { return queryOperate.list(condition, params); } /** *

* 返回指定类型的映射信息 *

* @param type 类型 * @return 映射信息 */ public ClassMapping getClassMapping(Class type) { if (type == null) { return null; } return ClassMapping.getMappedClass(type, metadata); } /** *

* 返回唯一标示值 *

* @param entity 实体对象 * @return 唯一标示值 */ public Serializable getIdentity(T entity) { if (entity == null) { return null; } return getOperate.getIdentity(entity); } /** * @return 是否自动设置生成的主键值 * @see cn.featherfly.component.sorm.operate.AbstractExecuteOperate#isGeneratedKey() */ public boolean isGeneratedKey() { return insertOperate.isGeneratedKey(); } /** * @param generatedKey 是否自动设置生成的主键值 * @see cn.featherfly.component.sorm.operate.AbstractExecuteOperate#setGeneratedKey(boolean) */ public void setGeneratedKey(boolean generatedKey) { insertOperate.setGeneratedKey(generatedKey); } /** *

* 根据指定条件返回Execution. *

* @param conditionBuilder 查询条件构建器 * @return Execution */ public Execution getQueryExecution(ConditionBuilder conditionBuilder) { Execution execution = new SimpleExecution(queryOperate.getSql(conditionBuilder), conditionBuilder.getParams().toArray()); return execution; } /** *

* 返回获取指定对象的Execution. *

* @param id 唯一标识 * @return Execution */ public Execution getGetExecution(Serializable id) { Execution execution = new SimpleExecution(getOperate.getSql(), new Object[] {id}); return execution; } /** *

* 返回保存对象的Execution. *

* @param entity 对象 * @return Execution */ public Execution getSaveExecution(T entity) { insertOperate.execute(entity); Execution execution = new SimpleExecution(insertOperate.getSql(), insertOperate.getParameters(entity)); return execution; } /** *

* 返回更新对象的Execution. *

* @param entity 对象 * @return Execution */ public Execution getUpdateExecution(T entity) { Execution execution = new SimpleExecution(updateOperate.getSql(), updateOperate.getParameters(entity)); return execution; } // public Execution getMergeExecution(T entity) { // TODO 之后来实现 // Execution execution = new SimpleExecution(mergeOperate.getSql(), // new Object[] {id}); // return execution; // } /** *

* 返回删除对象的Execution. *

* @param entity 对象 * @return Execution */ public Execution getDeleteExecution(T entity) { Execution execution = new SimpleExecution(deleteOperate.getSql(), deleteOperate.getParameters(entity)); return execution; } // ******************************************************************** // property // ******************************************************************** private InsertOperate insertOperate; private UpdateOperate updateOperate; private DeleteOperate deleteOperate; private MergeOperate mergeOperate; private GetOperate getOperate; private QueryOperate queryOperate; private DatabaseMetadata metadata; private Validator validator; /** * 返回validator * @return validator */ public Validator getValidator() { return validator; } /** * 设置validator * @param validator validator */ public void setValidator(Validator validator) { this.validator = validator; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy