67,513
社区成员
发帖
与我相关
我的任务
分享
@Query(value = "select id,appid,name,logo_url ,url,phone,introduce,description from t_org where id=?1", nativeQuery = true)
Org findSimpleOrg2(Integer orgId);
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String appid;
private String appSecret;
private String mchid;
private String mchSecret;
private String name;//名称
private BigDecimal balance;//账号预存款余额
private Date createdTime;
private Date updatedTime;
private String logoUrl;//图标url
private String url;//首页链接
private String qrcodeUrl;//二维码url
private String phone;//联系电话
private String businessLicense;//营业执照
private String username;//登陆账号
private String introduce;//关于我们
private String description;//描述
private String uploadVerify;//上传验证文件路径
@Query(value = "select o.id,o.appid,o.name,o.logoUrl,o.url,o.phone,o.introduce,o.description from Org o where o.id=?1")
Org findSimpleOrg2(Integer orgId);
方法二:
使用 Projection,这里有Spring Data JPA 官网Projection介绍,创建一个OrgProjection接口,里面包含所需属性的get方法,可以把Org中的get方法复制过去,由于我不知道Org类中各个属性的类型,我就随便写了,代码如下,
public interface OrgProjection {
Integer getId();
String getAppid();
String getName();
String getLogoUrl();
String getUrl();
String getPhone();
String getIntroduce();
String getDescription();
}
Repository中的方法
OrgProjection findSimpleOrg1(Integer orgId);
使用这种方法,如果需要的不同属性的情况很多,比如:三个字段,四个字段,五个字段,那么就要有相应的Projection接口,里面有三个字段的get方法,四个字段的get方法,五个字段get方法.
也可以使用 Dynamic Projection
Repository中的方法
<T> T findSimpleOrg1(Integer orgId, Class<T> type);
方法三:
使用你之前说的 Map<String, Object> findSimpleOrg1(Integer orgId);
@Query(value = "select id,appid,name,logo_url logoUrl,url,phone,ifnull(introduce,'') introduce,ifnull(description,'') description\n " +
"from t_org where id=?1 ", nativeQuery = true)
Map<String, Object> findSimpleOrg1(Integer orgId);
Map<String, Object> result = orgRepository.findSimpleOrg1(1623);
String json = JSONObject.toJSONString(result);
Org testOrg = JSONObject.parseObject(json, Org.class);