关于java web中传参中含有HashMap的对象,resultType该如何选择类型问题分析。

一、结论:对于直接select *出来的navicat 中mysql的字段可以采用map来赋值,如果间接通过id来为其他实体类对象进行赋值的对象则会在table field引用过程中出现underfined问题,即此时是mybatis无法完成对象关系映射的,所以对于HashMap对象传参此时要精确返回类型为实体类对象方可在layui或其他jsp页面调用获取通过间接获取的实体对象属性值。

二、通过一个layui分页功能来说明类型情况选择。

1,controller文件夹:

public DataGridView selStudentPage(Student student,PageVO pageVO){
     pageVO.setPage((pageVO.getPage()-1)*pageVO.getLimit());
     Map<String,Object>map=new HashMap<>();
     map.put("page", pageVO);
     map.put("student1", student);
     List<Object>list=studentService.selStudentPage(map);//对于多个参数传值,常用哈希地图来存储
     DataGridView dataView= new DataGridView();
     dataView.setCode(0);
     dataView.setData(list);
     dataView.setCount(studentService.count(student));
     dataView.setMsg("返回成功!!");
     return dataView;
 };

2,mapping文件夹:
<select id="selStudentPage" resultType="Map" parameterType="Student">//一般resultType通过观察namespace里的命名可以获取。
    SELECT *  from student
    <where>
        <if test="student1.id!=null and student1.id!=0">//此处略去了其他一些表字段
            id=#{student1.id}
      
    </where>order by student.id asc limit #{page.page},#{page.limit}
</select>

3,model实体类
public class Student {
   private int id;

   private String idb;//如果id=1,则idb为其的00001形式

   DecimalFormat idbDecimalFormat=new DecimalFormat("00000");

     public int getId() {
    return id;
  }
public void setId(int id) {
    this.id = id;
    setIdb(idbDecimalFormat.format(id));//不够5位则在前面补0。超过5位则输出实际大小。
}
   public String getIdb() {
    return idb;
}
public void setIdb(String idb) {
    this.idb = idb;
}

}

4,运行结果:id是可以获取的,但是学生编号(idb)却undefined了。

5,作如下改动

结果获取正确的形式。(虽然resultType取实体类,但毕竟感觉跟resultType命名有点怪)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值