欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

mybatisplus中的xml对象参数传递问题,

来源: javaer 分享于  点击 14971 次 点评:171

mybatisplus中的xml对象参数传递问题,


目录
  • mybatisplus中的xml对象参数传递
    • 举个栗子
  • mybatis传递参数四种方式
    • 方式一、顺序传递参数
    • 方式二、注解@Param传递参数
    • 方式三、使用Map集合传递参数
    • 方式四、使用JavaBean实体类传递参数

mybatisplus中的xml对象参数传递

如果是一般类型的参数,直接把类型加上,在xml的sql中通过#{}或者${}的方式引入就行了,如果是一个java对象,在mapper的参数前面加上@Param注解,给定参数名,在xml中直接调用。

举个栗子

下面是mapper的接口的一个方法

List<DesHistoryVo> getHistory(@Param("dto") HistoryQueryDto dto);

接着在xml中调用dto对象的属性

省略...
and supplier_id = ${dto.supplierId}
省略...

在select或者是其它xml标签中,记得填写parameterType参数的类型,也就是全类名,直接右键对象,copy reference就行了。

另外,可以根据sql输出的列,可以直接将对象转换为给定的对象,入页面展示需要的Vo对象,这时就需要配置resultType参数,同样,也是对象的全类名。

mybatis传递参数四种方式

方式一、顺序传递参数

mapper.java文件:

public User selectUser(String name, int deptId);

mapper.xml文件:

<select id="selectUser" resultType="com.wyj.entity.po.User">
    select * from user where userName = #{0} and deptId = #{1}
</select>

注意:里面的数字代表你传入参数的顺序,不是特别建议使用这种方法传递参数,特别是参数个数多的时候

方式二、注解@Param传递参数

mapper.java文件:

public User selectUser(@Param("userName") String name, int @Param("deptId") id);

mapper.xml文件:

<select id="selectUser" resultType="com.wyj.entity.po.User">
    select * from user where userName = #{userName} and deptId = #{deptId}
</select>

注意:在xml文件中就只能以在@Param注解中声明的参数名称获取参数

方式三、使用Map集合传递参数

mapper.java文件:

public User selectUser(Map<String, Object> params);

mapper.xml文件:

<select id="selectUser" parameterType="java.util.Map" resultType="com.wyj.entity.po.User">
    select * from user where userName = #{userName} and deptId = #{deptId}
</select>

方式四、使用JavaBean实体类传递参数

mapper.java文件:

public User selectUser(User user);

mapper.xml文件:

<select id="selectUser" parameterType="com.wyj.entity.po.User" resultType="com.wyj.entity.po.User">
    select * from user where userName = #{userName} and deptId = #{deptId}
</select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • Mybatis传递多个参数的三种实现方法
  • Mybatis如何传入多个参数(实体类型和基本类型)
  • Mybatis或Mybatis-Plus框架的xml文件中特殊符号的使用详解
相关栏目:

用户点评