jpa@Query左连接返回Object

1、数据库简略设计

CUSTOM表:
jpa@Query左连接返回Object_第1张图片
jpa@Query左连接返回Object_第2张图片
SERVICE_SHEET表:
SERVICE_SHEET表
jpa@Query左连接返回Object_第3张图片

2、SQL语句设计

SELECT DISTINCT
	a.CUSTOM_NAME,
	a.LATITUDE,
	a.LONGITUDE,
	COUNT( b.CUSTOMER_ID )  AS SUM_OF_SERVICE
FROM  
	CUSTOM a
LEFT JOIN
	SERVICE_SHEET b
ON
	DISTANCE ( 114.42695730.513611, a.LONGITUDE, a.LATITUDE ) < 2 
AND
	a.ID = b.CUSTOMER_ID
GROUP BY
	a.CUSTOM_NAME,
	a.LATITUDE,
	a.LONGITUDE
ORDER BY
	a.CUSTOM_NAME
ASC;

查询结果:
jpa@Query左连接返回Object_第4张图片

3、实体类定义

Custom.java

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Custom {
    @Id
    private Integer ID;
    private Float CUSTOM_NO;
    private Float LONGITUDE;
    private Float LATITUDE;
    private String CUSTOM_NAME;

    //getter setter已省略
}

Service_shett.java:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Service_sheet {
    @Id
    private Integer ID;
    private Integer SUPPLIER_ID;
    private  Integer CUSTOMER_ID;
    //getter and setter已省略

4、jpa实现

CustomRepository.java

import com.example.demo.entity.Custom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface ClientRepository extends JpaRepository<Custom,Integer> {

    //调用数据库自定义函数
    @Query(value="SELECT DISTINCT " +
            "a.CUSTOM_NAME," +
            "a.LATITUDE," +
            "a.LONGITUDE, " +
            "function('COUNT', b.CUSTOMER_ID )  AS SUM_OF_SERVICE " +
            " FROM " +
            "Custom a" +
            " LEFT JOIN " +
            "Service_sheet b" +
            " ON " +
            "function('DISTANCE', ?1,?2, a.LONGITUDE, a.LATITUDE ) < 10 " +
            " AND " +
            "a.ID = b.CUSTOMER_ID" +
            " GROUP BY " +
            "a.CUSTOM_NAME," +
            "a.LATITUDE," +
            "a.LONGITUDE" +
            " ORDER BY " +
            "a.CUSTOM_NAME" +
            " ASC")
    public List<Object[]> findNearClient(Float LONGITUDE,Float LATITUDE);
}

5、接口定义与实现

CustomService.java

import com.example.demo.entity.Custom;
import java.util.List;

public interface ClientService {
    List<Object[]> findNearClient(Float LONGITUDE, Float LATITUDE);

}

CustomServiceImpl.java

import com.example.demo.entity.Custom;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.repository.CustomRepository;
import com.example.demo.service.CustomService;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientRepository clientRepository;

    @Override
    public List<Object[]> findNearClient(Float LONGITUDE, Float LATITUDE) {
        return clientRepository.findNearClient(LONGITUDE,LATITUDE);
    }

}

6、控制器实现

CustomController.java

import com.example.demo.entity.Custom;
import com.example.demo.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/")
public class ClientController {
    @Autowired
    private ClientService clientService;

    //获取附近客户列表
    @PostMapping("/wxGetNearClientList")
    public List<Object[]> getNearClientList
    (@RequestParam(name="LONGITUDE",required = true) Float LONGITUDE,
     @RequestParam(name="LATITUDE",required = true) Float LATITUDE){
        List<Object[]> nearClientList=clientService.findNearClient(LONGITUDE,LATITUDE);
        return nearClientList;
    }

}

7、测试连接情况

jpa@Query左连接返回Object_第5张图片

8、参考资料

1、How to join unrelated entities with JPA and Hibernate
2、Oracle左连接、右连接、全外连接以及(+)号用法

你可能感兴趣的:(SpringBoot)