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.426957,30.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;
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已省略
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);
}
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);
}
}
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;
}
}
1、How to join unrelated entities with JPA and Hibernate
2、Oracle左连接、右连接、全外连接以及(+)号用法