if there is an interface for querying user details
the user base table is associated with a lot of other information about the user, it is better to query all the queries now, whether to use multiple dao to combine in the service, or to directly link the query
user Table (user_base)
user Information Table 1 (user_info_1)
user Information Table 2 (user_info_2)
user Information Table 3 (user_info_3)
.
user Information Table n (user_info_n)
multiple dao queries returned
@Service
public class UserDetailServiceImpl implements UserService{
@Autowired
private UserBaseDao userBaseDao;
@Autowired
private UserInfoDao1 userInfoDao1;
@Autowired
private UserInfoDao2 userInfoDao2;
@Override
public UserInfoVo getDetail(String userId){
UserInfoVo userInfoVo = new UserInfoVo ();
// dao
UserBase userBase = userBaseDao.getByUserId(userId);
UserInfo1 userInfo1 = userInfoDao1.getByUserId(userId);
UserInfo2 userInfo2 = userInfoDao2.getByUserId(userId);
// VOset
userInfoVo.set(dao1);
...
...
userInfoVo.set(daoN);
return userInfoVo ;
}
}
Link query
@Service
public class UserDetailServiceImpl implements UserService{
@Autowired
private UserBaseDao userBaseDao;
@Override
public UserInfoVo getDetail(String userId){
// daoVO
UserInfoVo userInfoVo = userBaseDao.getDetail(userId);
// sql
String sql =
"select "" from user_base
left join user_info_1 on user_base.user_id = user_info_1.user_id
left join user_info_2 on user_base.user_id = user_info_2.user_id
...
left join user_info_n on user_base.user_id = user_info_n.user_id
where user_base.user_id = :userId";
// set
return userInfoVo ;
}
}
< H2 > related questions < / H2 >
use the first combination of multiple Dao to return data. Although the various set in the Service layer is more complex, it feels less coupled. Each part of the dao is very independent, and it can also be reused in other places
.using the second method, the processing of the Service layer is much easier except for very long sql statements that you have to write by hand. But the reusability is not enough
I hope to give relevant answers to the choice of these two ways based on the relevant programming ideas, or there are other design and implementation methods. Thank you very much