Spring Boot demo系列(二):Spring Web+MyBatis Plus

1 概述

Spring Web+MyBatis Plus,包括常见的CRUD功能。

2 dao

MyBatis Plus相比起MyBaits可以简化不少配置,对于普通的CRUD提供了两个接口实现:

  • BaseMapper<T>
  • ISerivce<T>

最简单的BaseMapper<T>CRUD接口如下:

  • insert(T eneity):插入,返回int
  • deleteById(Serializable id):删除,返回int
  • updateById(T entity):更新,返回int
  • selectById(Serializable id):查询,返回T

上面是根据主键进行操作的方法,还有是根据Wrapper进行操作的,其他接口请查看官网。

其中最简单的IService<T>CRUD接口如下:

  • save(T entity):插入,返回布尔
  • saveOrUpdate(T entity):插入或更新,返回布尔
  • removeById(Serializable id):删除,返回布尔
  • updateById(Serializable id):更新,返回布尔
  • getById(Serializable id):查询,返回T
  • list():查询所有,返回List<T>

同样道理也可以根据Wrapper操作,下面演示分别演示这两种实现方式的Demo

2.1 BaseMapper<T>

BaseMapper<T>的实现方式比IService<T>要相对简单一点,首先需要一个继承了BaseMapper<T>的接口,其中T一般是实体类:

public interface UserMapper extends BaseMapper<User> {
}

接着在业务层中直接注入并使用:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusMapper {
    private final UserMapper mapper;

    public boolean save(User user) {
        if (mapper.selectById(user.getId()) != null) {
            return mapper.updateById(user) == 1;
        }
        return mapper.insert(user) == 1;
    }

    public boolean delete(Long id) {
        return mapper.deleteById(id) == 1;
    }

    public User select(Long id) {
        return mapper.selectById(id);
    }

    public List<User> selectAll() {
        return mapper.selectList(null);
    }
}

由于insert/updateById/deleteById都是返回int,表示SQL语句操作影响的行数,因为都是对单个实体进行操作,所以将返回值与1判断就可以知道是否操作成功。

2.2 IService<T>

同样需要先创建一个接口并继承IService<T>

public interface UserService extends IService<User> {
}

接着业务类继承ServiceImpl<UserMapper,User>并实现UserService,这个UserMapper是上面的UserMapper

@Service
public class MyBatisPlusIService extends ServiceImpl<UserMapper, User> implements UserService {
    public boolean save(User user) {
        if (getById(user.getId()) != null) {
            return updateById(user);
        }
        return super.save(user);
    }

    public boolean delete(Long id) {
        return removeById(id);
    }

    public User select(Long id) {
        return getById(id);
    }

    public List<User> selectAll() {
        return list();
    }
}

由于remove/saveOrUpdate都是返回布尔值,就不需要像BaseMapper一样将返回值与1判断了。

3 Controller

两个Controller,分别使用IService<T>以及BaseMapper<T>

@RestController
@RequestMapping("/mapper/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusMapperController {
    private final MyBatisPlusMapper myBatisPlusMapper;

    @GetMapping("select/id")
    public User select1(@RequestParam Long id) {
        return myBatisPlusMapper.select(id);
    }

    @GetMapping("select/id/{id}")
    public User select2(@PathVariable("id") Long id) {
        return myBatisPlusMapper.select(id);
    }

    @GetMapping("select/all")
    public List<User> selectAll() {
        return myBatisPlusMapper.selectAll();
    }

    @GetMapping("delete")
    public boolean delete1(@RequestParam Long id) {
        return myBatisPlusMapper.delete(id);
    }

    @GetMapping("delete/{id}")
    public boolean delete2(@PathVariable("id") Long id) {
        return myBatisPlusMapper.delete(id);
    }

    @PostMapping("save")
    public boolean save(@RequestBody User user) {
        return myBatisPlusMapper.save(user);
    }
}
@RestController
@RequestMapping("/iservice/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusIServiceController {
    private final MyBatisPlusIService myBatisPlusIService;

    @GetMapping("select/id")
    public User select1(@RequestParam Long id) {
        return myBatisPlusIService.select(id);
    }

    @GetMapping("select/id/{id}")
    public User select2(@PathVariable("id") Long id) {
        return myBatisPlusIService.select(id);
    }

    @GetMapping("select/all")
    public List<User> selectAll() {
        return myBatisPlusIService.selectAll();
    }

    @GetMapping("delete")
    public boolean delete1(@RequestParam Long id) {
        return myBatisPlusIService.delete(id);
    }

    @GetMapping("delete/{id}")
    public boolean delete2(@PathVariable("id") Long id) {
        return myBatisPlusIService.delete(id);
    }

    @PostMapping("save")
    public boolean save(@RequestBody User user) {
        return myBatisPlusIService.save(user);
    }
}

4 其他

4.1 实体类

@Data
@AllArgsConstructor
public class User {
    private Long id;
    private String username;
    private String password;
}

4.2 配置类

配置类主要就是加一个@MapperScan

@Configuration
@MapperScan("com.example.demo.dao")
public class MyBatisPlusConfig {
}

4.3 配置文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印SQL,如果不需要可以注释掉这个配置

按需要修改即可。

4.4 数据库

SQL文件在源码链接中。

5 测试

测试就直接运行test目录下的文件即可,笔者简单做了两个测试,上个图:

在这里插入图片描述

在这里插入图片描述

6 源码

Java版:

Kotlin版:

posted @ 2020-09-07 21:50  氷泠  阅读(554)  评论(0编辑  收藏  举报