Mybatis注解开发使用举例(单表操作、多表操作,立即加载、延迟加载)

单表操作

public interface IUserDao {

    /**
     * 查询所有用户
     * @return
     */
    @Select(value = "select * from user")
    List<User> findAll();

    /**
     * 保存用户
     */
    @Insert(value = "insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
    void saverUser(User user);

    /**
     * 更新用户
     */
    @Update("update user set username = #{username},birthday = #{birthday},sex = #{sex},address = #{address} where uid = #{uid}")
    void updateUser(User user);


    /**
     * 删除用户
     */
    @Delete("delete from user where uid = #{uid}")
    void deleteUser(Integer id);


    /**
     * 根据id查询用户
     */
    @Select("select * from user where uid = #{uid}")
    User findById(Integer id);


    /**
     * 根据用户名称模糊查询
     */
//    @Select("select * from user where username like #{username}")//需要传百分号
    @Select("select * from user where username like '%${value}%'")//不需要传百分号
    List<User> findUserByName(String name);

    /**
     * 查询用户总数量
     */
    @Select("select count(*) from user")
    int findTotalUser();



}

多表操作(一对一,立即加载)

public interface IAccountDao {

    /**
     * 查询所有账户,并且获取每个账户所属的用户信息
     * @return
     */
    @Select("select * from account")
    @Results(id = "accountMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(property = "user",column = "uid",
                    one = @One(select="com.itheima.dao.IUserDao.findById",fetchType= FetchType.EAGER))
    })
    List<Account> findAll();

    /**
     * 根据用户id查询账户信息
     * @param userId
     * @return
     */
    @Select("select * from account where uid = #{userId}")
    List<Account> findAccountByUid(Integer userId);


}

多表操作(一对多,延迟加载)

//开启二级缓存
@CacheNamespace(blocking = true)

public interface IUserDao {
    /**
     * 查询所有用户
     *
     * @return
     */
    @Select(value = "select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "userId", column = "uid"),
            @Result(property = "userName", column = "username"),
            @Result(property = "userSex", column = "sex"),
            @Result(property = "userAddress", column = "address"),
            @Result(property = "userBirthday", column = "birthday"),
            @Result(property = "accounts", column = "uid",
                    many = @Many(select = "com.itheima.dao.IAccountDao.findAccountByUid", fetchType = FetchType.LAZY))
    })
    List<User> findAll();

    /**
     * 根据uid查询用户
     */
    @Select("select * from user where uid = #{uid}")
    @ResultMap(value = {"userMap"})
    User findById(Integer uid);


    /**
     * 根据用户名称模糊查询
     */
    @Select("select * from user where username like #{username}")//需要传百分号
//    @Select("select * from user where username like '%${value}%'")//不需要传百分号
    @ResultMap(value = {"userMap"})
    List<User> findUserByName(String name);


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值