mybatis 查询集合里每一条数据包含子查询结果集。一对多查询,一对一查询,子查询。
实现方案:子查询
我重新写了一个实体类来作为返回给前端
public class LanguageVO {
private String code;
private String zhLanguage; //中文
private String cnLanguage; //英文
private List<TLanguageIta> list; //其它语言
get set方法略.....
}
原实体类:
public class LanguageI {
private String id;
private String code;
private String languageType;
private String content;
get set方法略..
}
方案一: 直接在类中用注解方式实现:
@Select("select code from language group by code")
@Results({
@Result(property = "code", column = "code"),
@Result(property = "list", column = "code",
many =@Many(select ="com.mapper.TLanguageItaMapper.getLanguageListByCode")),
@Result(property = "zhLanguage", column = "code",
one =@One(select ="com.mapper.TLanguageItaMapper.getZhLanguage")),
@Result(property = "cnLanguage", column = "code",
one =@One(select ="com.mapper.TLanguageItaMapper.getCnLanguage"))
})
List<LanguageVO> getLanguageListByPage();
@Select("select t.id,t.code,t.language_type as languageType,"
+ "t.content,t.remark from language t where t.code = #{code}")
List<TLanguageIta> getLanguageListByCode(@Param("code")String code);
@Select("select t.content from language t where t.code = #{code} and t.language_type = '1'")
String getZhLanguage(@Param("code")String code);
@Select("select t.content from language t where t.code = #{code} and t.language_type = '2'")
String getCnLanguage(@Param("code")String code);
方案二:在xml中实现
<resultMap type="com.vo.TLanguageVO" id="getLanguageList">
<result column="code" property="code"/>
<association property="zhLanguage"
select="getZhLanguage"
column="code"/>
<association property="cnLanguage"
select="getCnLanguage"
column="code"/>
<collection property="list"
select="getLanguageListByCode"
column="code"/>
</resultMap>
<select id="getLanguageListByPage" resultMap="getLanguageList" >
<select id="getZhLanguage" resultType="String">
select t.content from language t where t.code = #{code} and t.language_type = '1'
</select>
<select id="getCnLanguage" resultType="String">
select t.content from language t where t.code = #{code} and t.language_type = '2'
</select>
<select id="getLanguageListByCode" resultMap="BaseResultMap">
select t.id,t.code,t.language_type,t.content,t.remark from language t
where t.code = #{code}
</select>