MyBatis------4.MyBatis一对多,多对一关系的查询

一.数据库表及数据

1.各表的关系如下

MyBatis------4.MyBatis一对多,多对一关系的查询_第1张图片

2.user表

MyBatis------4.MyBatis一对多,多对一关系的查询_第2张图片

3.product表(商品表)

MyBatis------4.MyBatis一对多,多对一关系的查询_第3张图片

4.orders表(订单表)

MyBatis------4.MyBatis一对多,多对一关系的查询_第4张图片

5.orderitem(订单详情表)

MyBatis------4.MyBatis一对多,多对一关系的查询_第5张图片

 

二.项目的搭建

1.新建maven工程

MyBatis------4.MyBatis一对多,多对一关系的查询_第6张图片
2.导入相应的依赖
3.建立4个pojo类与4个表相对应
4.建立UserDao接口,定义查询方法

package com.bd.dao;

import com.bd.domain.Orders;
import com.bd.domain.Product;
import com.bd.domain.User;

public interface UserDao {
	//单表查询
	Product getProductById(String id);
	Orders getOrderById(String id);
	
	//多表查询
	User getUserAndOrder(String id);
	User getUserAndAllOrder();
	Orders getOrdersByOrdersId(String id);
	Orders getOrders_Tree(String id);
	Orders getOrders_Four(String id);
	
}

5.mybatis的配置(mapper.xml,mybatis-config.xml,db.properties)

在mapper.xml中实现接口,接下来的代码会详细说明

6.编写测试类TestShopping.java

package com.bd.test;

import java.io.IOException;
import java.io.InputStream;

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

import com.bd.dao.UserDao;
import com.bd.domain.Orders;
import com.bd.domain.Product;
import com.bd.domain.User;

import junit.framework.TestCase;

public class TestShopping extends TestCase {

	UserDao userDao;
	SqlSession session;
	{
		String resource = "mybatis-config.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			
			session = sqlSessionFactory.openSession();
			userDao=session.getMapper(UserDao.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}

	//通过id获取商品信息
	public void testGetProductByid() {
		Product product=userDao.getProductById("1");
		System.out.println(product);
	}
	
	//通过id获取订单信息
	public void testGetOrderById() {
		Orders orders=userDao.getOrderById("120d014993dc4525871adaef257dee1d");
		System.out.println(orders);
	}
	
	//通过id获取用户和相应订单信息
	public void testGetUserAndOrderById() {
		User user=userDao.getUserAndOrder("120d014993dc4525871adaef257dee1d");
		System.out.println(user);
	}
	
	//通过id获取用户和其所有订单信息
	public void testGetUserAndAllOrderById() {
		User user=userDao.getUserAndAllOrder();
		System.out.println(user);
	}
	
	// 通过订单号查询相应的user和order信息 
	public void testGetOrdersByUserId() {
		Orders orders = userDao.getOrdersByOrdersId("120d014993dc4525871adaef257dee1d");
		System.out.println(orders);
	}
	
	//三表查询
	public void testGetOrders_Tree() {
		Orders orders = userDao.getOrders_Tree("74295730485f496dba12f61bc4e35730");
		System.out.println(orders);
	}
	
	//四表查询
	public void testgetOrders_Four() {
		Orders orders = userDao.getOrders_Four("74295730485f496dba12f61bc4e35730");
		System.out.println(orders);
	}
	
}

 

接下来进入核心内容

三.一对多,多对一关系的查询





	
	

	
	
	
		
	


	
	
	
	
	
		
		
		
	

	
	
	
	
		
		
		
		
	
	
	
	
	
	
		
		
		
		
	
	
	
	
	
	
	
		
		
			
		
		
			
			
		
	
	
	
	
	
	
	
		
		
		
		
			
		
		
		
		
			
			
			
			
				
			
		
	
	

 

你可能感兴趣的:(java框架)