Mybatis学习【4】

1. 延迟加载和立即加载:

①:延迟加载:按需查询(多对多、一对多);

②:立即加载:不管用不用全部查询(一对一、多对一)。

③:延迟加载多对一的实现:

IAccountDao.XML配置:

    <resultMap id="accountUserMap" type="cn.xupt.domain.Account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <association property="user" column="uid" javaType="cn.xupt.domain.User" select="cn.xupt.dao.IUserDao.findById"></association>
    </resultMap>

    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>

IUserDao.XML配置:

    <select id="findById" parameterType="int" resultType="cn.xupt.domain.User">
        select * from user where id=#{uid}
    </select>

2. Mybatis的缓存:

①:一级缓存:

SqlSession级别的缓存,操作数据库时需要构造SQLSession对象, 在对象中有一个数据结构(HashMap)用于存储缓存数据,不同的SQLSession对象之间的缓存数据是不共享的,即独立的,查数据时先在SqlSession对象中找若没有再去数据库里找。

注意:在进行添加、删除、修改、commit、close操作时一级缓存会清空。

②:二级缓存:

SqlSessionFactory级别的缓存,由同一个SqlSessionFactory对象创建的SqlSession可以共享其缓存。

使用步骤:

(1):SqlMapConfig.XML配置setting使Mybatis支持二级缓存;

<settings>
    <setting name="cacheEnabled" value="true"></setting>
</settings>

(2):配置IUserDao.XML;

<mapper>
    <cache/>
</mapper>

(3):让当前操作支持二级缓存,即配置select标签:

<select useCache="true">
    select * from user
</select>

注意:二级缓存存的都是零散的信息,当查询user对象时,创建一个新的对象,将这些信息存在这个对象里。

3. Mabatis的注解开发:

①:CRUD操作:

public interface IUserDao {

    /**
     * 查所有
     * @return
     */
    @Select("select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(id = false, property = "username", column = "username"),
            @Result(id = false, property = "address", column = "address"),
            @Result(id = false, property = "sex", column = "sex"),
            @Result(id = false, property = "birthday", column = "birthday")
    })
    List<User> findAll();

    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into user(username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address})")
    void saveUser(User user);

    /**
     * 修改用户
     * @param user
     */
    @Update("update user set username=#{username}, birthday=#{birthday}, sex=#{sex}, address=#{address} where id=#{id}")
    void updateUser(User user);

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

    /**
     * 通过id查用户
     * @param id
     * @return
     */
    @Select("select * from user where id=#{id}")
    @ResultMap("userMap")
    User findById(Integer id);

    /**
     * 通过username查用户(模糊查询)
     * @param username
     * @return
     */
    //@Select("select * from user where username like '#{value}'")
    @Select("select * from user where username like #{username}")
    List<User> findByUsername(String username);

    /**
     * 计算总数
     * @return
     */
    @Select("select count(*) from user")
    int countAll();
}

注意:Results即相当于ResultMap。

②:一对一(多对一):

注解配置:

    /**
     * 查所有账户及其所属用户
     * @return
     */
    @Select("select * from account")
    @Results(id = "accountMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "money", property = "money"),
            @Result(column = "uid", property = "user", one = @One(select = "cn.xupt.dao.IUserDao.findById", fetchType = FetchType.EAGER))
    })
    List<Account> findAll();

③:一对多(多对多):

注解配置:

    /**
     * 查所有及其账户
     * @return
     */
    @Select("select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(id = false, property = "username", column = "username"),
            @Result(id = false, property = "address", column = "address"),
            @Result(id = false, property = "sex", column = "sex"),
            @Result(id = false, property = "birthday", column = "birthday"),
            @Result(property = "accounts", column = "id", many = @Many(select = "cn.xupt.dao.IAccountDao.findByUid", fetchType = FetchType.LAZY))
    })
    List<User> findAll();

4. Mybatis二级缓存的注解使用:

①:SqlMapConfig.XML配置:

<settings>
    <setting name = "chcheEnabled", value = "true">
</settings>

②:注解配置:

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

5. 总结:

Mybatis学习完了,一开始XMl的配置方式觉得有些麻烦,但一开始就用注解的话一定很懵,其中有几段实在学不下来了,看的头疼,好在坚持了下来,收获也是挺多的。Mybatis的开发方式还是注解好用一点,唯一麻烦的地方就是<Results>标签,也就是<ResultMap>标签,不过相对XML配置要方便了许多许多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值