zoukankan      html  css  js  c++  java
  • 反射的学习笔记--sql语句生成

    学生实体类

     public class Student
        {
        
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public string Address { get; set; }
            public string Tel { get; set; }
            public int GrateId { get; set; }
            public Grate grate { get; set; }
    
        }

    年级实体类

    1 public class Grate
    2     {
    3          
    4         public int Id { get; set; }
    5         public string Name { get; set; }
    6         public List<Student> Students { get; set; }
    7     }

    反射实现代码

     1  public class Entity<T>
     2     {
     3         public string GetSql(T en) 
     4         {
     5             Type ty = en.GetType();
     6             PropertyInfo []pf = ty.GetProperties();
     7             StringBuilder sb = new StringBuilder();
     8             sb.Append("insert into "+ty.Name+" (");
     9             foreach (PropertyInfo item in pf)
    10             {
    11                
    12 
    13                 sb.Append(item.Name + ",");
    14             }
    15         
    16             sb.Remove(sb.Length - 1, 1);
    17             sb.Append(") ");
    18             sb.Append("values(");
    19             foreach (PropertyInfo item in pf)
    20             {
    21                 
    22 
    23                 sb.Append("@" + item.Name + ",");
    24             }
    25             sb.Remove(sb.Length - 1, 1);
    26             sb.Append(")");
    27             return sb.ToString(); ;
    28         }
    29         
    30 
    31 
    32     }

    反射生成的sql 反射学习笔记

  • 相关阅读:
    我是来讲笑话的
    dom4j读取xml
    Mysql常用命令
    如何快速开发小型系统
    Spring aop的实现原理
    Spring IOC容器解析及实现原理
    如何编写更棒的代码
    Git使用教程
    关于程序员吃青春饭问题之探讨
    如何自学编程
  • 原文地址:https://www.cnblogs.com/zhousiyu/p/3228992.html
Copyright © 2011-2022 走看看