MyBatis-Plus Helper ??? MyBatis-Plus ????,???????????,??? Mapper??? Service??????????,????????? MyBatis-Plus Helper ???????
- ????
??,????????? MyBatis-Plus Helper ????? Maven ??? pom.xml ?????????:
com.baomidou mybatis-plus-extension 3.x.x
?? 3.x.x
??????????
- ?????
???????,??? JSR-303(Java ????)???????????:
import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; public class User { private Long id; @NotBlank(message = "???????") @Size(min = 4, max = 20, message = "????????4?20?????") private String username; // ????? getter/setter ?? }
- ?? Mapper ??
??????? BaseMapper
? Mapper ??:
import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper{ }
- ?? Service ??????
???? Service ??,??? IService
:
import com.baomidou.mybatisplus.extension.service.IService; public interface UserService extends IService{ }
?????? UserService
????:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @Service public class UserServiceImpl extends ServiceImplimplements UserService { }
- ? Controller ??? Service
? Controller ?,???? Service ?????????????,??????:
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/create") public ResponseEntitycreateUser(@Valid @RequestBody User user) { boolean result = userService.save(user); if (result) { return ResponseEntity.ok("??????"); } else { return ResponseEntity.badRequest().body("??????"); } } }
??????,???????? POST ??? /user/create
?,@Valid
??????????????????,???????????????????????,??? userService.save()
?????????
??,?????? MyBatis-Plus Helper ???????????? MyBatis-Plus Helper ???,???????:https://mybatis.plus/guide/wrapper.html#数据校验