1. 程式人生 > 實用技巧 >Spring Boot demo系列(三):Spring Web+MyBatis Plus

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

1 概述

Spring Web+MyBatis Plus的一個Demo,內容和上一篇類似,因此重點放在MyBatis Plus這裡。

2 dao

MyBatis Plus相比起MyBaits可以簡化不少配置,能夠使用程式碼生成器快速生成EntityMapper等各個模組的程式碼。另外,對於普通的CRUD提供了兩個介面實現:

  • ISerivce<T>
  • BaseMapper<T>

其中最簡單的IService<T>CRUD介面如下:

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

上面是根據主鍵進行操作的方法,還有是根據Wrapper進行操作的,具體請檢視官方文件。

最簡單的BaseMapper<T>CRUD介面如下:

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

同樣道理也可以根據Wrapper操作,具體請檢視官網,下面演示分別演示這兩種實現方式的Demo

2.1 BaseMapper<T>

BaseMapper<T>的實現方式比IService<T>要相對簡單一點,首先需要一個繼承了BaseMapper<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("/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);
    }
}
@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);
    }
}

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+"\nusername:"+username+"\npassword:"+password+"\n";
    }
}

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版: