Search in sources :

Example 1 with Collection

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

the class ResourceServiceImpl method collected.

@Override
public void collected(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.increaseCollection(id);
        // 新增用户收藏记录
        Collection collection = new Collection();
        collection.setResourceId(id);
        collection.setCreator(userId);
        collectionService.save(collection);
        transactionManager.commit(status);
    } catch (Exception e) {
        e.printStackTrace();
        transactionManager.rollback(status);
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Collection(com.cas.sim.tis.entity.Collection) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 2 with Collection

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

the class ResourceServiceImpl method uncollect.

@Override
public void uncollect(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.decreaseCollection(id);
        // 修改用户收藏记录
        Condition condition = new Condition(Collection.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("creator", userId);
        criteria.andEqualTo("resourceId", id);
        criteria.andEqualTo("del", 0);
        List<Collection> collections = collectionService.findByCondition(condition);
        for (Collection collection : collections) {
            collection.setDel(1);
            collectionService.update(collection);
        }
        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) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Collection(com.cas.sim.tis.entity.Collection) Criteria(tk.mybatis.mapper.entity.Example.Criteria) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Aggregations

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