汪的爱琪 2023-08-27 13:52 采纳率: 67.5%
浏览 16
已结题

mybatis查询为空

为什么我的mybatis查询为null?
这是我的SQL语句

img


然后就是我的xml文件__


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.experience.dao.User_orders_dao">
    <select id="getOrders" resultMap="Orders">
        select * from orders where
            orders.id = #{id};
    </select>
    <resultMap id="Orders" type="com.experience.entity.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="sex" column="sex"/>
        <result property="tel" column="tel"/>
        <result property="address" column="address"/>
        <collection property="orders" ofType="com.experience.entity.Orders">
            <id property="id" column="id"/>
            <result property="goodsname" column="goodsname"/>
            <result property="time" column="time"/>
        </collection>
    </resultMap>
</mapper>

我的实体类

package com.experience.entity;

import java.util.List;

public class User {
    private int id;
    private String username;
    private String password;
    private String sex;
    private String tel;
    private String address;

    private List<Orders> orders;

    public List<Orders> getOrders() {
        return orders;
    }

    public void setOrders(List<Orders> orders) {
        this.orders = orders;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }



    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", tel='" + tel + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}


实体类


package com.experience.entity;

public class Orders {

  private int id;
  private String goodsname;
  private String time;


  public long getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }


  public String getGoodsname() {
    return goodsname;
  }

  public void setGoodsname(String goodsname) {
    this.goodsname = goodsname;
  }


  public String getTime() {
    return time;
  }

  public void setTime() {
    this.time = time;
  }

  @Override
  public String toString() {
    return "Orders{" +
            "id=" + id +
            ", goodsname='" + goodsname + '\'' +
            ", time=" + time +
            '}';
  }
}

dao层

package com.experience.dao;

import com.experience.entity.Orders;
import com.experience.entity.User;

import java.util.List;

public interface User_orders_dao {
    List<User> getOrders(int id);
}


service层

package com.experience.service.impl;

import com.experience.dao.User_orders_dao;
import com.experience.entity.User;
import com.experience.service.User_orders_service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User_orders_service_impl implements User_orders_service {
    @Autowired
    User_orders_dao user_orders_dao;

    @Override
    public List<User> getOrders(int id) {
        return user_orders_dao.getOrders(id);
    }
}


Controller层


package com.experience.controller;

import com.experience.entity.Orders;
import com.experience.entity.User;
import com.experience.service.User_orders_service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class User_orders_Controller {
    @Autowired
    User_orders_service user_orders_service;
    @RequestMapping("/getList")
    public void getList(@RequestParam(value = "id",required = true) int id){
       List<User> orders = user_orders_service.getOrders(id);
       orders.stream().forEach(obj -> System.out.println(obj));
    }
}

输出结果

img

  • 写回答

2条回答 默认 最新

  • 一条有梦想的咸鱼- 2023-09-11 16:21
    关注

    dao层参数前面加一个@Param("id"),变成List getOrders(@Param("id") int id);
    另外你这个类名。。。建议了解一下java中各种命名规范

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月23日
  • 已采纳回答 2月15日
  • 创建了问题 8月27日

悬赏问题

  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)