MyBatis数据封装

37 阅读1分钟

实体类属性名和数据库表查询返回的字段名一致,mybatis会自动封装,否则不能自动封装

方法:给字段起别名,让别名和属性名一致

实例

原始代码

@Select("select * from emp where id = 1")
public Emp selectById(Integer id);

查询效果

image.png

因为后面三个属性名和字段名不一致,所以无法封装和赋值

修改后的代码

@Select("select id, username, password, name, gender, image, job, entrydate, "+
        "dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
public Emp selectById(Integer id);

image.png 每个字段都赋值了