Search in sources :

Example 1 with BrowseHistory

use of com.cas.sim.tis.entity.BrowseHistory in project TeachingInSimulation by ScOrPiOzzy.

the class ResourceServiceImpl method browsed.

@Override
public void browsed(Integer id, Integer userId) {
    // 1.获取事务控制管理器
    DataSourceTransactionManager transactionManager = SpringUtil.getBean(DataSourceTransactionManager.class);
    // 2.获取事务定义
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    // 3.设置事务隔离级别,开启新事务
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    // 4.获得事务状态
    TransactionStatus status = transactionManager.getTransaction(def);
    try {
        // 增加资源查看数量
        ResourceMapper resourceMapper = (ResourceMapper) mapper;
        resourceMapper.increaseBrowse(id);
        Condition condition = new Condition(BrowseHistory.class);
        condition.orderBy("createDate").asc();
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("creator", userId);
        List<BrowseHistory> histories = browseHistoryService.findByCondition(condition);
        // 查看历史记录是否超过100条,注意这里是物理删除!!!
        if (histories.size() >= 100) {
            BrowseHistory history = histories.get(0);
            browseHistoryService.deleteById(history.getId());
        }
        BrowseHistory history = new BrowseHistory();
        history.setResourceId(id);
        history.setCreator(userId);
        browseHistoryService.save(history);
        transactionManager.commit(status);
    } catch (Exception e) {
        e.printStackTrace();
        transactionManager.rollback(status);
    }
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) BrowseHistory(com.cas.sim.tis.entity.BrowseHistory) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Criteria(tk.mybatis.mapper.entity.Example.Criteria) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Aggregations

BrowseHistory (com.cas.sim.tis.entity.BrowseHistory)1 ResourceMapper (com.cas.sim.tis.mapper.ResourceMapper)1 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)1 Condition (tk.mybatis.mapper.entity.Condition)1 Criteria (tk.mybatis.mapper.entity.Example.Criteria)1