zoukankan      html  css  js  c++  java
  • 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(); 
    }
    

      

  • 相关阅读:
    Matlab从入门到精通 Chapter5 数据可视化
    给source insight添加.cc的C++文件后缀识别
    机构研究报告
    配置Haproxy
    Ceph:一个 Linux PB 级分布式文件系统
    Centos安装源包出错Package xxx.rpm is not signed
    [虚拟机] 小实验: 使用KVM虚拟机,安装一个windows系统
    关于北京地铁通车计划
    python字符串和数字的基本运算符 valar
    python种类 valar
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5535788.html
Copyright © 2011-2022 走看看