I have a query like this
select id, employee_name as name, address from employee
To get the result from the query by using Mybatis normally I have to create an class Employee like:
package com.employee;
public class Employee {
Integer id;
String name;
String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
and Mybatis Java mapper:
package com.employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper {
Employee retrieveEmployee();
}
and Mybatis xml mapper:
<?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.employee.EmployeeMapper">
<resultMap id="retrieveEmployeeResult" type="com.employee.Employee">
<id property="id" column="id"/>
<result property="name" column="employee_name"/>
<result property="address" column="address"/>
</resultMap>
<select id="retrieveEmployee" resultMap="retrieveEmployeeResult">
select id, employee_name as name, address from employee
</select>
</mapper>
Is there anyway that I don't need to create the Employee class for mapping, can we just return the result set of the sql, then we will loop through the result set then get the value basing on the column's name.
Example the query returns 2 records like this:
id name address
1 Tom 123 street
2 Mike abc street
So my expectation will be something like(pseudo code):
ResultSet results = EmployeeMapper.retrieveEmployee();
System.out.println(results[0].get("id") + "-" + results[0].get("name") + "-" + results[0].get("address"));
// => the result will be: 1-Tom-123 street