Skip to content

Latest commit

 

History

History
256 lines (199 loc) · 4.68 KB

README.adoc

File metadata and controls

256 lines (199 loc) · 4.68 KB

Base Service

简介

Mybatis、通用mapper、Jpa的Base Service,实现了基本的CRUD。

1. 依赖

1.1 Gradle工程

repositories {

    ...
    maven { url "https://raw.githubusercontent.com/jgaybjone/mvnrepo/master" }
    mavenCentral()
}

dependencies {
	//下面根据情况三选一
	compile 'cn.jgayb:mybatis-service:1.3-RELEASE' //mybatis项目使用
	compile 'cn.jgayb:tk-mybatis-service:1.4-RELEASE' //通用mapper项目使用
	compile 'cn.jgayb:jpa-service:1.3-RELEASE' //spring data jpa项目使用
}

1.2 Maven工程

  <repositories>
    <repository>
      <id>jgayb</id>
      <name>jgayb Repository</name>
      <url>https://raw.githubusercontent.com/jgaybjone/mvnrepo/master</url>
    </repository>
  </repositories>
  <dependencies>
  	<!--下面根据情况三选一 -->
  	<dependency>
    	<groupId>cn.jgayb</groupId>
		<artifactId>mybatis-service</artifactId>
		<version>1.3-RELEASE</version>
  	</dependency>
  	<dependency>
    	<groupId>cn.jgayb</groupId>
		<artifactId>tk-mybatis-service</artifactId>
		<version>1.4-RELEASE</version>
  	</dependency>
  	<dependency>
    	<groupId>cn.jgayb</groupId>
		<artifactId>jpa-service</artifactId>
		<version>1.3-RELEASE</version>
  	</dependency>
  </dependencies>

2. Mybatis BaseService使用介绍

2.1 自己的Mapper继承IMapper接口

public interface UserMapper extends IMapper<User> {

	User selectById(String id);

	void update(User user);

}

2.2 自己的Service继承BaseService

public interface UserService extends BaseService<User, UserMapper> {

}

2.3 实现类

@Service
public class UserServiceImpl implements UserService{

    @Autowird
    private UserMapper userMapper;

    @Override
    public UserMapper getMapper(){
        return this.userMapper;
    }

    @Override
    @Transactional
    public void crudAndConsumer(Consumer<UserMapper> consumer){
        consumer.accept(userMapper);
    }

    ...

}

2.4 方法调用例子

public class example{

    @Autowird
    private UserService userService;

    //Lambda 函数式优雅使用
	public void findAndDeletedById(String id){
	    userService.crudAndConsumer(userMapper -> {
	        User user = userMapper.selectById(id);
	        user.setDisable(true);
	        userMapper.updateBySelective(user);
	    });
	}
}

3. 通用mapper BaseService使用介绍

3.1 所有的mapper都继承BaseMapper

public interface UserMapper extends BaseMapper<User> {

	/**
	*  通用mapper已经很有很多方法了
	*/
	UserDto selectById(String id);

}

3.2 自己的Service继承BaseService

public interface UserService extends BaseService<User, UserMapper> {

}

3.3 实现类

@Service
public class UserServiceImpl implements UserService{

    @Autowird
    private UserMapper userMapper;

    @Override
    public UserMapper getMapper(){
        return this.userMapper;
    }

    @Override
    @Transactional
    public void crudAndConsumer(Consumer<UserMapper> consumer){
        consumer.accept(userMapper);
    }

    ...

}

3.4 方法调用例子

public class example{

    @Autowird
    private UserService userService;

    //Lambda 函数式优雅使用
	public void findAndDeletedById(String id){
	    userService.crudAndConsumer(userMapper -> {
	        UserDto userDto = userMapper.selectById(id);
	        userDto.setDisable(true);
	        userMapper.updateBySelective(userDto);
	    });
	}
}

4. Spring data jpa BaseService介绍

4.1 自己的Service继承BaseService

public interface UserService extends BaseService<User, String, UserRepository> {

}

4.2 Service的实现

@Service
public class UserServiceImpl implements UserService{

    @Autowird
    private UserRepository userRepository;

    @Override
    public UserRepository getRepository(){
        return this.userRepository;
    }

    @Override
    @Transactional
    public void crudAndConsumer(Consumer<UserRepository> consumer){
        consumer.accept(userRepository);
    }

    ...

}

4.3 方法调用例子

public class example{

    @Autowird
    private UserService userService;

    //Lambda 函数式优雅使用
	public void findAndDeletedById(String id){
	    userService.crudAndConsumer(userRepository -> {
	        User user = userRepository.findById(id);
	        //CRUD
	        ...

	        userRepository.save(user);
	    });
	}
}