JdbcEntityDaoSupport 类添加 update 方法,方便使用。

This commit is contained in:
zhouxy108 2022-12-07 18:29:29 +08:00
parent e916d067f3
commit cb07c0befd
5 changed files with 26 additions and 22 deletions

View File

@ -56,6 +56,10 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
return Boolean.TRUE.equals(isExists); return Boolean.TRUE.equals(isExists);
} }
protected final int update(String sql, SqlParameterSource parameterSource) {
return this.jdbc.update(sql, parameterSource);
}
protected abstract T mapRow(ResultSet rs) throws SQLException; protected abstract T mapRow(ResultSet rs) throws SQLException;
protected void setRowMapper(@Nonnull RowMapper<T> rowMapper) { protected void setRowMapper(@Nonnull RowMapper<T> rowMapper) {

View File

@ -34,7 +34,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override @Override
protected final void doDelete(@Nonnull Account entity) { protected final void doDelete(@Nonnull Account entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_account SET deleted = id, "version" = "version" + 1 UPDATE sys_account SET deleted = id, "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
@ -132,15 +132,16 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override @Override
protected final Account doInsert(@Nonnull Account entity) { protected final Account doInsert(@Nonnull Account entity) {
String sql = """
INSERT INTO sys_account
(id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, created_by, create_time)
VALUES
(:id, :email, :mobilePhone, :username, :password, :salt, :avatar, :sex, :nickname, :status, :createdBy, :createTime)
""";
long id = IdUtil.getSnowflakeNextId(); long id = IdUtil.getSnowflakeNextId();
SqlParameterSource params = generateParamSource(id, entity); int i = update("""
int i = jdbc.update(sql, params); INSERT INTO sys_account
(id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
created_by, create_time)
VALUES
(:id, :email, :mobilePhone, :username, :password, :salt, :avatar, :sex, :nickname, :status,
:createdBy, :createTime)
""",
generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.update(i, 1);
this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds());
return entity; return entity;
@ -148,7 +149,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override @Override
protected final Account doUpdate(@Nonnull Account entity) { protected final Account doUpdate(@Nonnull Account entity) {
String sql = """ int i = update("""
UPDATE sys_account UPDATE sys_account
SET "email" = :email, SET "email" = :email,
"mobile_phone" = :mobilePhone, "mobile_phone" = :mobilePhone,
@ -163,9 +164,8 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
"update_time" = :updateTime, "update_time" = :updateTime,
"version" = "version" + 1 "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
"""; """,
SqlParameterSource params = generateParamSource(entity); generateParamSource(entity));
int i = this.jdbc.update(sql, params);
AssertResult.update(i, 1); AssertResult.update(i, 1);
this.accountRoleDAO.saveAccountRoleRefs(entity); this.accountRoleDAO.saveAccountRoleRefs(entity);
return entity; return entity;

View File

@ -42,7 +42,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
protected final Dict doInsert(@Nonnull Dict entity) { protected final Dict doInsert(@Nonnull Dict entity) {
long id = IdUtil.getSnowflakeNextId(); long id = IdUtil.getSnowflakeNextId();
int i = this.jdbc.update(""" int i = update("""
INSERT INTO sys_dict_type (dict_type, dict_label, create_time, created_by) INSERT INTO sys_dict_type (dict_type, dict_label, create_time, created_by)
VALUES (:dictType, :dictLabel, :createTime, :createdBy) VALUES (:dictType, :dictLabel, :createTime, :createdBy)
""", """,
@ -54,7 +54,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
protected final Dict doUpdate(@Nonnull Dict entity) { protected final Dict doUpdate(@Nonnull Dict entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_dict_type UPDATE sys_dict_type
SET dict_type = :dictType, SET dict_type = :dictType,
dict_label = :dictLabel, dict_label = :dictLabel,
@ -71,7 +71,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
protected final void doDelete(@Nonnull Dict entity) { protected final void doDelete(@Nonnull Dict entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_dict_type SET deleted = id, "version" = "version" + 1 UPDATE sys_dict_type SET deleted = id, "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,

View File

@ -61,7 +61,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
:component, :cache, :resource, :createTime, :createdBy) :component, :cache, :resource, :createTime, :createdBy)
"""; """;
MapSqlParameterSource paramSource = generateParamSource(id, entity); MapSqlParameterSource paramSource = generateParamSource(id, entity);
int i = this.jdbc.update(sql, paramSource); int i = update(sql, paramSource);
AssertResult.update(i, 1); AssertResult.update(i, 1);
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return entity;
@ -91,7 +91,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
"""; """;
// 更新菜单 // 更新菜单
int i = this.jdbc.update(sql, generateParamSource(entity)); int i = update(sql, generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.update(i, 1);
// 保存权限 // 保存权限
@ -102,7 +102,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
@Override @Override
protected final void doDelete(@Nonnull Menu entity) { protected final void doDelete(@Nonnull Menu entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_menu SET deleted = id, "version" = "version" + 1 UPDATE sys_menu SET deleted = id, "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,

View File

@ -46,7 +46,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
@Override @Override
protected final void doDelete(@Nonnull Role entity) { protected final void doDelete(@Nonnull Role entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_account SET deleted = id, "version" = "version" + 1 UPDATE sys_account SET deleted = id, "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
@ -64,7 +64,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
@Override @Override
protected final Role doInsert(@Nonnull Role entity) { protected final Role doInsert(@Nonnull Role entity) {
long id = IdUtil.getSnowflakeNextId(); long id = IdUtil.getSnowflakeNextId();
int i = this.jdbc.update(""" int i = update("""
INSERT INTO sys_role INSERT INTO sys_role
(id, "name", identifier, status, remarks, create_time, created_by) (id, "name", identifier, status, remarks, create_time, created_by)
VALUES VALUES
@ -78,7 +78,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
@Override @Override
protected final Role doUpdate(@Nonnull Role entity) { protected final Role doUpdate(@Nonnull Role entity) {
int i = this.jdbc.update(""" int i = update("""
UPDATE sys_role UPDATE sys_role
SET "name" = :name, SET "name" = :name,
"identifier" = :identifier, "identifier" = :identifier,