MyBatis:CRUD 注解配置

  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 {
/**
* 增
* @param user
* @return
*/
@Insert("insert into user(name,sex,age) values(#{name},#{sex},#{age})")
@Options(useGeneratedKeys=true,keyProperty="id") //返回自增主键
int saveUser(User user);

/**
* 删
* @param user
* @return
*/
@Delete("Delete from user where user_id = #{userId}")
int deleteById(@Param("userId") Integer id);

/**
* 改
* @param user
*/
@Update("update user set name=#{name},sex=#{sex},age=#{age} where id = #{id}")
void updateById(User user);


/**
* 查
* @param id
* @return 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);

/**
* 查-返回集合
* @return List<User>
*/
@Select("select * from user")
List<User> queryAll();
}

关系映射注解

  1. 一对一注解
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public 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);
    }

  2. 一对多注解
    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);
    }

作者

光星

发布于

2018-03-06

更新于

2022-06-17

许可协议

评论