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):插入,返回intdeleteById(Serializable id):刪除,返回intupdateById(T entity):更新,返回intselectById(Serializable id):查詢,返回T
上面是根據主鍵進行操作的方法,還有是根據Wrapper進行操作的,其他接口請查看官網。
其中最簡單的IService<T>的CRUD接口如下:
save(T entity):插入,返回布爾saveOrUpdate(T entity):插入或更新,返回布爾removeById(Serializable id):刪除,返回布爾updateById(Serializable id):更新,返回布爾getById(Serializable id):查詢,返回Tlist():查詢所有,返回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+"\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版:
