繁体   English   中英

如何在springboot中为微服务构建自定义存储库

[英]How to build customise repository in springboot for microservices

如何在 Java 中为微服务设计自己的自定义存储库

例如: search/user? 将给出所有用户详细信息 search/user?name=john 将给出数据只有 name 和 have john Search/user?name=john&salary=100 将给出特定数据到 name 和薪水 search/user?name =john&salary=100&add=china 会给出具体记录姓名工资和地址

Same way the this one Url search/user will take one or n number of define request object in it so that this single Api will give output json objects as per the inputs provided

下面是我的代码和我在加入查询时遇到的错误

移动实体:

@Entity
@Table(name = "MOBILE")
public class Mobile implements Serializable {

//Logger logger = (Logger) LoggerFactory.getLogger(Mobile.class);

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "brand")
private String brand;

@Column(name = "phone")
private String phone;

@Column(name = "picture")
private String picture;

@Column(name = "sim")
private String sim;

@Column(name = "resolution")
private String resolution;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="hardware_id",referencedColumnName = "id")
private Hardware hardware;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="releases_id",referencedColumnName = "id")
private Releases releases;

硬件实体

@Entity
@Table(name="Hardware")
public class Hardware implements Serializable {

//Logger logger = (Logger) LoggerFactory.getLogger(Hardware.class);

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name="audioJacks")
private String audioJacks;

@Column(name = "gps")
private String gps;


@Column(name = "battery")
private String battery;
enter code here

发布实体 class

enter code here
 @Entity
 @Table(name="Releases")
 public class Releases implements Serializable {

 //Logger logger = (Logger) LoggerFactory.getLogger(Releases.class);



 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private long id;

 @Column(name="priceEuro")
 private long priceEuro;

 @Column(name="announceDate")
 private String announceDate;

重新实现

  @Repository
  @Transactional(readOnly = true)
  public class UserRepositoryCustomizedImpl implements UserRepoCustom {

@PersistenceContext
EntityManager entityManager;

@Override
public List<UserInformation> search(Map<String, String> requestMap) {

    if(requestMap!=null && requestMap.isEmpty()) {
    }

  //Below commented all the query i tried i got the error for all it is query which is wrong i am writing help me with the write query :)



    //String qry="Select m.brand,m.phone,m.picture,h.audioJacks,h.gps from 
com.example.users.m.hardware_id.Mobile m inner join 
com.example.users.entities.Hardware h where m.hardware_id=h.id ";
    //SELECT DISTINCT e FROM Employee e INNER JOIN e.tasks t
    //String qry="select m from Mobile m inner join m.hardware h";
    String qry="Select m.brand from Mobile m inner join m.hardware_id h 
where h.id=m.hardware_id";
System.out.println("Query is "+ qry);

    Query query = entityManager.createQuery(qry,Hardware.class);

    return query.getResultList();
  }

}

String qry="Select m.brand from Mobile m inner join m.hardware_id h where h.id=m.hardware_id";

您将hardware_id称为连接属性(假设它是连接列名称)

您应该改用实体 class 中的属性名称。 hardware

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM