Coder Social home page Coder Social logo

base-service's Introduction

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);
	    });
	}
}

base-service's People

Contributors

wailouci avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

yuanhan1993

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.