IBatis一对多嵌套查询
1)类
public class AppData { // public int ModuleId { get; set; } public int DataId { get; set; } public int DataSequence { get; set; } public string ContentValue { get; set; } } public class AppModuleJoin { //public int PageId { get; set; } public int ModuleId { get; set; } public IList<AppData> DataList { get; set; } //public int DataId { get; set; } //public int DataSequence { get; set; } //public string ContentValue { get; set; } } public class AppModule { //public int PageId { get; set; } public int ModuleId { get; set; } //public int DataId { get; set; } //public int DataSequence { get; set; } //public string ContentValue { get; set; } } public class AppPageJoin { public int PageId { get; set; } public IList<AppModuleJoin> ModuleList { get; set; } } public class AppPage { public int PageId { get; set; } }
sqlxml
<?xml version="1.0" encoding="utf-8" ?> <!--<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">--> <sqlMap namespace="App" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <alias> <typeAlias alias="AppPageJoin" type="IBatis.AppPageJoin"/> <typeAlias alias="AppPage" type="IBatis.AppPage"/> <typeAlias alias="AppModule" type="IBatis.AppModule"/> <typeAlias alias="AppModuleJoin" type="IBatis.AppModuleJoin"/> <typeAlias alias="AppData" type="IBatis.AppData"/> </alias> <resultMaps> <!--<resultMap id="AppModuleReslut" class="AppModule"> --><!--<result property="PageId" column="PageId"/>--><!-- <result property="ModuleId" column="ModuleId"/> <result property="DataId" column="DataId"/> <result property="DataSequence" column="DataSequence"/> <result property="ContentValue" column="ContentValue"/> </resultMap>--> <resultMap id="AppDataReslut" class="AppData"> <!--<result property="PageId" column="PageId"/>--> <!--<result property="ModuleId" column="ModuleId"/>--> <result property="DataId" column="DataId"/> <result property="DataSequence" column="DataSequence"/> <result property="ContentValue" column="ContentValue"/> </resultMap> <resultMap id="AppModuleReslut" class="AppModule"> <!--<result property="PageId" column="PageId"/>--> <result property="ModuleId" column="ModuleId"/> </resultMap> <resultMap id="AppPageReslut" class="AppPage"> <result property="PageId" column="PageId"/> </resultMap> <resultMap id="AppPageModuleJoinReslut" class="AppModuleJoin" extends="AppModuleReslut" groupBy="ModuleId"> <result property="DataList" resultMapping="App.AppDataReslut" /> </resultMap> <resultMap id="AppPageJoinReslut" class="AppPageJoin" extends="AppPageReslut" groupBy="PageId"> <result property="ModuleList" resultMapping="App.AppPageModuleJoinReslut" /> </resultMap> </resultMaps> <statements> <select id="QueryData" resultMap="AppPageJoinReslut"> select * from QueryData </select> </statements> </sqlMap>
在调用中
public IList<AppPageJoin> QueryData() { ISqlMapper mapper = _sqlMap; IList<AppPageJoin> ListPerson = mapper.QueryForList<AppPageJoin>("QueryData", null); //这个"SelectAllPerson"就是xml映射文件的Id return ListPerson; }