Mybatis CRUD注解Annotation的使用

详见:https://blog.csdn.net/a772304419/article/details/80791007

Mybatis框架下的CRUD操作,除了常用的Mapper.xml的xml方式外,还可以使用注解的方式。

不需要再写Dao层每个类对应的xml文件,直接在Dao层的方法上添加以下注解:

1. @Select,@Insert,@Update,@Delete完成常见的增删改查sql语句的映射。

2. @Options(useGeneratedKeys=true,keyProperty=”id”)

有时与@Insert注解一起使用,属useGeneratedKeys=true表示使用数据库自动增长的主键,该操作需要底层数据库的支持。keyProperty="id"表示将插入数据生成的主键设置到 user对象的id当中。

3. 

@Results(
   @Result(id=true,column=”id”,property=”id”),
   @Result(column=”name”,property=” name”),
   @Result(column=”sex”,property=” sex”),
   @Result(column=”age”,property=” age”),
   //关联查询@One 和 @Many
)

4. @param注解

import org.apache.ibatis.annotations.Param;
dao层     xxMapper.java

1.采用@Param的方法可有多个参数

public void abc(@Param("userName") String name,@Param("password") String passWord);

而xxMapper.xml中的#{}中的参数则是根据@Param括号中的参数来获取相应的数据

<select>
       select * from testTable where username = #{userName}
</select>

2.采用@Param修饰Javabean对象

public void abc (@Param("t") TestTable testTable);

xml中采用对象点属性方式获取数据

<select>
       select * from testTable where username = #{t.userName}
</select>

3.不采用@Param修饰参数

public void abc (TestTable testTable);

xml中可以直接写Javabean中的属性来获取参数

<select>
       select * from testTable where username = #{userName}
</select>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值