gpt4 book ai didi

java - Spring Boot Hibernate、CriteriaBuilder

转载 作者:行者123 更新时间:2023-12-02 00:34:04 25 4
gpt4 key购买 nike

是否可以从 Controller 具有不同型号的 hibernate 中的 3 个表中获取数据。

代码AModel.Java

@Entity
@Table(name="demo")
//@NamedQuery(name="Demo.findAll", query="SELECT m FROM Demo m")
public class AModel implements Serializable {

@Column(name="demo_loc")
private String location;
@Column(name="name")
private String name;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

BModel.Java
@Entity
@Table(name="location")
//@NamedQuery(name="City.findAll", query="SELECT c FROM City c")
public class BModel implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="locationNoid")
private int locationNoId;

@Column(name="Code")
private int Code;
public int getLocationNoId() {
return locationNoId;
}
public void setLocationNoId(int locationNoId) {
this.locationNoId = locationNoId;
}
public int getCode() {
return Code;
}
public void setCode(int code) {
Code = code;
}
}



CModel.java
@Entity
@Table(name="offers")
public class CModel implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int OfferShopid;

@Column(name="offerCount")
private int offerCount;

@Column(name="foodCount")
private int foodCount;

public int getOfferShopid() {
return OfferShopid;
}
public void setOfferShopid(int offerShopid) {
OfferShopid = offerShopid;
}
public int getOfferCount() {
return offerCount;
}
public void setOfferCount(int offerCount) {
this.offerCount = offerCount;
}
public int getFoodCount() {
return foodCount;
}
public void setFoodCount(int foodCount) {
this.foodCount = foodCount;
}


}




DemoDTO.java

public class LocationDTO {

private int demoId;
private String name;
private int Code;
private int foodCount;


public int getDemoId() {
return demoId;
}
public void setDemoId(int demoId) {
this.demoId = demoId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCode() {
return Code;
}
public void setCode(int code) {
Code = code;
}
public int getFoodCount() {
return foodCount;
}
public void setFoodCount(int foodCount) {
this.foodCount = foodCount;
}
}


DemoController.java
@RestController
@RequestMapping("/api/abcService")
@Api(description = "REST API to list details")

public class DemoController {
@Autowired
private DemoService DemoService;

@RequestMapping(value = "/list/v1/{user_id}/uid/{locationNoId}/location",
method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "Get all Lists", notes = "Get all Address related
information")
public ResponseEntity<?> getAll(@PathVariable("user_id") int userId,
@PathVariable("locationNoId") int locationNoId)
{
try {
ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<List<DemoDTO>>() {
}.getType();
List<DemoDTO> listAll=DemoService.ListAll(userId,locationNoId);
return new ResponseEntity<>(listAll, HttpStatus.OK);
}catch (Exception ex){
String errorMessage;
errorMessage = ex.getMessage();
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}

}
}

演示DAOImpl

@Component
@Repository
@Transactional
public class DemoDAOImpl implements DemoDAO {

@PersistenceContext
private EntityManager entityManager;

@Autowired
private AService aService;

AModel aModel;


@Override
public @ResponseBody List<DemoDTO> ListAll(int User_id, int locationNoId) {
// TODO Auto-generated method stub



List<DemoDTO> listDemo=new ArrayList<>();

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<AModel> query =
criteriaBuilder.createQuery(AModel.class); Root<AModel> root =
query.from(AModel.class); query.select(root); CriteriaQuery<AModel>
select = query.select(root); TypedQuery<AModel> typedQuery =
entityManager.createQuery(select); List<AModel> AllLists =
typedQuery.getResultList();

for(AModel listofAll :AllLists ) {
System.out.println(listofAll.getid());
System.out.println(listofAll.getname());
System.out.println(listofAll.getlocation());
System.out.println(listofAll.getid());
System.out.println(listofAll.getRating());
listMalls.(AllLists);

return listMalls;

}

最佳答案

Base DemoController 您已经拥有一个可以在其他 Controller 之间重用的 DemoService。所以如果你有

@RestController
@RequestMapping("/api/abcServiceA")
public class DemoControllerA {
@Autowired
private DemoServiceA demoServiceA;

@RestController
@RequestMapping("/api/abcServiceB")
public class DemoControllerB {
@Autowired
private DemoServiceB demoServiceB;

您可以创建 Controller :

@RestController
@RequestMapping("/api/abcServiceB")
public class DemoControllerD {
@Autowired
private DemoServiceA demoServiceA;
@Autowired
private DemoServiceB demoServiceB;

以一种方法使用这两项服务:

@RequestMapping(value = "/list/v1/{user_id}/uid/{locationNoId}/location",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "Get all Lists", notes = "Get all Address related information")
public ResponseEntity<?>getAll(@PathVariable("user_id")int userId,
@PathVariable("locationNoId")int locationNoId) {
try {
ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<List<DemoDTO>>() {}.getType();
List<DemoDTO> listAllFromA = aemoServiceA.ListAll(userId, locationNoId);
List<DemoDTO> listAllFromB = aemoServiceB.ListAll(userId, locationNoId);
// do something with listAllFromA and listAllFromB
return new ResponseEntity<>(listAll, HttpStatus.OK);
} catch (Exception ex) {
String errorMessage;
errorMessage = ex.getMessage();
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}

}

关于java - Spring Boot Hibernate、CriteriaBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com