mybaits04 复习延迟加载 缓存,学习注解开发

mybatis

复习
  1. 复习延迟加载

    1. 开启延迟加载

      1. sqlMapcoonfig.xml主配置文件开启如下

      2.  <setting name="lazyLoadingEnabled" value="true"/>
                <setting name="aggressiveLazyLoading" value="true"/>
        
        1. 配置实现
          1. select属性指定配置文件 确认sql语句位置
          2. column属性 获取查询条件的数据(也就是另一张表根据什么列表属性查询的)

      ​ userDao.xml

<resultMap id="findAllMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>
        <collection property="accounts" column="id" ofType="account" select="com.qqhru.wjy.dao.AccountDao.findById"></collection>
    </resultMap>
    <select id="findAll" resultMap="findAllMap">
    <!-- select user.*, a.id as aid, a.money as amoney from user left join account a on a.uid = user.id-->
        select * from user
    </select>

​ accountDao.xml

    <select id="findById" resultType="account">
        select * from account where uid = #{uid}
    </select>
  1. 复习一级缓存,二级缓存
    1. 一级缓存, 存储在Sqlsession中
    2. 二级缓存,存储在SqlSessionFactory中
注解开发
  1. 环境搭建

  2. 单表crud

  3.   @Insert("insert into user (id, username, birthday, sex, address) values (#{id},#{username},#{birthday},#{sex},#{address})")
        void insertUser(User user);
    
        @Update("update user set username = #{username}, address = #{address}, birthday = #{birthday},sex = #{sex} where  id = #{id}")
        void updateUser(User user);
    
        @Delete("delete from user where id = #{id} ")
        void dropUser(Integer id);
    
  4. 多表

    1. results 和 result 注解

    2. ID

    3. one

    4. many

      一对多
          @Select(value = "select * from user")
          @Results(id = "userMap", value = {
                  @Result(id = true, property = "id",column = "id"),
                  @Result(property = "username", column = "username"),
                  @Result(property = "birthday", column = "birthday"),
                  @Result(property = "sex", column = "sex"),
                  @Result(property = "address", column = "address"),
                  @Result(property = "accounts", column = "id" ,
                          many = @Many (select = "com.qqhru.wjy.dao.AccountDao.findById", fetchType = FetchType.LAZY))
          })
          List<User> findAll();
      
      一对一
      @Select(value = "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.qqhru.wjy.dao.UserDao.findById", fetchType = FetchType.LAZY
                  ))
          })
          List<Account> findAll();
      

      缓存

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值