为什么我的mybatis查询为null?
这是我的SQL语句
<?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));
}
}
输出结果
【以下回答由 GPT 生成】
首先,我们要检查以下几个方面来确定为什么MyBatis查询不返回任何结果:
数据库连接是否有效:请确保数据库连接配置正确并且可成功连接到数据库。可以尝试使用其他数据库工具连接数据库测试一下。
SQL语句是否正确:请检查你的SQL语句是否正确。可以尝试在数据库工具中执行这个SQL语句查看是否有结果返回。
MyBatis配置是否正确:请确保你的MyBatis配置文件正确,并且Mapper文件已经正确映射到对应的DAO接口。
经过检查发现你的Mapper文件中有一点错误,导致查询结果为空。在resultMap的定义中,你将orders的类型声明为了User,但实际上你想要查询的是Orders表的数据,所以应该将类型声明为Orders。修改resultMap的定义如下:
<resultMap id="Orders" type="com.experience.entity.Orders">
然后修改查询语句,将查询结果映射到定义的resultMap中,修改select语句如下:
<select id="getOrders" resultMap="Orders">
select * from orders where
orders.id = #{id};
</select>
接下来,确认以下几个问题:
确定你的DAO接口方法名称与Mapper文件中定义的id一致。
确定传递给查询方法的参数与Mapper文件中定义的参数一致。
确定查询方法的返回值类型与Mapper文件中定义的resultMap一致。
如果以上几个步骤没有问题的话,你的查询应该可以返回正确的结果了。如果还有问题,请提供更多关于你的DAO接口、调用代码以及查询参数的信息,以便更好地帮助你解决问题。
【相关推荐】
dao层参数前面加一个@Param("id"),变成List getOrders(@Param("id") int id);
另外你这个类名。。。建议了解一下java中各种命名规范