当表字段于实体属性不一致时,则映射失败,值无法注入,因为orm就来解决这个问题
两个方法:设置别名与结果映射
设置别名
dao层语句
//取别名
public List<Manager> selectAll();
mapper语句
<select id="selectAll" resultType="manager">
select mgr_id id,mgr_name name,mgr_pwd pwd from t_manager
</select
测试层语句
ManagerDao managerDao = MyBatisUtils.getMapper(ManagerDao.class);
List<Manager> managers = managerDao.selectAll();
结果映射
dao层语句
//结果映射
public List<Manager> selectAllOrm();
mapper语句文章来源:https://www.toymoban.com/news/detail-546818.html
<resultMap id="rm" type="manager">
<id property="id" column="mgr_id"></id>
<result property="name" column="mgr_name"></result>
<result property="pwd" column="mgr_pwd"></result>
</resultMap>
<select id="selectAllOrm" resultMap="rm">
select * from t_manager
</select>
测试层语句文章来源地址https://www.toymoban.com/news/detail-546818.html
List<Manager> orm = managerDao.selectAllOrm();
System.out.println(orm);
到了这里,关于如何在MyBatis中完成ORM的映射的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!