欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mybatis“集合嵌套查询”和“集合嵌套结果”两种方法实现数据库

程序员文章站 2022-05-03 17:02:59
...

两个实体类分别如下:User用户类和Goods商品类。一个用户对应多个商品(一对多) package com.leo.entity;import java.util.List;public class User {private Integer id;private String username;private Integer age;private String address;private ListG

两个实体类分别如下:User用户类和Goods商品类。一个用户对应多个商品(一对多)

package com.leo.entity;

import java.util.List;
public class User {
	private Integer id;
	private String username;
	private Integer age;
	private String address;
	private List goodsList;

	public List getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List goodsList) {
		this.goodsList = goodsList;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", age=" + age
				+ ", address=" + address + ", goodsList=" + goodsList + "]";
	}
	
	
	
	
	
}

Goods商品类

package com.leo.entity;

public class Goods {
	private Integer id;
	private String goodsName;
	private Integer goodsNumber;
	private Integer user_id;
	
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getGoodsName() {
		return goodsName;
	}
	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}
	public Integer getGoodsNumber() {
		return goodsNumber;
	}
	public void setGoodsNumber(Integer goodsNumber) {
		this.goodsNumber = goodsNumber;
	}
	public Integer getUser_id() {
		return user_id;
	}
	public void setUser_id(Integer user_id) {
		this.user_id = user_id;
	}
	
	
	
	
}

User实体类的mapper映射文件:UserDao.xml


Goods实体类的mapper映射文件:GoodsDao.xml



mabatis的环境配置文件mabatis-config.xml
测试的servlet(也可以用main函数测试)

package com.leo.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.leo.entity.Goods;
import com.leo.entity.User;
import com.leo.mapper.GoodsDao;
import com.leo.mapper.UserDao;



/**
 * Servlet implementation class MybatisServlet
 */
@WebServlet("/MybatisServlet")
public class MybatisServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InputStream is = Resources.getResourceAsStream("com/leo/resources/mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		
//		UserDao ud = session.getMapper(UserDao.class);
		GoodsDao gd = session.getMapper(GoodsDao.class);

		List goodsList= gd.selectGoodsForUser(1);
		
//		User user = ud.getUserinfoById(1);		
//		System.out.println(user);
//		List goodsList  =  user.getGoodsList();
		for (Goods goods : goodsList) {
			System.out.println(goods.getId()+"   "+ goods.getGoodsName()+"   "+goods.getGoodsNumber()+ "  "+ goods.getUser_id());
		}
		session.commit();
		session.close();
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		
	}

}

以上是集合嵌套查询,还有一种方式是集合嵌套结果,这种方式只需要一个实体类文件即可,它是一种级联查询,自动完成的

下面用集合嵌套结果这种方式:

只需要改动UserDao.xml,且只是用这一个映射文件就可以完成



希望可以帮到大家,有什么措辞不正确,希望得到指正,希望进步