簡體   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