首页 > 其他分享 >MyBatis框架:第七章:注解使用方式和参数传递及#{}和${}

MyBatis框架:第七章:注解使用方式和参数传递及#{}和${}

时间:2022-10-08 16:00:32浏览次数:67  
标签:int id 参数传递 占位 user MyBatis 注解 sex public

myBatis的注解使用方式(了解。主要使用xml)
注解的使用示例:

public interface UserMapperAnnotation {

@Select(“select id,last_name userName ,sex from t_user where id = #{id}”)
public User selectUser(int id);

@Select(“select * from t_user”)
public List selectUserList();

@Update(“update t_user set last_name = #{lastName}, sex = #{sex} where id = #{id}”)
public int updateUser(User user);

@Delete(“delete from t_user where id = #{id}”)
public int deleteUserById(int id);

@Insert(“insert into t_user(last_name,sex) values(#{lastName},#{sex})”)
@SelectKey(before = false, keyProperty = “id”, resultType = Integer.class, statement = { “select last_insert_id()” })
public int insertUser(User user);

}

mybatis-config.xml配置文件中导入

<mappers>
	<mapper class="com.dao.UserMapperAnnotation"/>
</mappers>
batis的参数传递

1.一个普通数据类型
当一个方法中只有一个普通数据类型。在mapper配置文件中可以使用#{}占位符来进行占位输出。
#{} 占位符中,可以写参数的 #{变量名}。 也可以写 #{value}。

方法:
public int deleteUserById(int id);

#{变量名}

<delete id="deleteUserById" parameterType="int">
	delete from t_user where id = #{id}
</delete>

#{value}

<delete id="deleteUserById" parameterType="int">
	delete from t_user where id = #{value}
</delete>

2.多个普通数据类型
多个普通的参数。当我们需要使用 #{} 占位输出的时候,可以使用
param1,param2 …… paramN
也就是 #{param1} …… #{paramN}

或者使用@Param命名参数

使用param1、param2 …… paramN 占位输出参数
方法:

public List findUserByNameAndSex(String username, int sex);

使用param1、param2 …… paramN 的方式 占位输出参数

 

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120472401

 

标签:int,id,参数传递,占位,user,MyBatis,注解,sex,public
From: https://www.cnblogs.com/wangchuanxinshi/p/16769197.html

相关文章

  • springboot中mybatis-plus简单配置与使用
    依赖mybatis-plus与mybatis依赖不兼容,同时使用会导致报错<dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>${or......
  • mybatis-plus 分页操作
    mybatis-plus分页操作通过username,email,address等参数查询分页//分页查询-mybatis-plus的方式@GetMapping("/page")publicIPage<User>findPage(@R......
  • 源码角度了解Skywalking之@Trace注解的原理
    源码角度了解Skywalking之@Trace注解的原理@Trace要解决的问题是收集一些关键业务的Trace信息,使用方法就是在需要收集Trace信息的方法上添加@Trace注解就可以了。使用@Tr......
  • MyBatis的执行器
    Mybatis执行器种类Mybatis的执行器Executor分为三类简单执行器:SimpleExcutor可重用执行器:ReuseExcutor批量执行器:BatchExcutor配置:在Mybatis配置文件中配置执行器......
  • Mybatis-config.xml
    configurations:根标签,表示配置信息、environments:可以配置多个环境environment:(包括事务管理器的配置+数据源的配置)transactionManager(配置事务管理器)type属......
  • 【MyBatis】执行INSERT一条数据后返回这条数据的自增ID
    1.在MybatisMapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名。1<insertid="insert"paramete......
  • MyBatis流式查询
    转自:​​http://www.java265.com/JavaFramework/MyBatis/202206/3614.html​​下文笔者讲述Mybatis流式查询的相关简介说明,如下所示Mybatis流式查询简介流式查询简介:......
  • SpringBoot集成MyBatis(详细)
    前言JPA和MyBatis,JPA是SpringBoot官方的,前身就是著名的三大框架之一的Hibernate,好处是不用手写SQL(当然它也支持手写,如果必要的话),而MyBatis灵活性非常高,但是需要手写SQL语......
  • MyBatis框架:第八章:自定义结果集,一对一,一对多,延迟加载,赖加载
    13.1、自定义结果集介绍自定义结果集,可以给复杂的对象使用。也就是对象内又嵌套一个对象。或者一个集合。在这种情况下。前面学过的知识点,已经无法直接获取出对象内对象......
  • MyBatis框架:第九章:动态SQL语句
    准备工作:publicclassUser{privateintid;privateStringlastName;privateintsex;14.1、if语句说明:if语句,可以动态的根据你的值来决定,是否需要动态的添加查......