Question: MyBatis vs. MyBatis-Plus Implementation问题:MyBatis与MyBatis-Plus实施
I have two implementations of an article management feature using MyBatis and MyBatis-Plus. Below are the respective code snippets for each approach.
- Code written using MyBatis1.
@RestController
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleService articleService;
@PostMapping
public Result add(@RequestBody @Validated Article article) {
articleService.add(article);
return Result.success();
}
}
public interface ArticleService {
void add(Article article);
}
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
@Autowired
private ArticleMapper articleMapper;
@Override
public void add(Article article) {
article.setCreateTime(LocalDateTime.now());
article.setUpdateTime(LocalDateTime.now());
Map<String, Object> map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
article.setCreateUser(userId);
articleMapper.add(article);
}
}
@Mapper
public interface ArticleMapper {
@Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time) " +
"values(#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
void add(Article article);
}
- Code written using MyBatis-Plus2.
@RestController
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleService articleService;
@PostMapping
public Result add(@RequestBody @Validated Article article) {
article.setCreateTime(LocalDateTime.now());
article.setUpdateTime(LocalDateTime.now());
Map<String, Object> map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
article.setCreateUser(userId);
articleService.save(article);
return Result.success();
}
}
public interface ArticleService extends IService<Article> {
}
@Service
`your text`public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
}
@Mapper
`your text`public interface ArticleMapper extends BaseMapper<Article> {
}
Question
Which implementation is better in terms of code maintainability, performance, and ease of use? Are there any best practices or suggestions for improving the use of MyBatis or MyBatis-Plus in this context?
New contributor
user24206347 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.