The use of spring-data-jpa requires special compensation, which is better handled by mapper
//Make up for the insufficiency of jpa single entity. It is better to use different factory classes to operate complex sql and mapper. It is better to deal with single entity query directly without daoimpl, that is, use
XML does not require daoimpl for complex queries
jpa complex operations make up:
public class TbPowerGroupBeanDaoImpl implements ITbPowerGroupBeanDao {
@Autowired
public ESteelSqlSessionTemplate sqlSessionTemplate;
@Override
public List<Map<String,Object>> getBigPowerMenu(Map<String, Object> param) {
// TODO Auto-generated method stub
return sqlSessionTemplate.selectList("account.getBigPowerMenu", param);
}
}
public class UserBListDaoImpl implements UserBListDaoHelper{
@PersistenceContext
private EntityManager em;
@Override
public List<CusFirmBean> findCustomerExceptOne(Map<String, Object> paraMap) {
StringBuffer sb = new StringBuffer();
sb.append(" select distinct t.CUSTOMER_ID,t.CUSTOMER_KEY,t.CUSTOMER_NAME ");
sb.append(" from tb_user_blist t1 left join tb_cus_firm t on(t1.blist_customer_key=t.customer_key) ");
sb.append(" left join TB_USER_BLIST_LABEL t2 on(t1.blist_id=t2.blist_id) left join TB_USER_LABEL t3 on(t2.label_id=t3.label_id) ");
sb.append(" where t1.CUSTOMER_KEY=:CUSTOMER_KEY ");
if (!StringUtils.isNullObj(paraMap.get("CUSTOMER_NAME"))) {
sb.append(" and t.CUSTOMER_NAME like '%'||:CUSTOMER_NAME||'%'");
}
if (!StringUtils.isNullObj(paraMap.get("CUSTOMER_SEARCHKEY"))) {
sb.append(" and t.CUSTOMER_ID = :CUSTOMER_SEARCHKEY ");
}
if (!StringUtils.isNullObj(paraMap.get("label_name"))) {
sb.append(" and t3.label_name =:LABEL_NAME ");
}
if (!StringUtils.isNullObj(paraMap.get("selectedCustomerKey"))
&¶Map.get("selectedCustomerKey").toString()!="[]") {
sb.append(" and t.CUSTOMER_KEY not in (:selectedCustomerKey)");
}
sb.append(" and t.CUS_TRADE_KIND = 'A'");
Query query = em.createNativeQuery(sb.toString());
query.setParameter("CUSTOMER_KEY", paraMap.get("CUSTOMER_KEY"));
if (!StringUtils.isNullObj(paraMap.get("CUSTOMER_NAME"))) {
query.setParameter("CUSTOMER_NAME", paraMap.get("CUSTOMER_NAME"));
}
if (!StringUtils.isNullObj(paraMap.get("CUSTOMER_SEARCHKEY"))) {
query.setParameter("CUSTOMER_SEARCHKEY", ((String) paraMap.get("CUSTOMER_SEARCHKEY")).toUpperCase());
}
if (!StringUtils.isNullObj(paraMap.get("label_name"))) {
query.setParameter("LABEL_NAME", paraMap.get("label_name"));
}
if (!StringUtils.isNullObj(paraMap.get("selectedCustomerKey"))
&¶Map.get("selectedCustomerKey").toString()!="[]") {
query.setParameter("selectedCustomerKey", paraMap.get("selectedCustomerKey"));
}
List<Object[]> list = query.getResultList();
List<CusFirmBean> rstList = new ArrayList<CusFirmBean> ();
if(list!=null && list.size()>0) {
for(Object[] obj : list) {
CusFirmBean cus = new CusFirmBean ();
cus.setCustomerId(obj[0]!=null?obj[0].toString():"");
cus.setCustomerKey(NumberUtils.toLong(obj[1].toString()));
cus.setCustomerName(obj[2].toString());
rstList.add(cus);
}
}
return rstList;
}
}
The jpa dao layer operates directly (limited to a single entity):
@Component("basLkpService")
public class BasLkpServiceImpl implements BasLkpService {
@Autowired
public BasAreaDao basAreaDao;
@Autowired
public BasLkpDao basLkpDao;
@Override
public List<BasAreaBean> getAllProvince() {
return basAreaDao.findByAreaLevel("1");
}
@Override
public BasLkpBean getTbBasLkpByBusinessTypeLookupType(String businessType, String priceType) {
BasLkpBeanPK pk=new BasLkpBeanPK();
pk.setBusinessType(businessType);
pk.setLookupType(priceType);
return basLkpDao.findOne(pk);
}
}