Expand an object to save the query results
Create a dto package SRC main Java com mybatis dto
Dto is a special JavaBean,JavaBean is a kind of Java class, which conforms to a certain writing specification. It is a specification of entity and information.
JavaBean specification:
(1) There must be 1 Public parameterless construct
(2) All properties private
(3) Attributes are exposed to other programs through getters and setters
(4) Class requires serializability
Create goodsdto in dto directory
package com.MyBatis.dto;
import com.MyBatis.entity.Goods;
public class GoodsDTO {
private Goods goods;
private String categoryName;
private String test;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
Write in mapper XMLLabel and write SQL statements
select g.*,c.category_name,'1' as test from t_goods g,t_category c
where g.category_id = c.category_id
test
@Test
public void testSelectGoodsDTO(){
SqlSession sqlSession=null;
try{
sqlSession=MyBatisUtils.openSession();
List list = sqlSession.selectList("goods.selectGoodsDTO");
for(GoodsDTO g : list){
System.out.println(g.getGoods().getTitle());
}
}catch (Exception e){
throw e;
}finally {
MyBatisUtils.closeSession(sqlSession);
}
}