mybatis一对多查询过程只查询了一个结果

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fOrLOveDoit/article/details/53380751

新学mybatis,在一对多关联查询过程中出现了一点bug

首先附上我的代码

User.java

public class User
{

	public User()
	{
		// TODO Auto-generated constructor stub
	}

	private String userName;
	private String passWord;
	private int id;
	private Car car;
	private List<Order> orders;//订单
	public String getUserName()
	{
		return userName;
	}
	public void setUserName(String userName)
	{
		this.userName = userName;
	}


	@Override
	public String toString()
	{
		return "User [userName=" + userName + ", passWord=" + passWord + ", id=" + id + ", car=" + car + ", orders="
				+ orders + ", age=" + age + "]";
	}
	public List<Order> getOrders()
	{
		return orders;
	}
	public void setOrders(List<Order> orders)
	{
		this.orders = orders;
	}
	public Car getCar()
	{
		return car;
	}
	public void setCar(Car car)
	{
		this.car = car;
	}
	public String getPassWord()
	{
		return passWord;
	}
	public void setPassWord(String passWord)
	{
		this.passWord = passWord;
	}
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}

	private int age;
	
}

Car.java

public class Car
{

	private int id;
	private int pid;
	private String name;
	@Override
	public String toString()
	{
		return "Car [id=" + id + ", pid=" + pid + ", name=" + name + "]";
	}
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public int getPid()
	{
		return pid;
	}
	public void setPid(int pid)
	{
		this.pid = pid;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Car()
	{
		// TODO Auto-generated constructor stub
	}

}


Order.java//订单类

public class Order
{
	private int id;

	private int price;
	private int cid;

	public void setId(int id)
	{
		this.id = id;
	}
	@Override
	public String toString()
	{
		return "Order [id=" + id + ", price=" + price + ", cid=" + cid + "]";
	}
	public int getId()
	{
		return id;
	}
	public int getPrice()
	{
		return price;
	}
	public void setPrice(int price)
	{
		this.price = price;
	}
	public int getCid()
	{
		return cid;
	}
	
	public void setCid(int cid)
	{
		this.cid = cid;
	}
	
}

然后是user,orders,car的三个表的表结构

出于清晰明了,我这里直接截图


user 表:


car表


orders表





UserMapper.xml中的相关查询代码如下:

<select id="selectUser" resultMap="userMap">
    select * from user u inner join car on u.id=pid inner join orders on u.id=cid where u.id = #{id}
  </select>
  
  <resultMap type="u" id="userMap">
  	<id column="id" property="id"/>
  	<result column="userName" property="userName"/>
  	<result column="passWord" property="passWord"/>
  	<result column="age" property="age"/>
   	<association property="car" javaType="com.zzc.po.Car">//一对一的关联pojo
   		<id column="id" property="id"/>
  		<result column="name" property="name"/>
  		<result column="pid" property="pid"/>
  	</association>
  	<collection property="orders"   ofType="com.zzc.po.Order" >//一对多的关联pojo
  		<id column="id" property="id"/>
  		<result column="cid" property="cid"/>
  		<result column="price" property="price"/>
  	</collection>
  </resultMap>


测试:

	SqlSession  session = null;

	@Before
	public void before() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(inputStream);
		session = sqlSessionFactory.openSession();
	}
	public Tests()
	{
		// TODO Auto-generated constructor stub
	}

	@Test
	public void demo1() throws IOException{
	
		UserMapper mapper = session.getMapper(UserMapper.class);
		User user = mapper.selectUser(2);
		System.out.println(user);
//		System.out.println(mapper.insertUser("323", "111", 11));
//		User u = new User();
//		u.setPassWord("asas");
//		u.setUserName("q323");
//		System.out.println(mapper.insertUser2(u));
//		System.out.println(mapper.insertUserGetKey(u));
//		System.out.println(u.getId());
	
	}
	
	
	@After
	public void after(){
		session.commit();
		session.close();
	}


输出台部分输出:
2016 十一月 28 18:13:25,622 DEBUG selectUser:145 - ==>  Preparing: select * from user u inner join car on u.id=pid inner join orders on u.id=cid where u.id = ? 
2016 十一月 28 18:13:25,691 DEBUG selectUser:145 - ==> Parameters: 2(Integer)
2016 十一月 28 18:13:25,719 DEBUG selectUser:145 - <==      Total: 2
User [userName=www, passWord=123, id=2, car=Car [id=2, pid=2, name=rrr], orders=[Order [id=2, price=2000, cid=2]], age=101]


在navicat执行该sql:



可以看到这个user下面有两个与之关联的订单 但是java执行却只显示一个 


这不是mybatis配置问题 也不是sql语句问题

而是user表和ordesr表的主键名都为id所引起的,不知道mybatis内部是什么机制去处理的   改为不同名的主键便ok了

具体原因得在日后深入源码学习再来这里公布


mybatis一对多具体的请看:http://www.tuicool.com/articles/M3QJvu

展开阅读全文

没有更多推荐了,返回首页