注解 | 作用 |
---|---|
@Insert | 实现新增 |
@Delete | 实现删除 |
@Update | 实现更新 |
@Select | 实现查询 |
@Result | 实现结果集封装 |
@Results | 可以与@Result 一起使用,封装多个结果集 |
@ResultMap | 实现引用@Results 定义的封装 |
@One | 实现一对一结果集封装 |
@Many | 实现一对多结果集封装 |
@SelectProvider | 实现动态 SQL 映射 |
@CacheNamespace | 实现注解二级缓存的使用 |
注意:
- 注解的首写字母是大写,因为注解和类、接口是同一级别的,注解后面是没有分号的;
- 在注解与语法单元之间可以隔若干空行、注释等非代码内容;
- 注解和 Mapper.xml 文件不能同时存在。
public interface UserMapper {
//查询所有用户
@Select("select * from user")
List<User> queryUserAll();
//根据 id 查询单个用户
@Select("select * from user where id = #{id}")
User queryUserById(int i);
//根据 username 模糊查询
@Select("select * from user where username like concat('%',#{username},'%')")
List<User> queryUserByName(String username);
//添加用户
@Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
void insertUser(User user);
//更新用户
@Update("update user set username = #{username}, birthday = #{birthday}, sex = #{sex}, address = #{address} where id = #{id}")
void updateUser(User user);
//删除用户
@Delete("delete from user where id = #{id}")
void deleteUserById(Integer id);
}
- 分析:
当实体类属性名与数据库列名不一致,又想成功封装数据,使用注解开发,就需要用results
注解来解决名称不一致的问题;
使用方法与在 mapper.xml 中 resultMap 标签的写法大致类似
,案例如下:
//查询所有用户
@Select("select * from user")
@Results( id = "UserMap",value = {
@Result(id = true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "birthday",property = "userBirthday"),
@Result(column = "sex",property = "userSex"),
@Result(column = "address",property = "userAddress")
})
List<User> queryUserAll();
//根据 id 查询单个用户
@Select("select * from user where id = #{id}")
@ResultMap(value = {"userMap"})
User queryUserById(Integer userId);
@Results
注解用于封装多个结果集
id
=“userMap” 是唯一标识,用来方便其他方法引用该"Results"的配置)value
属性用于接收@Result
注解类型的数组。
.@Result
注解用于修饰返回的结果集,关联实体类属性和数据库字段一一对应
id
表示是否是主键,默认false
,如果是主键,可以添加上"id=true"
。column
属性指定数据库表中对应的列,property
属性指定实体类中对应的属性名,
.@ResultMap
注解用于引用@Results
定义的映射结果集,避免了重复定义映射结果集。
- 分析用户表和账户表的关系:
1. 一个用户可以有一个账户,也可以有多个账户(一对多);
2. 一个账户只能属于一个用户(一对一);
public interface AccountMapper {
//查询所有账户信息,并查询所属用户
@Select("select * from account")
@Results(id = "accountMap", value = {
@Result(property = "user", column = "uid",
one = @One(select = "com.hn.dao.UserMapper.queryUserById", fetchType = FetchType.EAGER))
})
List<Account> queryAccountAll();
}
public interface UserMapper {
//根据 id 查询单个用户
@Select("select * from user where id = #{id}")
User queryUserById(Integer userId);
@One
注解相当于标签,实现一对一的结果集封装。
select
属性指定用于 statement 的 id。fetchType
属性指定用于指定立即加载(FetchType.EAGER
)或延迟加载(FetchType.LAZY
)。在包含
@one
注解的@Result
中,column
属性用于指定作为参数进行关联查询的数据库列名。
public interface UserMapper {
//查询所有用户,使用延迟加载,如果需要账户信息,再执行查询账户的信息
@Select("select * from user")
@Results(id = "userMap", value = {
@Result(id = true, column = "id", property = "userId"),
@Result(column = "username", property = "userName"),
@Result(column = "birthday", property = "userBirthday"),
@Result(column = "sex", property = "userSex"),
@Result(column = "address", property = "userAddress"),
@Result(property = "accounts", column = "id",
many = @Many(select = "com.hn.dao.AccountMapper.queryAccountByUid", fetchType = FetchType.LAZY))
})
List<User> queryUserAll();
//根据用户 id 查询 账户信息
@Select("select * from account where uid = #{id}")
List<Account> queryAccountByUid();
@Many
注解相当于标签collection
,实现一对多结果集封装
select
属性指定用于 statement 的 id。fetchType
属性指定用于指定立即加载(FetchType.EAGER
)或延迟加载(FetchType.LAZY
)。- 在包含
@many
注解的@Result
中,column
属性用于指定作为参数进行关联查询的数据库列名。