Mybatis——多表操作(语法,例子)

多表

1一对多

1.1语法

一对多:需要使用many属性和@Many注解

@Result(
property = “1表JavaBean属性名”,
column = “1表字段名”,
many = @Many(select = “多表Mapper的方法签名”)
)

2 例子

JavaBean类之间的关系:

public class User {
    @Id
    private String uid;
    private String username;
..
    //一个用户有多个订单
   private List<Order> orderList = new ArrayList<>(); 
    }
public class Order {

    private String oid; 		// 订单编号
    private Date ordertime; 	// 下单时间
...
    //一个订单对应一个用户
    private User user;
    }

Mapper:

public interface OrderMapper {
/**
* 通过id查询详情
* @param uid
* @return
*/
@Select("select * from orders where uid = #{uid}")
public Order findOrdersByUserId(@Param("uid") String uid) ;
}

public interface UserMapper {
/**
* 查询所有
* @return
*/
@Select("select * from user")
@Results(id = "userResult", value = {
@Result(property = "uid", column = "uid", id = true),
@Result(property = "username", column = "username"),
@Result(property = "password", column = "password"),
@Result(property="orderList" ,
many=@Many(select="com.czxy.ssm.mapper.OrderMapper.findOrdersByUserId"),
column="uid")
})
public List<User> selectAll();
}
上一篇:TCP/UDP 协议,和 HTTP、FTP、SMTP,区别及应用场景


下一篇:手机端flex、字体设置、快速点击