Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

MyBatis Parameter Passing Techniques

Tech May 18 2

MyBatis Parameter Passing Techniques

1. Single Primitive Data Type

2. Multiple Primitive Data Types

3. Single Reference Data Type

4. Map Collection Data Type

5. Multiple Reference Data Types

Interface

package com.msb.mapper;
import com.msb.pojo.Employee;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
 * Employee Data Access Interface
 */
public interface EmployeeMapper {
    /**
     * Retrieves all employee records
     * @return List of Employee objects
     */
    List<Employee> getAllEmployees();
    /**
     * Finds a single employee by their ID
     * @param id Employee identifier
     * @return Employee object if found, null otherwise
     */
    Employee findById(int id);
    /**
     * Finds employees by department ID and minimum salary
     * @param deptId Department identifier
     * @param minSalary Minimum salary threshold
     * @return List of Employee objects meeting criteria
     */
    List<Employee> findByDepartmentAndSalary(@Param("deptId") int deptId, @Param("minSalary") double minSalary);
    List<Employee> findByDepartmentAndSalary2(Map parameters);
    List<Employee> findByDepartmentAndSalary3(Employee criteria);
    List<Employee> findByDepartmentAndSalary4(@Param("firstCriteria") Employee firstCriteria, @Param("secondCriteria") Employee secondCriteria);
}

Mapper Configuration File



<mapper namespace="com.msb.mapper.EmployeeMapper">
    
    
    <select id="getAllEmployees" resultType="employee" >
        select * from employee
    </select>
    
    <select id="findById" resultType="employee" >
        select * from employee where id =#{id}
    </select>
    
    <select id="findByDepartmentAndSalary" resultType="employee">
        
        
        
    </select>
    
    <select id="findByDepartmentAndSalary2" resultType="employee" parameterType="map" >
        
        
         select * from employee where dept_id =#{deptId} and salary >= #{minSalary}
    </select>
    
    <select id="findByDepartmentAndSalary3" resultType="employee" parameterType="employee" >
        select * from employee where dept_id =#{deptId} and salary >= #{minSalary}
    </select>
    
    <select id="findByDepartmentAndSalary4" resultType="employee"  >
        
         select * from employee where dept_id =#{param1.deptId} and salary >= #{param2.minSalary}
         
    </select>
</mapper>

Test Code

package com.msb.testDemo;
import com.msb.mapper.EmployeeMapper;
import com.msb.pojo.Employee;
import com.msb.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class EmployeeTest {
    public static void main(String[] args) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession(true);
        /*
        * Creates a proxy implementation of the interface
        *
        */
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employees = mapper.getAllEmployees();
        for(Employee employee : employees) {
            System.out.println(employee);
        }
        // 1. Single primitive data type as method parameter
        Employee employee = mapper.findById(7902);
        System.out.println(employee);
        // 2. Multiple primitive data types as method parameters
        List<Employee> employees2 = mapper.findByDepartmentAndSalary(10, 1500);
        for(Employee emp : employees2) {
            System.out.println(emp);
        }
        // 3. Single reference type as method parameter
        Employee criteria = new Employee();
        criteria.setDeptId(10);
        criteria.setMinSalary(1500.0);
        List<Employee> employees3 = mapper.findByDepartmentAndSalary2(criteria);
        for(Employee emp : employees3) {
            System.out.println(emp);
        }
        // 4. Multiple reference types as method parameters
        Employee criteria1 = new Employee();
        criteria1.setDeptId(10);
        Employee criteria2 = new Employee();
        criteria2.setMinSalary(1500.0);
        List<Employee> employees4 = mapper.findByDepartmentAndSalary3(criteria1, criteria2);
        for(Employee emp : employees4) {
            System.out.println(emp);
        }
        sqlSession.close();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.