MyBatis多表查询

1.多表关联

1.一对一关联

2.一对多关联 

 3.多对多

 

2.MyBatis级联查询

 1.一对一

 

2.一对多 

 

3.MyBatis缓存

1.一级缓存

 

 2.二级缓存

4.MyBatis注解开发方式 

 //1.查询User表的所有数据
    List<User> selectAll();
    //2.根据id查询用户信息
    @Select("select * from USER where id=#{id}")
    User selectById(int id);
    //3.根据用户id删除用户
    @Delete("DELETE  from USER where id=#{id}")
    void deleteById1(int id);

    //4.根据id修改用户
    @Update("UPDATE user SET username=#{username} WHERE id=#{id}")
    void updateById(User user);
    //5.添加用户
    @Insert("INSERT INTO user VALUES (null, #{username},#{birthday}, #{sex},#{address})")
    void add(User user);

 //6.根据id查询用户信息,带有多表查询(级联)
    @Select("select * from user where id=#{id}")
    @Results(value =  {
            @Result(column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "address",property = "address"),
            @Result(property = "infoData",one = @One(select="selectInfoById"),column = "id"),
            @Result(property = "orderList",many = @Many(select = "selectOrdersById"),column = "id")
    })
    User selectByIdAndOthers(int id);

    //7.根据id查询用户扩展信息
    @Select("select * from user_info where id=#{id}")
    UserInfo selectInfoById(int id);

    //8.根据id查询订单数据信息
    @Select("select * from `order` where user_id=#{id}")
    List<Order> selectOrdersById(int id);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值