这里的nativeQuery=true
代表在执行这个方法的时候执行原生sql语句,直接写数据库中的实际表名
和表中的实际字段名
如果不写的话那么需要使用实体类去声明sql,实体类对应表,实体类中的属性名对应表中的字段名。比如
这句话中CapitalFlow
是数据库表capital_flow
对应的实体类
subAccount
也是对应的实体类CapitalFlow
中的属性对应的表字段名
自定义sql映射接口实体
实体接口
package com.xx.xx.xx.dto;
import java.util.Date;
public interface UserLastLoginTimeVO {
Long getUserId();
Date getLastLoginTime();
}
jpa的dao层
package com.xx.xx.xx.repository;
import com.xx.xx.xx.domain.UserOperLogDO;
import com.xx.xx.xx.dto.UserLastLoginTimeVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
@Repository
public interface UserOperLogRepository extends JpaRepository<UserOperLogDO, Long> {
@Query(value = "select user_id as userId,oper_time as lastLoginTime from user_oper_log where id in ( select max(id) from user_oper_log where user_id in (?1) group by user_id ) ", nativeQuery = true)
List<UserLastLoginTimeVO> getUserLastLoginTime(Set<Long> ids);
}