mybatis接口方法参数传参解读

简介: mybatis接口方法参数传参解读

单个简单类型参数

简单类型包括:

  • byte short int long float double char
  • Byte Short Integer Long Float Double Character
  • String
  • java.util.Date
  • java.sql.Date
1. /**
2.      * 根据学生id进行查询
3.      * @param id
4.      * @return
5.      */
6.     Student selectById(Long id);

对应的映射xml文件

1.     <select id="selectById" resultType="com.study.pojo.Student">
2.         select * from t_student where id=#{id};
3.     </select>

简单类型对于mybatis来说都是可以自动类型识别的:

  • 也就是说对于mybatis来说,它是可以自动推断出ps.setXxxx()方法的。ps.setString()还是ps.setInt()。它可以自动推断。

如果参数只有一个的话,#{} 里面的内容就随便写了。对于 ${} 来说,注意加单引号。

Map参数

需求:根据name和age查询

1. /**
2. * 根据name和age查询
3. * @param paramMap
4. * @return
5. */
6. List<Student> selectByParamMap(Map<String,Object> paramMap);

 对应的映射xml文件

1. <select id="selectByParamMap" resultType="com.study.pojo.Student">
2.         select * from t_student where name=#{name} and age=#{age}
3. </select>

对应的java测试代码

1. @org.junit.Test
2. public void test02(){
3. SqlSession sqlSession = SqlSessionUtil.openSession();
4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
5.         Map map=new HashMap();
6.         map.put("name","张三");
7.         map.put("age","18");
8.         List<Student> student = mapper.selectByParamMap(map);
9.         System.out.println(student);
10.     }

这种方式是手动封装Map集合,将每个条件以key和value的形式存放到集合中。然后在使用的时候通过#{map集合的key}来取值。

实体类(pojo)参数

需求:插入一条Student数据

1. /**
2.      * 插入学生数据
3.      * @param student
4.      * @return
5.      */
6. int insertStudent(Student student);

对应的映射xml文件

1.     <insert id="insertStudent">
2.         insert  into t_student values (null ,#{name},#{age},#{brith})
3.     </insert>

 对应的java测试代码

1. @org.junit.Test
2. public void test03(){
3. SqlSession sqlSession = SqlSessionUtil.openSession();
4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
5. Student student = new Student();
6.         student.setAge("28");
7.         student.setBrith(new Date());
8.         student.setName("李四");
9. int i = mapper.insertStudent(student);
10.         System.out.println(i);
11.         System.out.println(student);
12.     }

这里需要注意的是:#{} 里面写的是属性名字。这个属性名其本质上是:set/get方法名去掉set/get之后的名字,也就是使用了反射进行。

多参数

需求:通过name和sex查询

1. /**
2.      * 根据name和age查询
3.      * @param name
4.      * @param age
5.      * @return
6.      */
7.     List<Student> selectByNameAndAge(String name, String age);

 对应的映射xml文件(错误示范)

1.     <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
2.         select * from t_student where name=#{name} and age=#{age}
3.     </select>

  对应的java测试代码

1. @org.junit.Test
2. public void test04(){
3. SqlSession sqlSession = SqlSessionUtil.openSession();
4. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
5.         List<Student> student = mapper.selectByNameAndAge("张三", "18");
6.         System.out.println(student);
7.     }

运行报错

异常信息描述了:name参数找不到,可用的参数包括[arg1, arg0, param1, param2]

修改StudentMapper.xml配置文件:尝试使用[arg1, arg0, param1, param2]去参数

修改xml文件

1. <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
2.         select * from t_student where name=#{param1} and age=#{param2}
3. </select>

再次运行得到结果

同时也可以将xml文件修改为:

1. <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
2.         select * from t_student where name=#{arg0} and age=#{arg1}
3. </select>

通过测试可以看到:

  • arg0 是第一个参数
  • param1是第一个参数
  • arg1 是第二个参数
  • param2是第二个参数

实现原理:实际上在mybatis底层会创建一个map集合,以arg0/param1为key,以方法上的参数为value例如以下代码:

1. Map<String,Object> map = new HashMap<>();
2. map.put("arg0", name);
3. map.put("arg1", sex);
4. map.put("param1", name);
5. map.put("param2", sex);
6. 
7. // 所以可以这样取值:#{arg0} #{arg1} #{param1} #{param2}
8. // 其本质就是#{map集合的key}
注意:使用mybatis3.4.2之前的版本时:要用#{0}和#{1}这种形式。

@Param注解(命名参数)

可以不用arg0 arg1 param1 param2吗?这个map集合的key我们自定义可以吗?当然可以。使用@Param注解即可。这样可以增强可读性。

需求:根据name和age查询

添加注解

1. /**
2.      * 根据name和age查询
3.      * @param name
4.      * @param age
5.      * @return
6.      */
7.     List<Student> selectByNameAndAge(@Param("name") String name,@Param("age") String age);

对应的xml文件

1.     <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
2.         select * from t_student where name=#{name} and age=#{age}
3.     </select>


相关文章
|
4月前
|
SQL XML Java
MyBatis Mapper中使用limit参数的查询问题
总结而言,MyBatis中使用 `limit`参数的查询可以高度定制并且灵活,基于方法签名和XML映射文件的组合来达成多样化的查询需求。通过参数化查询和动态SQL,MyBatis可以有效地处理各种复杂情境下的数据库操作,并且将SQL语句的维护与业务代码的编写相分离,提升代码的可维护性和可阅读性。
441 13
|
8月前
|
SQL XML Java
四、MyBatis获取参数值的两种方式(重点)
四、MyBatis获取参数值的两种方式(重点)
161 4
|
8月前
|
人工智能 Java 数据库连接
MyBatis Plus 使用 Service 接口进行增删改查
本文介绍了基于 MyBatis-Plus 的数据库操作流程,包括配置、实体类、Service 层及 Mapper 层的创建。通过在 `application.yml` 中配置 SQL 日志打印,确保调试便利。示例中新建了 `UserTableEntity` 实体类映射 `sys_user` 表,并构建了 `UserService` 和 `UserServiceImpl` 处理业务逻辑,同时定义了 `UserTableMapper` 进行数据交互。测试部分展示了查询、插入、删除和更新的操作方法及输出结果,帮助开发者快速上手 MyBatis-Plus 数据持久化框架。
610 0
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
|
SQL Java 数据库连接
mybatis使用四:dao接口参数与mapper 接口中SQL的对应和对应方式的总结,MyBatis的parameterType传入参数类型
这篇文章是关于MyBatis中DAO接口参数与Mapper接口中SQL的对应关系,以及如何使用parameterType传入参数类型的详细总结。
371 10
|
XML Java 数据库连接
MyBatis中的接口代理机制及其使用
【8月更文挑战第5天】MyBatis的接口代理机制是其核心功能之一,允许通过定义接口并在运行时生成代理对象来操作数据库。开发者声明一个带有`@Mapper`注解的接口,MyBatis则依据接口方法、映射配置(XML或注解)及数据库信息动态生成代理类。此机制分为四步:创建接口、配置映射文件或使用注解、最后在业务逻辑中注入并使用代理对象。这种方式简化了数据库操作,提高了代码的可读性和可维护性。例如,在电商系统中可通过`OrderMapper`处理订单数据,在社交应用中利用`MessageMapper`管理消息,实现高效且清晰的数据库交互。
217 5
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
281 11
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
|
5月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
956 1
Spring boot 使用mybatis generator 自动生成代码插件
|
8月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
669 0