【问题标题】:SpringBoot Hibernate, CriteriaBuilderSpring Boot 休眠,CriteriaBuilder
【发布时间】: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


【解决方案1】:

Base DemoController 你已经有一个 DemoService 可以在其他控制器之间重用。所以如果你有

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

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

您可以创建控制器:

@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);
    }

}

【讨论】:

  • 是的,它将显示来自 AModel 和 BModel 的所有内容。这可以。但是如果我想要某些类型的参数,例如我只想从模型中选择一个变量并在 DemoController 中检索它。
  • 如果您“只想从模型中选择一个变量”,那么为什么不在DemoService 中创建一个专用方法并在控制器中使用它。 @Stunner_94
  • 我分享的 DAO 实现,你能检查一下它是否进行了必要的更改。
  • 我不确定此代码是否按条件搜索:int User_id、int locationNoId。可以使用 Spring 数据docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/… 或者查看baeldung.com/spring-data-repositories
猜你喜欢
  • 2018-12-16
  • 2019-09-27
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
相关资源
最近更新 更多