简体   繁体   中英

multiple methods in dao layer

Currently I'm planning to refactor an old codebase which is written in spring+hibernate.

I have seen so many methods in dao layer which is solving same purpose like.

for example I have user_info table

in dao-layer there are so many methods like

 getUserInfoById(String userId);
 getUserInfoByName(String name);
 getUserInfoByIdAndName(String userId,String name) 

and the list goes on.

i know it is very bad practice to follow.

I thought a solution like i will have only one method getUserInfo(User user) inside this method i will encapsulate query construction like

query.with(user.username).with(user.userId) ...

i don't know is it correct solution to follow...

any suggestions? Any links to pages that explain this concept from the very basic stuff is also appreciated.

after an intense browsing i found spring-jpa integration here

only thing i have created is interface per entity. spring itself handles creation of DAO layer and handles all the operation's on that entity and also support bulk operation's.we can add our custom method's inside that interface spring jpa will implement those methods.no need to write any code which are related to DAO layer.

sample example:

@Repository

public interface UserRepository extends PagingAndSortingRepository<UserCore, Serializable>, JpaSpecificationExecutor<UserCore>  {

}

we can also return java stream from DAO methods as explained here

it also have specification support which is similar to criteria in hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM