【发布时间】:2019-09-19 15:07:12
【问题描述】:
是否可以在休眠状态下从控制器属于不同型号的 3 个表中获取数据。
codeAModel.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;
}
【问题讨论】:
-
可能是的 - 你能提供更多细节吗?
-
好的。有 3 个控制器(A、B、C)成功完成了 CRUD 操作。但我想做的是,创建另一个控制器(即 D),我想检索(A,B,C)的数据并显示它。
-
据我了解:3 个控制器(A、B、C)通过其他类(服务、存储库)进行此 CRUD 操作,并将 rest api 作为公共方法公开。好的方法是重构从控制器到服务的所有逻辑。例如控制器 A 使用服务 A。然后您可以在新控制器 D 中使用此服务(A、B、C)。您能否给出其中一个控件的一些代码,一种 REST 方法现在看起来如何?
-
@Iczapski 我分享了一个示例。
-
好的,我看到了实体和 DTO。但我想看看控制器有什么注释:RestController o RequestMapping
标签: java spring-boot mapping hibernate-criteria