统一的service接口基于统一的mapper,参考:Mybatis学习笔记——通用mapper

接口AbstractService<T>

package com.example.demo.core.service;

import java.util.List;

public interface AbstractService<T> {

    /**
     * 获取所有
     *
     * @return List<T>
     */
    List<T> listObjects();

    /**
     * 通过key查找
     *
     * @param key Object
     * @return T
     */
    T selectByKey(Object key);

    /**
     * 根据实体条件查找
     *
     * @param example Object
     * @return List<T>
     */
    List<T> selectByExample(Object example);

    /**
     * 持久化
     *
     * @param entity T
     * @return key
     */
    int save(T entity);

    /**
     * 通过主鍵刪除
     *
     * @param key Object
     */
    int deleteByKey(Object key);

    /**
     * 通过example条件刪除
     *
     * @param example T
     */
    int deleteByExample(T example);

    /**
     * 更新
     *
     * @param entity T
     */
    int update(T entity);

}

抽象类AbstractServiceImpl<T>

package com.example.demo.core.service.impl;

import com.example.demo.core.mapper.MyMapper;
import com.example.demo.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public abstract class AbstractServiceImpl<T> implements AbstractService<T> {

    @Autowired
    protected MyMapper<T> mapper;

    @Override
    public List<T> listObjects() {
        return mapper.selectAll();
    }

    @Override
    public T selectByKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Override
    @Transactional
    public int save(T entity) {
        return mapper.insert(entity);
    }

    @Override
    @Transactional
    public int deleteByKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    @Transactional
    public int deleteByExample(Object key) {
        return mapper.deleteByExample(key);
    }

    @Override
    @Transactional
    public int update(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

}

参考:

https://github.com/febsteam/FEBS-Security/blob/master/febs-common/src/main/java/cc/mrbird/common/service/impl/BaseService.java

以及

https://github.com/Zoctan/WYUOnlineJudge/blob/master/api/src/main/java/com/zoctan/api/core/service/AbstractService.java

之后在使用的时候,可以通过继承AbstractServiceImpl<T>的方式,来省略一些通用service方法的编写,比如

UserService接口

package com.example.demo.service;

import com.example.demo.core.service.AbstractService;
import com.example.demo.model.User;

public interface UserService extends AbstractService<User> {
}

UserServiceImpl实现类

package com.example.demo.service.impl;

import com.example.demo.core.service.impl.AbstractServiceImpl;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends AbstractServiceImpl<User> implements UserService {
}

进行测试

package com.example.demo.service.impl;

import com.example.demo.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class UserServiceImplTest {

    @Autowired
    private UserServiceImpl userService;

    @Test
    public void UserMapper() {
        List<User> list = userService.listObjects();
        for (User user: list) {
            System.out.println(user.getId());
        }
    }

}

 

分类:

技术点:

相关文章: