1.查询自定义的dto
@Query(nativeQuery = true,value="SELECT a.id as exchangeId,DATE_FORMAT(a.create_time,'%Y-%m-%d') as operationTime,p.background_url as imageUrl,a.status as isUsed,p.main_title as mainTitle,p.sub_title as subTitle,a.is_effective as isEffective from account_privilege a LEFT JOIN privilege p " + "on a.pid =p.id " + "WHERE a.type='1' and a.aid =?1") public List<Object> query(Long aid);
得到的是object列表,而不能得到对应的dto---(可能可以实现,不过没找到好的方法)
dto如下
public class PrivilegeWSDto implements Serializable{ private Long exchangeId; private String operationTime; private String imageUrl; private boolean isUsed; private String mainTitle; private String subTitle; private boolean isEffective; //setter,getter... }
自己做了一个封装,还有一部分没有完成。。。,我的实例就几个类型,所以就做了几个对比。。。
private <T> List<T> translateObjectToPojos(List<Object> items,Class<T> clazz) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException { List<T> lists = new ArrayList<T>(); for (Object object : items) { Object[] o = (Object[]) object; T t = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz); Method wM = pd.getWriteMethod();//获得写方法 if(f.getType()==String.class){ wM.invoke(t, String.valueOf(o[i])); }else if(f.getType()==Long.class||f.getType()==long.class){ wM.invoke(t, Long.valueOf(o[i].toString())); } else if(f.getType()==boolean.class||f.getType()==Boolean.class){ boolean boo = false; if(o[i].toString().equals("true")||o[i].toString().equals("1")){ boo = true; } // ....很多对比 wM.invoke(t, boo); } } lists.add(t); } return lists; }
又找到一个比较好的封装方法
private <T> List<T> translateObjectToPojos(List<Object> items,Class<T> clazz) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException { List<T> lists = new ArrayList<T>(); for (Object object : items) { Object[] o = (Object[]) object; T t = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz); Method wM = pd.getWriteMethod();//获得写方法
//ConvertUtils.convert --org.apache.commons.beanutils.ConvertUtils
wM.invoke(t, ConvertUtils.convert(o[i],f.getType())); } lists.add(t); } return lists; }
测试实例
@Test public void testGetAccountPrivilegeWSDtoList() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException { List<Object> s = manager.getAccountPrivilegeWSDtoList(8421382l); List<PrivilegeWSDto> l = translateObjectToPojos(s,PrivilegeWSDto.class); for (PrivilegeWSDto privilegeWSDto : l) { System.out.println(privilegeWSDto.getMainTitle()); } }
能达到我想要的结果,哈哈。。。
2.实体的修改
/** * 手机 */ @Modifying @Query(nativeQuery=true,value="update t_acct set tac_mobile=?2 where t_acct_seq=?1") public int updateAccountMobile(Long id,String mobile);
这个简单。。。
3.分页与排行
public Page<RewardsDetail> findByAidAndTypeOrderByCreateTimeDesc(Long aid,String type,Pageable pageable);
分页并有CreateTime排序
当然少了不了这个了
PageRequest pageRequest = new PageRequest(page-1,PAGE_SIZE,Sort.Direction.DESC, "id");
看不明白
可以看下源码
public class PageRequest extends AbstractPageRequest { /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}. * @param properties the properties to sort by, must not be {@literal null} or empty. */ public PageRequest(int page, int size, Direction direction, String... properties) { this(page, size, new Sort(direction, properties)); }