1. 程式人生 > >springboot-mybatits資料庫,ecache快取操作,批量操作,事物操作

springboot-mybatits資料庫,ecache快取操作,批量操作,事物操作

springboot-mybatits資料庫,ecache快取操作,批量操作,事物操作

文章目錄


程式碼下載: https://github.com/2010yhh/springBoot-demos.git
環境

idea2018,jdk1.8,

springboot版本:1.5.9.RELEASE

1.mybatits,資料庫ecache快取操作

ecache配置:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache">
	<diskStore path="java.io.tmpdir"/>
	<defaultCache
			maxElementsInMemory="10000"
			eternal="false"
			timeToIdleSeconds="120"
			timeToLiveSeconds="120"
			overflowToDisk="false"
			diskPersistent="false"
			diskExpiryThreadIntervalSeconds="120"
	/>
	<!-- 表示此快取最多可以存活2分鐘,如果期間超過1分鐘未訪問 那麼此快取失效-->
	<cache name="user"
		   maxEntriesLocalHeap="2000"
		   eternal="false"
		   timeToIdleSeconds="60"
		   timeToLiveSeconds="120"
		   overflowToDisk="false"
		   statistics="true">
	</cache>
</ehcache>

使用:

@Service
@CacheConfig(cacheNames = {"user"})
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Cacheable(key = "#userName")
    @Override
    public List<User> findByUserName(String userName, String passWord) {
        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(userName).andPassWordEqualTo(passWord);
        return userMapper.selectByExample(example);
    }

    @CachePut(key = "#user.userId")
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addUser(User user) {
        //測試事物特性
        userMapper.insertSelective(user);
         //userMapper.insertSelective(user);
    }

}

測試時,設定快取30s,(timeToIdleSeconds=“30”
timeToLiveSeconds=“30”)在快取有效期內查詢:從日誌可看出,只查詢資料庫一次。

在這裡插入圖片描述

2.mybatits,資料庫批量操作:增刪查改

配置:mapper的sql語句

  <!-- 批量增刪查改 -->
    <insert id="batchAddUser" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="userId">
        insert into sys_user (user_id, user_name, real_name,
        pass_word, telephone, email,
        photo, create_time, update_time,
        status)
        values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.userId,jdbcType=INTEGER}, #{item.userName,jdbcType=VARCHAR}, #{item.realName,jdbcType=VARCHAR},
            #{item.passWord,jdbcType=VARCHAR}, #{item.telephone,jdbcType=VARCHAR}, #{item.email,jdbcType=VARCHAR},
            #{item.photo,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.status,jdbcType=INTEGER})
        </foreach>
    </insert>
    <!-- 批量刪除-->
    <delete id="batchDeleteUser" parameterType="java.util.List">
        delete from sys_user where user_id in
        <foreach collection="list" index="index" item="item" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!--批量查詢 -->
    <select id="batchFindUser" resultMap="BaseResultMap" parameterType="java.util.List">
        select
        <include refid="Base_Column_List"/>
        from sys_user
        where user_name in
        <foreach collection="list" item="index" separator="," open="("
                 close=")">
            #{index}
        </foreach>
    </select>
    <!--批量更新1 -->
    <update id="batchUpdate" parameterType="java.util.List">

        <foreach collection="list" separator=";" item="item" open="" close="">
            update sys_user
            <set>
                <if test="#{item.userName} != null">
                    user_name = #{item.userName},
                </if>
                <if test="#{item.realName} != null">
                    real_name = #{item.realName},
                </if>
                <if test="#{item.passWord} != null">
                    pass_word = #{item.passWord},
                </if>
                <if test="#{item.telephone} != null">
                    telephone = #{item.telephone},
                </if>
                <if test="#{item.email} != null">
                    email = #{item.email,jdbcType=VARCHAR},
                </if>
                <if test="#{item.photo} != null">
                    photo = #{item.photo},
                </if>
                <if test="#{item.createTime}!= null">
                    create_time = #{item.createTime},
                </if>
                <if test="#{item.updateTime}!= null">
                    update_time = #{item.updateTime},
                </if>
                <if test="#{item.status} != null">
                    status = #{item.status},
                </if>
            </set>
            where user_id = #{item.userId}
        </foreach>
    </update>
    <!--批量更新2 -->
    <update id="batchUpdateUser" parameterType="java.util.List">
        update sys_user
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="user_name =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.userName!=null">
                        when user_id=#{item.userId} then #{item.userName}
                    </if>
                </foreach>
            </trim>
        <trim prefix="real_name =case" suffix="end,">
            <foreach collection="list" item="item">
                <if test="item.realName!=null">
                    when user_id=#{item.userId} then #{item.realName}
                </if>
            </foreach>
        </trim>
            <trim prefix="pass_word =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.passWord!=null">
                        when user_id=#{item.userId} then #{item.passWord}
                    </if>
                </foreach>
            </trim>
            <trim prefix="telephone=case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.telephone!=null">
                        when user_id=#{item.userId} then #{item.telephone}
                    </if>
                </foreach>
            </trim>
            <trim prefix="email =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.email!=null">
                        when user_id=#{item.userId} then #{item.email}
                    </if>
                </foreach>
            </trim>
            <trim prefix="photo =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.photo!=null">
                        when user_id=#{item.userId} then #{item.photo}
                    </if>
                </foreach>
            </trim>
            <trim prefix="create_time=case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.createTime!=null">
                        when user_id=#{item.userId} then #{item.createTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.updateTime!=null">
                        when user_id=#{item.userId} then #{item.updateTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="status=case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.status!=null">
                        when user_id=#{item.userId} then #{item.status}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="item">
             user_id= #{item.userId}
            </foreach>
        </where>
    </update>

2.1批量查詢:
在這裡插入圖片描述

2.2批量插入:
在這裡插入圖片描述
在這裡插入圖片描述
2.3批量更新:

在這裡插入圖片描述

在這裡插入圖片描述

2.4批量刪除:

在這裡插入圖片描述

在這裡插入圖片描述

3.mybatits,資料庫事物操作

 * 宣告式事務管理: 建立在AOP之上的。其本質是對方法前後進行攔截,
 * 然後在目標方法開始之前建立或者加入一個事務,在執行完目標方法之後根據執行情況提交或者回滾事務。
 * 宣告式事務管理不需要入侵程式碼,通過@Transactional就可以進行事務操作,更快捷而且簡單
 * 隔離級別
 * 隔離級別是指若干個併發的事務之間的隔離程度,與我們開發時候主要相關的場景包括:髒讀取、重複讀、幻讀
 */

/**
 * 傳播行為
 * 所謂事務的傳播行為是指,如果在開始當前事務之前,
 * 一個事務上下文已經存在,此時有若干選項可以指定一個事務性方法的執行行為
 */
 @CacheEvict(key = "#id")
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteUser(int userId) {
        //測試事物特性
        //userMapper.insertSelective(user);
        userMapper.deleteByPrimaryKey(userId);
    }

在這裡插入圖片描述