SpringDataJpa查询返回自定义实体方法

完整实体类
@Table(name = "st_microcode")
@Entity
@Data
public class StMicrocode {

    @Column(nullable = true, name = "id")
    @Id
    private String id;

    @Column(nullable = true, name = "id1")
    private String id1;

    @Column(nullable = true, name = "code")
    private String code;

    @Column(nullable = true, name = "name")
    private String name;

    @Column(nullable = true, name = "setvalue")
    private String setvalue;

    @Column(nullable = true, name = "issys")
    private String issys;

    @Column(nullable = true, name = "setting")
    private Long setting;

    @Column(nullable = true, name = "isset")
    private Long isset;

    @Column(nullable = true, name = "indexno")
    private Long indexno;

    @Column(nullable = true, name = "memo")
    private String memo;

    @Column(nullable = true, name = "status")
    private String status;

    @Column(nullable = true, name = "belong_comid")
    private String belongComid;

    @Column(nullable = true, name = "id2")
    private String id2;

}
自定义查询返回的dto
@Data
@AllArgsConstructor
public class MicroCodeDto {
    private String id;
    private String code;
    private String name;
}
查询方法
@Query(value = "select new com.hencego.web_service.dto.MicroCodeDto(a.id,a.code,a.name) from StMicrocode a where a.code like ?1 and a.status = '1'")
List<MicroCodeDto> findAllXDMCheckOptions(String flag);
返回结果
{"code":0,"message":"SUCCESS","result":[{"code":"355","id":"M3122","name":"选择产品"},{"code":"355.1","id":"M3123","name":"日本-阿尔法"},{"code":"355.2","id":"M3124","name":"美国-欧几里得"},{"code":"355.3","id":"M3125","name":"韩国-露晰得"},{"code":"355.4","id":"M3126","name":"中国-梦戴维"},{"code":"355.5","id":"M3127","name":"美国-鸥博"},{"code":"355.6","id":"M3128","name":"美国-菁视"}]}
发布了200 篇原创文章 · 获赞 97 · 访问量 59万+

猜你喜欢

转载自blog.csdn.net/u010838785/article/details/104020637