java - Spring Boot Hibernate、CriteriaBuilder

标签 java spring-boot mapping hibernate-criteria

是否可以从 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/

相关文章:

java - 使用netbeans调试java代码

c# - NHibernate:更改父级 - "deleted object would be re-saved by cascade"

java - Java 中的条件映射框架

javascript - 动态调整 D3 TopoJSON map 上的点大小

java - Android 自定义通知 RemoView 不使用样式

java - 无法发布到黑板构建 block

spring - 如何使用 spring-boot 外部化数据源配置?

java - 如何在Java Spring boot应用程序中实现TLS 1.3?

java - 在 Spring Boot 中将 .csv 文件上传到数据库

java - Gradle 依赖排除后依赖仍然在类路径上