CRUD注解:@Select,@Insert,@Update,@Delete
。
配置项注解:@Options
。
参数绑定注解:@Param
, 将传入方法的参数绑定到 SQL 语句的参数上。
**结果映射注解: **@Results
—— 结果映射列表;@Result
—— 在列和属性或字段之间的单独结果映射。
CRUD注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.vending.test; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; public interface UserMapper {
@Insert("insert into user(name,sex,age) values(#{name},#{sex},#{age})") @Options(useGeneratedKeys=true,keyProperty="id") int saveUser(User user);
@Delete("Delete from user where user_id = #{userId}") int deleteById(@Param("userId") Integer id);
@Update("update user set name=#{name},sex=#{sex},age=#{age} where id = #{id}") void updateById(User user);
@Select(value = "select * from user where user_id = #{userId}") @Results({ @Result(id = true, property = "id", column = "user_id"), @Result(property = "username", column = "username"), @Result(property = "password", column = "password"), @Result(property = "home_address", column = "homeAddress")}) User queryById(@Param("userId") Integer id);
@Select("select * from user") List<User> queryAll(); }
|
关系映射注解
- 一对一注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public interface UserMapper{
@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
|
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); }
|