ResultMap结果映射,LinkedHashMap虽然将按数据库字段顺序来存在数据,但是缺点是不能像Java实体类一样存储数据。而ResultMap可以将查询结果映射为复杂类型的Java对象,并且支持对象关联查询等高级特训,非常适用于Java对象保存多表联查的结果
我们将两个表的进行关联查询t_category和t_goods
创建GoodsDTO.java来接收多表联查的数据
// Data Transfer Object 数据传输对象
public class GoodsDTO {
private Goods goods = new Goods();
private Category category = new Category();
private String test;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@Override
public String toString() {
return "GoodsDTO{" +
"goods=" + goods +
", category=" + category +
", test='" + test + '\'' +
'}';
}
}
/**
* 多表联查
*/
@Test
public void selectGoodsDTO(){
SqlSession sqlSession = JdbcUtil.getSqlSession();
List
for(GoodsDTO g : map){
System.out.println(g);
}
}