在DAO层实现两张表的JOIN操作, 并自定义把返回的数据
- 定义返回类(需要在构造函数中进行初始化, 后续需要使用)
public class SqlQueryResult {
public SqlQueryResult(
int checkState,
long checkDate,
String legalName,
int hotelID,
int bodyID,
String password
) {
setCheckState(checkState);
setCheckDate(checkDate);
setLegalName(legalName);
setPassword(password);
setHotelbodyID(bodyID);
setHotelID(hotelID);
}
public int checkState;
public long checkDate;
public String legalName;
public String password;
public int hotelID;
public int bodyID;
}
- Dao层编写HQL语句
Hibernate不认识 JOIN…ON…, 需要把JOIN换成逗号’ , ’ , 然后直接通过WHERE来筛选
public interface HotelBodyDao extends JpaSpecificationExecutor<HotelBody>, JpaRepository<HotelBody, Integer> {
@Query(value = "SELECT
NEW myproject.response.SqlQueryResult(hc.CheckState, hc.CheckDate, hc.LegalName, hc.HotelID, hc.BodyID, hb.Password)
FROM HotelBody AS hb , HotelCheck AS hc
WHERE hb.HotelID = ?1 AND hc.HotelID = ?1 ",
nativeQuery = false)
List<SqlQueryResult> findhotel(int nHotelID);
}