MyBatis:CRUD 注解配置
CRUD注解:@Select,@Insert,@Update,@Delete
。
配置项注解:@Options
。
参数绑定注解:@Param
, 将传入方法的参数绑定到 SQL 语句的参数上。
**结果映射注解: **@Results
—— 结果映射列表;@Result
—— 在列和属性或字段之间的单独结果映射。
CRUD注解
1 | package com.vending.test; |
关系映射注解
- 一对一注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public interface UserMapper{
/**
* 一对一关联注解:需要关联执行的SQL语句
* fetchType表示查询时是立即加载(eager)还是懒加载(lazy)
* @param id
* @return
*/
@Select("select * from user where id = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="sex",property="sex"),
@Result(column="age",property="age"),
@Result(column="card_id",property="card",
one=@One(select="com.vending.mapper.CardMapper.selectCardById",
fetchType=FetchType.EAGER))})
User selectUserById(Integer id);
} - 一对多注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* 一对多关联注解
* @param id
* @return
*/
public interface ClazzMapper {
@Select("SELECT * FROM TB_CLAZZ WHERE ID = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="code",property="code"),
@Result(column="name",property="name"),
@Result(column="id",property="students",
many=@Many(
select="com.mapper.StudentMapper.selectByClazzId",
fetchType=FetchType.LAZY))
})
Clazz selectById(Integer id);
}
MyBatis:CRUD 注解配置
http://blog.gxitsky.com/2018/03/06/MyBatis-06-annotation-crud/