Mybatis多个接口构成重载如何解决

entity:

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private String phone;
    private String address;
}

dao层的接口:

java 复制代码
    List<User> selectAll();
    //方法重载 不推荐使用 调用API,不使用代理可以
    List<User> selectAll(String name);

mapper.xml文件

XML 复制代码
<?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">
<!--
    namespace属性:指定当前mapper配置文件的唯一标识符,取值为对应接口的全名
-->
<mapper namespace="org.wanho.dao.UserDAO">
    <!--
        insert:用来执行添加操作
            id属性:表示当前的方法名,取值必须与接口中的方法名相同
            parameterType属性:表示方法的参数类型
                如果参数是对象,可以使用类的全名
                如果参数是普通数据,可以使用mybatis中的别名
            标签体:编写sql语句
                使用#{xxx}表示占位符
                如果参数是对象,则xxx为对象的属性
                如果参数是普通数据,则xxx为参数名
    -->

<!--  sql:定义sql代码块,便于复用
            id属性:指定该sql代码段的唯一标识符
        -->
    <sql id="baseColumn">
        id, username, password, phone, address
    </sql>

    <sql id="baseQuery">
        select <include refid="baseColumn"/> from t_user
    </sql>


    <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
        insert into t_user (<include refid="baseColumn"/>)
        values (null,#{username}, #{password}, #{phone}, #{address})
    </insert>

    <update id="update">
        update t_user set username = #{username},
                          password = #{password},
                          phone = #{phone},
                          address = #{address}
        where id = #{id}
    </update>
    <select id="orderByColumn">
        <include refid="baseQuery"/> where 2=2
             order by ${id} desc
    </select>

    <select id="selectAll" resultType="User">
        <include refid="baseQuery"/>
    </select>

    <select id="selectAllByName" resultType="user">
        <include refid="baseQuery"/> where username like concat('%',#{username},'%')
    </select>
</mapper>

测试类:

java 复制代码
public class UserTest02 {
    private UserDAO userDAO;
    private SqlSession session;

    @Before
    public void before() {
        session = MybatisUtil.getSession();
        userDAO = session.getMapper(UserDAO.class);
    }  

  @Test
    public void selectAll() {
        userDAO = session.getMapper(UserDAO.class);
        List<User> userList = userDAO.selectAll();
        System.out.println("userList = " + userList);
    }

    @Test
    public void selectAllByName(){
        /**
         * 当dao层的方法构成重载的时候,不能通过指定id为方法名的方式进行配置,
         * 只能使用原生的sql执行该操作
         * 解决接口中的重载 ,直接调用 sqlSession 的 API
         */
        List<Object> th = session.selectList("org.wanho.dao.UserDAO.selectAll", "th");
        th.forEach(System.out::println);
    }
}

测试结果:

相关推荐
共享家95273 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
惊讶的猫4 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy4 小时前
Spring全家桶
java·spring·rpc
Halo_tjn4 小时前
基于封装的专项 知识点
java·前端·python·算法
Fleshy数模5 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
像少年啦飞驰点、5 小时前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用全闭环指南
java·spring boot·web开发·编程入门·后端开发
苍煜5 小时前
万字详解Maven打包策略:从基础插件到多模块实战
java·maven
有来技术5 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5166 小时前
xxx医患档案管理系统
java·spring boot·vue·毕业设计·智慧城市
一个响当当的名号6 小时前
lectrue9 索引并发控制
java·开发语言·数据库