我的存储库
public interface ProductRepository extends JpaRepository<Product,Integer> {
@Query(value = "select mp.*,u.name,p.name, p.ext_id from merchant_product mp join campaign c on c.product_id = mp.id and c.status in (4, 8)join product p on p.id = mp.product_id join user u on u.id = mp.user_id where mp.status = 4 and mp.availability = 'Y';", nativeQuery = true)
List<Object> getAllProduct();
}
这是我在Spring Boot中的查询我正在使用Spring数据JPA。我需要将它映射到Pojo类。这样我就可以用它来进一步处理。
任何人都可以帮助我。
我的pojo
@Data
@AllArgsConstructor
public class Product {
@Id
//data of merchant product table
//data of user table
private int id;
private String name;
private String ext_id;
}
答案 0 :(得分:2)
@Query(nativeQuery = true, name = "test", value = "select mp.* ...")
@SqlResultSetMapping(name="test", classes = {
@ConstructorResult(targetClass = Product.class,
columns = {@ColumnResult(name="name"), @ColumnResult(name="id")}, @ColumnResult(name="ext_id")})
})
添加SqlResultSetMapping以初始化映射对应于Pojo字段的查询中的哪些列
答案 1 :(得分:0)
我在Product和call -
上为你想要的参数创建一个构造函数 public interface ProductRepository extends JpaRepository<Product,Integer> {
@Query(value = "select new Product(u.name,p.name, p.ext_id) from merchant_product mp join campaign c on c.product_id = mp.id and c.status in (4, 8)join product p on p.id = mp.product_id join user u on u.id = mp.user_id where mp.status = 4 and mp.availability = 'Y';", nativeQuery = true)
List<Product> getAllProduct();
}
您需要在对象中定义通配符mp。*中的参数以进行映射