Reputation: 11
I was trying to write a mybatis mapper for a select * query which would return me a list of rows from a ProcessType table. And each row has to be mapped to a ProcessType pojo. I know how to map a single row to the POJO, but how to go about this for list of Process Type?
POJO-->
class name : ProcessType
Properties:
String ABC;
String id;
String Date;
From mapper I call a proc 'XYZ' which returns me the cursor for list of rows for ProcessType table being queried.
Upvotes: 1
Views: 25251
Reputation: 2006
Assuming you've used the mybatis javaModelGenerator, sqlMapGenerator, and javaClientGenerator, all you would need to do is to use the .selectByExample()
function in your mapper class and feed in an "empty" Example object such as the following:
for (MyTable myTable : myTableMapper.selectByExample(new MyTableExample())) {
System.out.println("found ID: " + myTable.getId());
}
This is equivalent to a select *
Upvotes: 1
Reputation: 7691
All you need to do is define a resultMap
like this:
<resultMap id="processTypeMap" type="yourpackage.ProcessType">
<id property="id" column="id_in_db"/>
<result property="abc" column="abc_in_db"/>
<result property="date" column="date_in_db"/>
</resultMap>
The column
is the associated field in your table mapping.
Then set resultMap
as what you defined above like this:
<select id="getProcessTypes" resultMap="processTypeMap">
select * from ProcessType
</select>
Now the columns in your table will be mapped accordingly.
I suppose that you want to get a list of ProcessType
, so in the DAO
layer, you have to use the selectList
method.
List<ProcessType> processTypes = sqlSessionTemplate.selectList("getProcessTypes")
Upvotes: 1
Reputation: 25
If I understand this correctly are you using a collection in your result map? this tells mybatis to expect a list.
<collect property="" column="">
Upvotes: 0
Reputation: 16086
I don't understand well at all the question. Primary, I think it is not needed a procedure for this simple operation, I would do it as a simple query.
So, If you have this entity.
public class ProcessType {
String ABC, id, Date;
public ProcessType(String aBC, String id, String date) {
ABC = aBC;
this.id = id;
Date = date;
}
public String getABC() {
return ABC;
}
public void setABC(String aBC) {
ABC = aBC;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
}
And a table ProcessType as something like this:
create table PROCESS_TYPE (
ABC VARCHAR(200),
ID VARCHAR(200),
DATE VARCHAR(200)
);
Your mapper using annotations should be as follows:
public interface MapperProcessType {
@Select("select * from PROCESS_TYPE")
@Results({
@Result(property = "ABC", column = "ABC"),
@Result(property = "id", column = "ID"),
@Result(property = "date", column = "DATE")
})
public List<ProcessType> findAll();
}
On other hand, using xml It is as the next:
<resultMap id = "result" type = "ProcessType">
<result property = "ABC" column = "ABC"/>
<result property = "id" column = "ID"/>
<result property = "date" column = "DATE"/>
</resultMap>
<select id = "findAll" resultMap = "result">
SELECT * FROM PROCESS_TYPE
</select>
Upvotes: 5
Reputation: 2463
Your Mapper.java class just needs to return a list:
List<ProcessType> getProcessTypes();
Your Mapper.xml should use the class as the resultType:
<select id="getProcessTypes" resultType="path.to.ProcessType">
Or you can create a result map to map your columns to the ProcessType properties, but that's outside the scope of this question.
Upvotes: 1