数据库表中的字段并不是我们需要全部获取的,获取所有字段中的部分字段
前言
如果使用 @JsonInclude(JsonInclude.Include.NON_NULL) 注解放到实体类上,这种方法就是 如果字段值为null的字段不序列化,但是有默认值的字段依然会被查出来,所以可以通过一下方式进行解决。
mapper.xml文件
实体类中所有的字段
<!-- Student全部字段 -->
<resultMap id="BaseResultMap" type="com.domain.entity.Student">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
<result column="student_id" jdbcType="VARCHAR" property="studentId" />
<result column="student_name" jdbcType="VARCHAR" property="studentName" />
</resultMap>
实体类中的部分字段
<!-- Student中要查询的字段 -->
<resultMap id="PartStudentMap" type="com.domain.entity.Student">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="student_id" jdbcType="VARCHAR" property="studentId" />
<result column="student_name" jdbcType="VARCHAR" property="studentName" />
</resultMap>
Mybatis查询语句
<!-- Student中要查询的字段 -->
<select id="selectStudentId" parameterType="com.domain.entity.Student" resultMap="PartStudentMap">
select
id,student_id,student_name
from sutdent
where student_id= #{studentId,jdbcType=VARCHAR}
</select>
mapper层
List<Map<String, Object>> selectStudentId(String studentId);
service层
public List<Map<String, Object>> selectStudentId(String studentId) {
List<Map<String, Object>> list = studentMapper.selectStudentId(studentId);
return list;
}
controller层
public Result<List<Map<String, Object>>> getStudent(String studentId){
List<Map<String, Object>> list = studentService.selectStudentId(studentId);
return Result.success(list);
}