首页 > 其他分享 >Mybatis学习笔记(六)——Mybatis注解

Mybatis学习笔记(六)——Mybatis注解

时间:2022-09-05 22:48:19浏览次数:66  
标签:name 映射 column 笔记 Result Mybatis 注解 property id

注解主要分为三大类,即 SQL 语句映射、结果集映射和关系映射。下面分别进行讲解。不过一般注解用的比较少。

1.SQL语句映射

1)@Insert:实现新增功能

 这个方法是在mapper接口里。
@Insert("insert into user(id,name) values(#{id},#{name})")
public int insert(User user);

2)@Select:实现查询功能

@Select("Select * from user")
@Results({
    @Result(id = true, column = "id", property = "id"),
    @Result(column = "name", property = "name"),
    @Result(column = "sex", property = "sex"),
    @Result(column = "age", property = "age")
})
List<User> queryAllUser();

3)@SelectKey:插入后,获取id的值

以 MySQL 为例,MySQL 在插入一条数据后,使用 select last_insert_id() 可以获取到自增 id 的值。

@Insert("insert into user(id,name) values(#{id},#{name})")
@SelectKey(statement = "select last_insert_id()", keyProperty = "id", keyColumn = "id", resultType = int,before = false)
public int insert(User user);

@SelectKey 各个属性含义如下。

  • statement:表示要运行的 SQL 语句;
  • keyProperty:可选项,表示将查询结果赋值给代码中的哪个对象;
  • keyColumn:可选项,表示将查询结果赋值给数据表中的哪一列;
  • resultType:指定 SQL 语句的返回值;
  • before:默认值为 true,在执行插入语句之前,执行 select last_insert_id()。值为 flase,则在执行插入语句之后,执行 select last_insert_id()。

4)@Insert:实现插入功能

@Insert("insert into user(name,sex,age) values(#{name},#{sex},#{age}")
int saveUser(User user);

5)@Update:实现更新功能

  • @Update("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
  • void updateUserById(User user);

6)@Delete:实现删除功能

  • @Delete("delete from user where id =#{id}")
  • void deleteById(Integer id);

7)@Param:映射多个参数

@Param 用于在 Mapper 接口中映射多个参数。

  • int saveUser(@Param(value="user") User user,@Param("name") String name,@Param("age") Int age);

@Param 中的 value 属性可省略,用于指定参数的别名。

2. 结果集映射

 @Result、@Results、@ResultMap 是结果集映射的三大注解。
声明结果集映射关系代码:
@Select({"select id, name, class_id from student"})
@Results(id="studentMap", value={
    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
    @Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();

下面为 @Results 各个属性的含义。

  • id:表示当前结果集声明的唯一标识;
  • value:表示结果集映射关系;
  • @Result:代表一个字段的映射关系。其中,column 指定数据库字段的名称,property 指定实体类属性的名称,jdbcType 数据库字段类型,id 为 true 表示主键,默认 false。

 

可使用 @ResultMap 来引用映射结果集,其中 value 可省略。

@Select({"select id, name, class_id from student where id = #{id}"})
@ResultMap(value="studentMap")
Student selectById(Integer id);

这样不需要每次声明结果集映射时都复制冗余代码,简化开发,提高了代码的复用性。

3. 关系映射

1)@one:用于一对一关系映射

@Select("select * from student") 
@Results({ 
    @Result(id=true,property="id",column="id"), 
    @Result(property="name",column="name"), 
    @Result(property="age",column="age"), 
    @Result(property="address",column="address_id",one=@One(select="net.biancheng.mapper.AddressMapper.getAddress")) 
}) 
public List<Student> getAllStudents();  

2)@many:用于一对多关系映射

@Select("select * from t_class where id=#{id}") 
@Results({ 
    @Result(id=true,column="id",property="id"), 
    @Result(column="class_name",property="className"), 
    @Result(property="students", column="id", many=@Many(select="net.biancheng.mapper.StudentMapper.getStudentsByClassId")) 
    }) 
public Class getClass(int id); 

 

标签:name,映射,column,笔记,Result,Mybatis,注解,property,id
From: https://www.cnblogs.com/worthmove/p/16659859.html

相关文章

  • mybatis 动态排序
    publicclassPagination{//当前页privateIntegerpage=1;//一页显示条数privateIntegerlimit=10;//排序字段privat......
  • 操作系统(学习笔记)
    操作系统(学习笔记)  进程=正在进行的程序=执行中的程序进程程序的本质:数据和对数据的处理;进程的本质:正在运行(执行/动态)的程序;进程是操作系统进行资源化分配......
  • mybatis资源扫描 插件
    <!--在build中配置resources,来防止我们资源导出失败的问题--><build><resources><resource><directory>src/main/resources</directory>......
  • 220905-读书笔记-把时间当做朋友
    印象中,第一次读这本书的时候,是在大学期间,应该是大一或者大二的时候,当时候自己的智识达不到理解这本书的层次。现在在读这本书,竟觉得略有些浅显。总的来说,这本书写的比较零......
  • Tensorflow2.0-随笔笔记-基础
    创建张量tf.convert_to_tensor函数In[22]:tf.convert_to_tensor([1,2.])#从列表创建张量Out[22]:<tf.Tensor:id=86,shape=(2,),dtype=float32,numpy=array([1......
  • MyBatis 八——修改全部字段/修改动态字段
    配置文件完成修改全部字段1、编写接口方法:Mapper接口观察参数返回结果   2、编写SQL语句:SQL映射文件;  ......
  • 字符串入门学习笔记
    字符串哈希idea将字符串映射成一个数值(称为哈希值),因此可以在O(1)时间内做到例如判断两个串是否相等这样的事情,优化了时间复杂度注意,哈希值不同时字符串一定不同;哈希值相......
  • 蓝途随堂笔记
    java中的结构-顺序结构:从上往下依次执行的叫做顺序结构选择结构:分支结构,有相关的判断和选择if:-if~else-if~elseif~elseif...else-switch~case........*循......
  • 【图像处理笔记】图像分割基础知识
    形态学处理相同,图像分割操作的输入是图像,输出是从图像中提取出来的属性。本章的大多数分割算法都基于图像灰度值的两个基本性质之一:不连续性和相似性。第一类方法根据灰度......
  • @Component注解和@Bean注解的作用,以及两者的区别
    程序猿的我们,开发中少不了使用Spring框架,虽然天天接触它,但有时就像一句话:世界上最远的距离仿佛是头到脚~~~那你是否有知道它的内部原理呢?下面跟大家分享一下@Compoent和@Be......