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);
}
在mapper.xml中实现接口,接下来的代码会详细说明
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);
}
}
接下来进入核心内容