根据用户ID,查询用户信息,包括用户编号,需要用到tb_user与tb_idcard两张表
分别创建接口 编写查询方法
package com.yiwu.mapper;
import com.yiwu.pojo.Idcard;
import org.apache.ibatis.annotations.Select;
public interface IdCardMapper {
@Select("select * from tb_idcard where id = #{id} ;")
Idcard findIdcardById(int id);
}
public interface TUserMapper {
@Select("select * from tb_user where uid = #{uid}")
@Results(
@Result(property = "uidcard",
column = "ucard_id",
one=@One(select = "com.yiwu.mapper.IdCardMapper.findIdcardById"))
)
TUser findUserByid(int id);
}
编写测试方法
@Test
public void test01(){
TUser userByid = mapper.findUserByid(1);
System.out.println(userByid);
}
@Select(" select u.*,card.id as cardID,card.code as ucard " +
" from tb_user u,tb_idcard card \n" +
" where u.uid = #{uid} and u.ucard_id = card.id")
@Results({
@Result(property = "uidcard.id",
column = "cardID"
),
@Result(
property = "uidcard.code",
column = "ucard"
)
}
)
TUser findUserByid2(int id);
根据用户ID,查询用户信息,包括用户编号,需要用到tb_user与tb_order两张表
创建接口
package com.yiwu.mapper;
import com.yiwu.pojo.Order;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface OrderMapper {
@Select("select * from tb_orders where user_id = #{uid} ;")
List<Order> findUserOrdrsByUid(int uid);
}
}
@Select("select * from tb_user where uid = #{id} ;")
@Results({
@Result(
property = "orderList",
column ="uid",
many = @Many(select = "com.yiwu.mapper.OrderMapper.findOrdersByUserId")),
}
)
TUser findUserOrderById(int id);
编写测试方法
@Test
public void test02(){
TUser userOrderByid = mapper.findUserOrderById(1);
System.out.println(userOrderByid);
}
使用订单表(tb_orders)与商品表(tb_product)借助中间表(tb_ordersitem)
实现多对多查询,根据产品订单,查询该订单所包含的商品。
分别创建接口 编写查询方法
package com.yiwu.mapper;
import com.yiwu.pojo.Product;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface ProductMapper {
@Select("select *" +
"from tb_product " +
"where pd_id in (select pd_id from tb_ordersitem where or_id = #{orid})")
List<Product> findProductbyId(int pdId);
}
package com.yiwu.mapper;
import com.yiwu.pojo.Order;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface OrderMapper {
@Select("select * from tb_orders where user_id = #{id} ;")
List<Order> findOrdersByUserId(int id);
@Select("select * from tb_orders where or_id = #{id};")
@Results(
@Result(
property = "productList",
column = "or_id",
many = @Many(select = "com.yiwu.mapper.ProductMapper.findProductbyId")
)
)
Order findOrders(int id);
}
编写测试方法
import com.yiwu.mapper.OrderMapper;
import com.yiwu.mapper.TUserMapper;
import com.yiwu.pojo.TUser;
import com.yiwu.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class OrderTest {
private OrderMapper mapper;
private SqlSession sqlSession;
@Before
public void init(){
sqlSession = MyBatisUtils.geSqlSession();
mapper = sqlSession.getMapper(OrderMapper.class);
}
@Test
public void test01(){
mapper.findOrders(1);
}
@After
public void destory(){
sqlSession.commit();
sqlSession.close();
}
}
重录版-推荐(36讲,无法分享组,点击前往bilibili查看全集)
32 注解方式实现一对一查询方式1