spring-data-jpa需要特别弥补的地方
使用spring-data-jpa需要特别弥补的地方,这点mapper处理比较好
//弥补jpa单实体的不足用不同的工厂类操作复杂sql,mapper这点比较好在处理了单实体查询直接不用daoimpl下,即使用
xml进行复杂查询也不需要daoimpl
jpa 复杂操作弥补:
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;
}
}
jpa dao层直接操作(只限于单实体):
@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);
}
}
上一篇: 云游科技何千军:游戏开发和后端逻辑经验
下一篇: 通用mapper集成,及代码成认识