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

2021.2.24 更新

1 概述

Spring Web+MyBatis Plus的一个Demo,内容和上一篇类似,因此重点放在MyBatis Plus这里。

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一般是实体类:

@Mapper
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(String id)
    {
        return mapper.deleteById(id) == 1;
    }

    public User select(String 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
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyBatisPlusIService extends ServiceImpl<UserMapper,User> implements UserService {
    public boolean save(User user)
    {
        return saveOrUpdate(user);
    }

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

    public User select(String 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")
    public User select1(@RequestParam String id)
    {
        return myBatisPlusMapper.select(id);
    }

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

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

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

    @GetMapping("delete/{id}")
    public boolean delete2(@PathVariable("id") String 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")
    public User select1(@RequestParam String id)
    {
        return myBatisPlusIService.select(id);
    }

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

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

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

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

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

4 其他

4.1 实体类

@Getter
@Setter
@AllArgsConstructor
public class User {
    private String id;
    private String username;
    private String password;
    @Override
    public String toString()
    {
        return "id:"+id+"
username:"+username+"
password:"+password+"
";
    }
}

4.2 配置类

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

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

4.3 配置文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: test
    password: test

按需要修改即可。

4.4 数据库

SQL文件在源码链接中。

5 测试

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

在这里插入图片描述

在这里插入图片描述

6 源码

Java版:

Kotlin版:

原文地址:https://www.cnblogs.com/6b7b5fc3/p/13629468.html