table 转实体

 public class Table2Entity<T> where T : class,new()
    {
        public static List<T> GetEntitys(DataTable dt)
        {
            Dictionary<string, string> columns = new Dictionary<string, string>();
            foreach (DataColumn item in dt.Columns)
            {
                columns.Add(item.ColumnName.Trim().ToLower(), item.ColumnName);
            }
            List<T> result = new List<T>();
            var proptetis = typeof(T).GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            foreach (DataRow dr in dt.Rows)
            {
                T obj = new T();
                foreach (var item in proptetis)
                {
                    var attributes = item.GetCustomAttributes(typeof(RenameAttribute), true);
                    var columnName = (attributes != null && attributes.Length > 0) ? ((RenameAttribute)(attributes[0])).Name.Trim().ToLower() : item.Name.Trim().ToLower();
                    if (columns.Keys.Contains(columnName))
                    {
                        var mt = item.GetSetMethod(true);
                        var value = dr[columns[columnName]];
                        if (value == null || value == DBNull.Value || string.IsNullOrWhiteSpace(value.ToString())) continue;
                        if (item.PropertyType == typeof(string))
                            mt.Invoke(obj, new object[] { value.ToString() });
                        else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(int?))
                            mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
                        else if (item.PropertyType == typeof(long) || item.PropertyType == typeof(long?))
                            mt.Invoke(obj, new object[] { Convert.ToInt64(value) });
                        else if (item.PropertyType == typeof(double) || item.PropertyType == typeof(double?))
                            mt.Invoke(obj, new object[] { Convert.ToDouble(value) });
                        else if (item.PropertyType == typeof(decimal) || item.PropertyType == typeof(decimal?))
                            mt.Invoke(obj, new object[] { Convert.ToDecimal(value) });
                        else if (item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
                            mt.Invoke(obj, new object[] { Convert.ToDateTime(value) });
                        else if (item.PropertyType == typeof(float) || item.PropertyType == typeof(float?))
                            mt.Invoke(obj, new object[] { float.Parse(value.ToString()) });
                        else if (item.PropertyType == typeof(bool))
                            mt.Invoke(obj, new object[] { bool.Parse(value.ToString()) });
                        else if (item.PropertyType.BaseType == typeof(System.Enum))
                        {
                            mt.Invoke(obj, new object[] { Convert.ToInt32(value) });
                        }
                    }
                }
                result.Add(obj);
            }
            return result;
        }
    }

 

转载于:https://www.cnblogs.com/jasonlwings/p/6808372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,可以使用第三方库或框架来实现实体 MySQL create table 语句。常用的第三方库有 Mybatis Generator 和 JPA-Auto。这些库可以根据实体类的注解和属性,自动生成对应的 MySQL create table 语句。 示例: 1.使用Mybatis Generator ```xml <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.dao" targetProject="src/main/java"> </javaClientGenerator> <table tableName="test_table" domainObjectName="TestTable"> <generatedKey column="id" sqlStatement="JDBC" identity="true"/> </table> </context> </generatorConfiguration> ``` 2.使用 JPA-Auto ```java @Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name", nullable = false) private String name; @Column(name = "age", nullable = false) private Integer age; // getters and setters } ``` 使用jpa-auto 生成对应的 create table 语句 ``` mvn jpa-auto:generate ``` 注意: - 使用第三方库或框架时需要先安装相应的包。 - 上述两个示例只是简单的用法,实际使用时还需要根据需求进行配置。 希望这能帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值