• IBatis一对多查询


     public  class User
        {
          public int UserId { get; set; }
          public string UserName { get; set; }
    
          
        }
    
        public class UserRight
        {
            public int UserRightId { get; set; }
            public int UserId { get; set; }
            public int RightId { get; set; }
            public string RightName { get; set; }
        }
    
        public class UserRightJoin
        {
            public int UserId { get; set; }
            public string UserName { get; set; }
    
            public IList<UserRight> UserRights { get; set; }
        }
    

      在Mapper的UserRight.xml中

    <?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="User"  xmlns="http://ibatis.apache.org/mapping"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <alias>
        <typeAlias alias="User" type="IBatis.User"/>
        <typeAlias alias="UserRight" type="IBatis.UserRight"/>
        <typeAlias alias="UserRightJoin" type="IBatis.UserRightJoin"/>
        
      </alias>
      <resultMaps>
        <resultMap id="UserRightReslut" class="UserRight">
          <result property="UserRightId" column="UserRightId"/>
          <result property="UserId" column="UserId"/>
          <result property="RightId" column="RightId"/>
          <result property="RightName" column="RightName"/>
        </resultMap>
        <resultMap id="UserReslut" class="User">
          <result property="UserId" column="UserId"/>
          <result property="UserName" column="UserName"/>
          
        </resultMap>
      
        <resultMap id="UserRightJoinReslut" class="UserRightJoin" extends="UserReslut" groupBy="UserId">
          <result property="UserRights" resultMapping="User.UserRightReslut"  />
        </resultMap>
    
      
        
      </resultMaps>
      <statements>
        <select id="selectAllUserRight" resultMap="UserRightJoinReslut">
          select A.*,b.*
          from  [dbo].[User] a join [userright] b on a.userid=b.userid
        </select>
       
      </statements>
    </sqlMap>
    

      

    在 sqlmap.config

    <?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="User"  xmlns="http://ibatis.apache.org/mapping"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <alias>
        <typeAlias alias="User" type="IBatis.User"/>
        <typeAlias alias="UserRight" type="IBatis.UserRight"/>
        <typeAlias alias="UserRightJoin" type="IBatis.UserRightJoin"/>
        
      </alias>
      <resultMaps>
        <resultMap id="UserRightReslut" class="UserRight">
          <result property="UserRightId" column="UserRightId"/>
          <result property="UserId" column="UserId"/>
          <result property="RightId" column="RightId"/>
          <result property="RightName" column="RightName"/>
        </resultMap>
        <resultMap id="UserReslut" class="User">
          <result property="UserId" column="UserId"/>
          <result property="UserName" column="UserName"/>
          
        </resultMap>
      
        <resultMap id="UserRightJoinReslut" class="UserRightJoin" extends="UserReslut" groupBy="UserId">
          <result property="UserRights" resultMapping="User.UserRightReslut"  />
        </resultMap>
    
      
        
      </resultMaps>
      <statements>
        <select id="selectAllUserRight" resultMap="UserRightJoinReslut">
          select A.*,b.*
          from  [dbo].[User] a join [userright] b on a.userid=b.userid
        </select>
       
      </statements>
    </sqlMap>
    

      

    然后是 DAO 

        public class BaseDao
        {
            public static ISqlMapper _sqlMap = null;
            static BaseDao()
            {
                  _sqlMap = Mapper.Instance();
            }
        }
    
    public class UserDao : BaseDao
        {
            public IList<UserRightJoin> GetList()
            {
                ISqlMapper mapper = _sqlMap;
                IList<UserRightJoin> ListPerson = mapper.QueryForList<UserRightJoin>("selectAllUserRight", null);  //这个"SelectAllPerson"就是xml映射文件的Id
                return ListPerson;
            }
     
            
            public decimal GetAmount()
            {
                ISqlMapper mapper = _sqlMap;
                decimal r = mapper.QueryForObject<decimal>("selectAmount", null);  //这个"SelectAllPerson"就是xml映射文件的Id
                return r;
            }
    
        }
    

      

    最后是调用 

    static void Main(string[] args) 
    { 
    UserDao ud=new UserDao(); 
    var lst= ud.GetList(); 
    }
    

      

  • 相关阅读:
    NYOJ 38布线问题
    NYOJ 106背包问题
    基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
    HDOJ 2546饭卡(01背包问题)
    FBI树-数据结构(二叉树)
    二叉树遍历(flist)(二叉树,已知中序层序,求先序)
    求先序排列(二叉树已知中序和后序,求先序)
    滑雪(dp)
    Python——plot可视化数据,作业8(python programming)
    数据库SQL语言学习----左外连接,右外连接,外连接,自然连接的形象对比
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5535788.html
Copyright © 2020-2023  润新知