mybatis入参为对象

1.对象传参方式一

Dao接口中方法

bash 复制代码
    /**
     * 根据对象中保存的属性查询emp对象
     * @param emp
     * @return
     */
    List<Emp> findByEmp(Emp emp);

mapper文件

bash 复制代码
   <select id="findByEmp" resultType="com.example.domain.Emp" parameterType="com.example.domain.Emp">
        select * from emp where name = #{name} or salary=#{salary} or job_id = #{job_id}
    </select>

结论:一个java对象作为方法的参数,使用对象的属性作为参数值使用。简单的语法:#{属性名},mybatis调用此属性的getXxx()方法获取属性值

2.对象传参方式二

编写接口,指定@Param注解参数(建议不要指定)

bash 复制代码
public interface LoginMapper {

    /**
     * 使用账号和密码登录 获取用户信息
     *
     * @param login 登录参数 account password
     * @return
     */
    Person selectPersonByAccountAndPassword01(@Param("login") Login login);
}

编写映射配置文件:

bash 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lihaozhe.mapper.LoginMapper">
    <!--使用账号和密码登录 获取用户信息-->
    <select id="selectPersonByAccountAndPassword01" resultType="person">
        SELECT p.id, nickname
        FROM login l
                 INNER JOIN person p
        WHERE pid = p.id
          AND account = #{login.account}
          AND auth_text = #{login.authText}
    </select>
</mapper>

mybatis传参

相关推荐
计算机学姐1 天前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
哲此一生9842 天前
SpringBoot3集成Mybatis(开启第一个集成Mybatis的后端接口)
java·spring boot·mybatis
九转苍翎2 天前
Java外功精要(3)——Spring配置文件和mybatis
spring boot·mybatis
程序员三明治2 天前
【Mybatis从入门到入土】ResultMap映射、多表查询与缓存机制全解析
java·sql·缓存·mybatis·resultmap·缓存机制·多表查询
此剑之势丶愈斩愈烈2 天前
mybatis-plus分页插件使用
mybatis
!if2 天前
springboot mybatisplus 配置SQL日志,但是没有日志输出
spring boot·sql·mybatis
讓丄帝愛伱2 天前
Mybatis Log Free插件使用
java·开发语言·mybatis
gaoshan123456789102 天前
‌MyBatis-Plus 的 LambdaQueryWrapper 可以实现 OR 条件查询‌
java·tomcat·mybatis
The best are water3 天前
jeesite mybatis添加拦截器,推送指定表的变更数据到其他数据库
数据库·mybatis
lunz_fly19923 天前
【源码解读之 Mybatis】【核心篇】-- 第6篇:StatementHandler语句处理器
mybatis