jpa源生sql在dao的实现类查询

  @Autowired
    EntityManager entityManager;

    @Override
    public List getSel(String name) {
        StringBuffer sql=new StringBuffer("SELECT f.id,f.title,f.createDate,f.content,f.classId,c.cname FROM faqs f\n" +
                "LEFT JOIN classes c on f.classId=c.id where 1=1");
        Query query=null;
        if(name!=null&&!name.equals("")){
            sql.append("  and f.title like CONCAT('%',:name,'%') ");
            query= entityManager.createNativeQuery(sql.toString()).setParameter("name",name);
        }else{
            query=entityManager.createNativeQuery(sql.toString());
        }
        return query.getResultList();
    }
  @Autowired
    FaqsDao faqsDao;
    @Override
    public List getSel(String name) {
        List obj=faqsDao.getSel(name);
        List list=new ArrayList<>();
        for (Object[] ob :obj){
            Faqs faqs=new Faqs();
            faqs.setId(Integer.parseInt(ob[0].toString()));
            faqs.setTitle(ob[1].toString());

            faqs.setCreateDate(ob[2].toString());
            faqs.setContent(ob[3].toString());
            faqs.setClassId(Integer.parseInt(ob[4].toString()));
             faqs.setCname(ob[5].toString());
            list.add(faqs);
        }
        return list;
    } 
   

 

转载于:https://my.oschina.net/u/4134908/blog/3056711

你可能感兴趣的:(jpa源生sql在dao的实现类查询)