2015-01-07 2 views
0

Я посетил слишком много сайтов, а также вопрос о stackoverflow for reference, но я не могу найти точно ответ ниже.Дизайн шаблона для Generic Dao, сервисный слой весной без использования Hibernate

Я хочу создать архитектуру для Generic DAO Pattern без использования hibernate.

В настоящее время у меня возникли проблемы с разработкой файла GenericSpringDAOImpl<DomainObject> java. Как сделать общий метод getList() (который возвращает DomainObject объектов) без hibernate или я говорю, используя jdbctemplate из spring?

Мой код ниже:

GenericDAO.java

public interface GenericDAO<DomainObject extends Serializable, KeyType extends Serializable> extends Serializable{ 

/** 
* Give list of all entities of underlying Domain Object. 
* @return List of all entities. 
*/ 
public List<DomainObject> getList(); 
/** 
* Returns the total number of Entities that is affected by sql query,. 
* 
* @param query 
*   the query 
* @return Number of Entities affected By SQL Query. 
*/ 
public int getRecordBySQLQuery(String query); 

/** 
* Returns Domain object whose Key-value pair matched. 
* @param keyName Column name 
* @param keyValue any value that is being matched under Column name. 
* @return DomainObject 
*/ 
public DomainObject getRecordByKeyandValue(String keyName,Object keyValue); 

public DomainObject getRecordByKeysAndValues(String[] keyName,Object[] keyValue); 

/** 
* Returns list of Domainobjects whose Key-value pair matched. 
* @param colname Column name 
* @param keyValue List of values. 
* @return List<DomainObject> according to the Condition satisfied. 
*/ 
public List<DomainObject> getListByKeyandValue(String colname,List<? extends Object> keyValue); 

/** 
* Returns the list of Entities according to the condition applied. 
* @param condition Condition 
* @return List<DomainObject> according to condition satisfied. 
*/ 
public List<DomainObject> getListByCondition(String condition); 

/** 
* Find Object based on primary key. 
* @param id Identifier value is passed. 
* @return DomainObject i.e POJO which has the passed id value. 
*/ 
public DomainObject getRecordByPrimaryKey(KeyType id); 

/** 
* get the list of primary keys based on the condition. 
* @param condition Condition 
* @return List<KeyType> of Primary Keys according to the Condition. 
*/ 
public List<KeyType> getPrimaryKeyCollection(String condition); 

/** 
* get the list of primary keys based on the condition. 
* @param condition Condition 
* @return List<KeyType> of Primary Keys according to the Condition. 
*/ 
public List<KeyType> getPrimaryKeyCollection(String condition,final Object... values); 

/** 
* Insert the Domain Object that is going to persist into the Database. 
* @param object Domain Object which is going to persist into the Database 
* @return KeyType Serializable value generated By Hibernate due to session.save(). 
*/ 
public KeyType insert(DomainObject object); 

/** 
* Update the Domain Object. 
* @param object Domain Object that is being changed/updated. 
* @return 1 if Domain Object is being successfully updated 
*   0 if Exception Generation while updating the Object. 
*/ 
public int update(DomainObject object); 


/** 
* Deleting Domain Object. 
* 
* @param object 
*   Domain Object that is going to be delete. 
* @return the int 
*/ 
public int delete(DomainObject object); 

/** 
* Delete Object whose oid(Object Identifier) matched with given Primary Key. 
* @param id Identifier value. 
*/ 
public void deleteById(KeyType id); 


} 

GenericSpringDAOImpl.java

public abstract class GenericDAOSpringImpl<DomainObject extends Serializable, KeyType extends Serializable> implements Serializable, GenericDAO<DomainObject, KeyType>{ 

@Autowired 
private NamedParameterJdbcTemplate jdbcTemplate; 

RowMapper<DomainObject> rowMapper; 

public NamedParameterJdbcTemplate getJdbcTemplate() { 
    return jdbcTemplate; 
} 

public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) { 
    this.jdbcTemplate = jdbcTemplate; 
} 

public List<DomainObject> getList(){  
    /*Develop Code which return DomainObjects 
    * One way is pass query to getList() but not good approch 
    */ 
    return new ArrayList<DomainObject>(); 
} 

/* 
    Other methods also required to implements. 
*/ 
} 

Здесь Домен Объект является любой объект, как студент, школа, учитель и т.д. .

+0

Что должно быть возвращено getList()? Вы должны будете дать ему какие-то параметры для извлечения, например, создать такие методы, как 'getDomainObjectBySomeValue (int someValue)' и т. Д. – Kayaman

+0

@Kayaman 'getList()' метод возвращает все объекты 'DomainObject' Предположим, я хочу выберите всех студентов, а затем «DomainObject», как «Студент», а метод getList() «возвращает всех учеников». –

+0

Если он возвращает все объекты, вы выполняете SQL-запрос и возвращаете объекты. Почему вы думаете, что вам нужно передать запрос в качестве параметра? – Kayaman

ответ

Смежные вопросы