Six MyBatis annotation development
This article is the sixth section of the learning process, check this column for the whole process
column directory
Article Directory
1 Overview
Before, the sql statement was written into the xml file
Annotation is to write SQL statements into annotations
Notice:
Using annotations to map simple statements will make the code more concise, but for slightly more complex statements, Java annotations will not only fail, but will also make the already complex SQL statements even more confusing. Therefore, if you need to do some very complex operations, it is best to use XML to map statements.
How you choose to configure the mapping, and whether you should unify the mapping statement definitions, is entirely up to you and your team. In other words, never stick to one method, you can easily port and switch freely between annotation-based and XML-based statement mapping methods.
2 annotation name
- Query: @Select
- Add: @Insert
- Edit: @Update
- Delete: @Delete
3 small cases
This case uses some content from the previous notes
UserMapper
/**
* 通过id查询某个用户的所有信息
*
* @param id 需要查询的用户id
* @return 查询到的用户
*/
@Select("select * from tb_user where id=#{id}")
User selectById(int id);
test
Successfully queried