# base-service
**Repository Path**: junjunyuanyuankeke/base-service
## Basic Information
- **Project Name**: base-service
- **Description**: No description available
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2019-06-27
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
= Base Service
www.jgayb.com;
:toc: left
:toc-title: 章节
:doctype: book
:icons: font
:source-highlighter: highlightjs
== 简介
Mybatis、通用mapper、Jpa的Base Service,实现了基本的CRUD。
== 1. 依赖
=== 1.1 *Gradle工程*
[source, groovy]
----
repositories {
...
maven { url "https://raw.githubusercontent.com/jgaybjone/mvnrepo/master" }
mavenCentral()
}
dependencies {
//下面根据情况三选一
compile 'cn.jgayb:mybatis-service:1.0-RELEASE' //mybatis项目使用
compile 'cn.jgayb:tk-mybatis-service:1.0-RELEASE' //通用mapper项目使用
compile 'cn.jgayb:jpa-service:1.0-RELEASE' //spring data jpa项目使用
}
----
=== 1.2 *Maven工程*
[source, xml]
----
jgayb
jgayb Repository
https://raw.githubusercontent.com/jgaybjone/mvnrepo/master
cn.jgayb
mybatis-service
1.0-RELEASE
cn.jgayb
tk-mybatis-service
1.0-RELEASE
cn.jgayb
jpa-service
1.0-RELEASE
----
== 2. Mybatis BaseService使用介绍
=== 2.1 *自己的Mapper继承IMapper接口*
[source, java]
----
public interface UserMapper extends IMapper {
User selectById(String id);
void update(User user);
}
----
=== 2.2 *自己的Service继承BaseService*
[source,java]
----
public interface UserService extends BaseService {
}
----
=== 2.3 *实现类*
[source,java]
----
@Service
public class UserServiceImpl implements UserService{
@Autowird
private UserMapper userMapper;
@Override
public UserMapper getMapper(){
return this.userMapper;
}
@Override
@Transactional
public void crudAndConsumer(Consumer consumer){
consumer.accept(userMapper);
}
...
}
----
=== 2.4 *方法调用例子*
[source,java]
----
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
[source,java]
----
public interface UserMapper extends BaseMapper {
/**
* 通用mapper已经很有很多方法了
*/
UserDto selectById(String id);
}
----
=== 3.2 *自己的Service继承BaseService*
[source,java]
----
public interface UserService extends BaseService {
}
----
=== 3.3 *实现类*
[source,java]
----
@Service
public class UserServiceImpl implements UserService{
@Autowird
private UserMapper userMapper;
@Override
public UserMapper getMapper(){
return this.userMapper;
}
@Override
@Transactional
public void crudAndConsumer(Consumer consumer){
consumer.accept(userMapper);
}
...
}
----
=== 3.4 *方法调用例子*
[source,java]
----
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*
[source,java]
----
public interface UserService extends BaseService {
}
----
=== 4.2 *Service的实现*
[source,java]
----
@Service
public class UserServiceImpl implements UserService{
@Autowird
private UserRepository userRepository;
@Override
public UserRepository getRepository(){
return this.userRepository;
}
@Override
@Transactional
public void crudAndConsumer(Consumer consumer){
consumer.accept(userRepository);
}
...
}
----
=== 4.3 *方法调用例子*
[source,java]
----
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);
});
}
}
----