通过泛型插入(更新)实体数据


/// <summary>
/// 通过泛型插入数据
/// </summary>
/// <typeparam name="T">类名称</typeparam>
/// <param name="obj">类对象,如果要插入空值,请使用@NULL</param>
/// <returns>插入的新记录ID</returns>
public static int Insert<T>(T obj)
{

StringBuilder strSQL = new StringBuilder();

strSQL = GetInsertSQL(obj);

// 插入到数据库中
object result = SQLPlus.ExecuteScalar(CommandType.Text, strSQL, null);

return Convert.IsDBNull(result) ? 0 : Convert.ToInt32(result);
}

/// <summary>
/// 通过泛型更新数据
/// </summary>
/// <typeparam name="T">类名称</typeparam>
/// <param name="obj">类对象,如果要更新空值,请使用@NULL</param>
/// <returns>更新结果,大于0为更新成功</returns>
public static int Update<T>(T obj)
{

StringBuilder strSQL = new StringBuilder();
strSQL = GetUpdateSQL(obj);

if (String.IsNullOrEmpty(strSQL.ToString()))
{
return 0;
}


// 更新到数据库中
object result = SQLPlus.ExecuteNonQuery(CommandType.Text, strSQL, null);

int returnValue = Convert.IsDBNull(result) ? 0 : Convert.ToInt32(result);

return returnValue;

}

/// <summary>
/// 获取实体的插入语句
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="obj">实体对象</param>
/// <returns>返回插入语句</returns>
public static StringBuilder GetInsertSQL<T>(T obj)
{

string tableKey = GetPropertyValue(obj, BaseSet.PrimaryKey);
string keyValue = GetPropertyValue(obj, tableKey);
string tableName = GetPropertyValue(obj, BaseSet.TableName);

Type t = obj.GetType();//获得该类的Type

StringBuilder strSQL = new StringBuilder();

strSQL.Append("insert into " + tableName + "(");

string fields = "";
string values = "";

//再用Type.GetProperties获得PropertyInfo[]
foreach (PropertyInfo pi in t.GetProperties())
{

object name = pi.Name;//用pi.GetValue获得值

// 替换Sql注入符
string value1 = Convert.ToString(pi.GetValue(obj, null)).Replace("'", "''");

//string dataType = pi.PropertyType.ToString().ToLower();

string properName = name.ToString().ToLower();

if (!string.IsNullOrEmpty(value1) && properName != tableKey.ToLower() && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
{
// 判断是否为空
if (value1 == BaseSet.NULL)
{
value1 = "";
}

fields += Convert.ToString(name) + ",";
values += "'" + value1 + "',";

}

}

// 去掉最后一个,
fields = fields.TrimEnd(',');
values = values.TrimEnd(',');

// 拼接Sql串
strSQL.Append(fields);
strSQL.Append(") values (");
strSQL.Append(values);
strSQL.Append(")");

strSQL.Append(";SELECT @@IDENTITY;");

return strSQL;
}

/// <summary>
/// 获取实体的更新SQL串
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="obj">实体对象</param>
/// <returns>返回插入语句</returns>
private static StringBuilder GetUpdateSQL<T>(T obj)
{

string tableKey = GetPropertyValue(obj, BaseSet.PrimaryKey);
string keyValue = GetPropertyValue(obj, tableKey);
string tableName = GetPropertyValue(obj, BaseSet.TableName);
StringBuilder strSQL = new StringBuilder();

if (string.IsNullOrEmpty(keyValue))
{
return strSQL;
}

Type t = obj.GetType();//获得该类的Type

strSQL.Append("update " + tableName + " set ");

string subSQL = "";

string condition = " where " + tableKey + "='" + keyValue.Replace("'", "''") + "'";


//再用Type.GetProperties获得PropertyInfo[]
foreach (PropertyInfo pi in t.GetProperties())
{

object name = pi.Name;//用pi.GetValue获得值

// 替换Sql注入符
string value1 = Convert.ToString(pi.GetValue(obj, null)).Replace("'", "''");

//string dataType = pi.PropertyType.ToString().ToLower();

string properName = name.ToString().ToLower();

if (!string.IsNullOrEmpty(value1) && properName != tableKey.ToLower() && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
{
// 判断是否为空
if (value1 == BaseSet.NULL)
{
value1 = "";
}

subSQL += Convert.ToString(name) + "='" + value1 + "',";

}

}

// 去掉最后一个,
subSQL = subSQL.TrimEnd(',');

// 拼接上更新子句
strSQL.Append(subSQL);

// 加入更新条件
strSQL.Append(condition);

return strSQL;

}

public class BaseSet
{
public static string NULL
{
get { return "@null"; }

}

public static string DateTimeShortNull
{
get { return "0001-1-1 0:00:00"; }

}

public static string DateTimeLongNull
{
get { return "0001-01-01 00:00:00"; }

}

public static string PrimaryKey
{
get { return "PrimaryKey"; }

}

public static string TableName
{
get { return "TableName"; }

}
}

#region 实体样例
[Serializable]
public class SortsInfo
{
private int _SortID;
private string _SortName;
public string TableName
{
get { return "Sorts"; }
}
public string PrimaryKey
{
get { return "SortID"; }
}
public int SortID
{
get { return _SortID; }
set
{
_SortID = value;
}
}
public string SortName
{
get { return _SortName; }
set
{
_SortName = value;
}
}

}

#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,以下是使用MybatisPlus框架下批量插入数据的具体实现逻辑: 1. 首先,需要在 pom.xml 文件中添加 MybatisPlus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.x.x</version> </dependency> ``` 2. 在实体类中添加 @TableName 注解,指定表名: ```java @Data @TableName("user") // 指定表名 public class User { private Long id; private String name; private Integer age; private String email; } ``` 3. 在 Dao 层中,编写批量插入数据的方法: ```java @Repository public interface UserDao extends BaseMapper<User> { /** * 批量插入数据 * * @param userList 用户列表 * @return 插入成功的记录数 */ int insertBatch(@Param("list") List<User> userList); } ``` 4. 在 Mapper.xml 文件中,编写批量插入数据SQL 语句: ```xml <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (name, age, email) VALUES <foreach collection="list" item="item" separator=","> (#{item.name}, #{item.age}, #{item.email}) </foreach> </insert> ``` 5. 最后,在 Service 层调用批量插入数据的方法即可: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public int insertBatch(List<User> userList) { return userDao.insertBatch(userList); } } ``` 以上就是使用 MybatisPlus 框架实现批量插入数据的具体实现逻辑。需要注意的是,在使用批量插入数据时,建议在插入之前先对数据进行校验和处理,以保证数据的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值