From 55a9e7864e91e8fab5240c8c96d7b28dfc52191d Mon Sep 17 00:00:00 2001
From: "Yangkai.Shen" <237497819@qq.com>
Date: Tue, 16 Oct 2018 16:49:45 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=9B=86=E6=88=90=20Jdbc=20Templa?=
=?UTF-8?q?te=EF=BC=8C=E5=B9=B6=E7=AE=80=E6=98=93=E5=B0=81=E8=A3=85?=
=?UTF-8?q?=E9=80=9A=E7=94=A8=20Dao=20=E5=B1=82=EF=BC=8C=E9=80=9A=E7=94=A8?=
=?UTF-8?q?=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../annotation/{Auto.java => Pk.java} | 12 +-
.../orm/jdbctemplate/constant/Const.java | 8 +
.../controller/UserController.java | 30 ++-
.../orm/jdbctemplate/dao/UserDao.java | 45 +++-
.../orm/jdbctemplate/dao/base/BaseDao.java | 197 +++++++++++++++---
.../orm/jdbctemplate/entity/User.java | 4 +-
.../jdbctemplate/service/IUserService.java | 36 ++++
.../service/impl/UserServiceImpl.java | 63 +++++-
8 files changed, 358 insertions(+), 37 deletions(-)
rename spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/{Auto.java => Pk.java} (77%)
diff --git a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Auto.java b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Pk.java
similarity index 77%
rename from spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Auto.java
rename to spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Pk.java
index 2f71566..aea8366 100644
--- a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Auto.java
+++ b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/annotation/Pk.java
@@ -7,11 +7,11 @@ import java.lang.annotation.Target;
/**
*
- * 自增列注解
+ * 主键注解
*
*
* @package: com.xkcoding.orm.jdbctemplate.annotation
- * @description: 自增列注解
+ * @description: 主键注解
* @author: yangkai.shen
* @date: Created in 2018/10/15 11:23 AM
* @copyright: Copyright (c) 2018
@@ -20,5 +20,11 @@ import java.lang.annotation.Target;
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
-public @interface Auto {
+public @interface Pk {
+ /**
+ * 自增
+ *
+ * @return 自增主键
+ */
+ boolean auto() default true;
}
diff --git a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/constant/Const.java b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/constant/Const.java
index 8d4881d..2156ea5 100644
--- a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/constant/Const.java
+++ b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/constant/Const.java
@@ -14,5 +14,13 @@ package com.xkcoding.orm.jdbctemplate.constant;
* @modified: yangkai.shen
*/
public interface Const {
+ /**
+ * 加密盐前缀
+ */
String SALT_PREFIX = "::SpringBootDemo::";
+
+ /**
+ * 逗号分隔符
+ */
+ String SEPARATOR_COMMA = ",";
}
diff --git a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/controller/UserController.java b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/controller/UserController.java
index 72829bb..4616ff3 100644
--- a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/controller/UserController.java
+++ b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/controller/UserController.java
@@ -5,9 +5,9 @@ import com.xkcoding.orm.jdbctemplate.entity.User;
import com.xkcoding.orm.jdbctemplate.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
/**
*
@@ -37,4 +37,28 @@ public class UserController {
Boolean save = userService.save(user);
return Dict.create().set("code", save ? 200 : 500).set("msg", save ? "成功" : "失败").set("data", save ? user : null);
}
+
+ @DeleteMapping("/user/{id}")
+ public Dict delete(@PathVariable Long id) {
+ Boolean delete = userService.delete(id);
+ return Dict.create().set("code", delete ? 200 : 500).set("msg", delete ? "成功" : "失败");
+ }
+
+ @PutMapping("/user/{id}")
+ public Dict update(@RequestBody User user, @PathVariable Long id) {
+ Boolean update = userService.update(user, id);
+ return Dict.create().set("code", update ? 200 : 500).set("msg", update ? "成功" : "失败").set("data", update ? user : null);
+ }
+
+ @GetMapping("/user/{id}")
+ public Dict getUser(@PathVariable Long id) {
+ User user = userService.getUser(id);
+ return Dict.create().set("code", 200).set("msg", "成功").set("data", user);
+ }
+
+ @GetMapping("/user")
+ public Dict getUser(User user) {
+ List userList = userService.getUser(user);
+ return Dict.create().set("code", 200).set("msg", "成功").set("data", userList);
+ }
}
diff --git a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/UserDao.java b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/UserDao.java
index a114008..0ccf990 100644
--- a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/UserDao.java
+++ b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/UserDao.java
@@ -6,6 +6,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
+import java.util.List;
+
/**
*
* User Dao
@@ -20,7 +22,7 @@ import org.springframework.stereotype.Repository;
* @modified: yangkai.shen
*/
@Repository
-public class UserDao extends BaseDao {
+public class UserDao extends BaseDao {
@Autowired
public UserDao(JdbcTemplate jdbcTemplate) {
@@ -36,4 +38,45 @@ public class UserDao extends BaseDao {
public Integer insert(User user) {
return super.insert(user, true);
}
+
+ /**
+ * 根据主键删除用户
+ *
+ * @param id 主键id
+ * @return 操作影响行数
+ */
+ public Integer delete(Long id) {
+ return super.deleteById(id);
+ }
+
+ /**
+ * 更新用户
+ *
+ * @param user 用户对象
+ * @param id 主键id
+ * @return 操作影响行数
+ */
+ public Integer update(User user, Long id) {
+ return super.updateById(user, id, true);
+ }
+
+ /**
+ * 根据主键获取用户
+ *
+ * @param id 主键id
+ * @return id对应的用户
+ */
+ public User selectById(Long id) {
+ return super.findOneById(id);
+ }
+
+ /**
+ * 根据查询条件获取用户列表
+ *
+ * @param user 用户查询条件
+ * @return 用户列表
+ */
+ public List selectUserList(User user) {
+ return super.findByExample(user);
+ }
}
diff --git a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/base/BaseDao.java b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/base/BaseDao.java
index 4c7958e..42c735d 100644
--- a/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/base/BaseDao.java
+++ b/spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/base/BaseDao.java
@@ -1,20 +1,27 @@
package com.xkcoding.orm.jdbctemplate.dao.base;
+import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
+import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
-import com.xkcoding.orm.jdbctemplate.annotation.Auto;
import com.xkcoding.orm.jdbctemplate.annotation.Column;
import com.xkcoding.orm.jdbctemplate.annotation.Ignore;
+import com.xkcoding.orm.jdbctemplate.annotation.Pk;
import com.xkcoding.orm.jdbctemplate.annotation.Table;
+import com.xkcoding.orm.jdbctemplate.constant.Const;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
import java.util.List;
+import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -32,46 +39,168 @@ import java.util.stream.Stream;
* @modified: yangkai.shen
*/
@Slf4j
-public class BaseDao {
+public class BaseDao {
private JdbcTemplate jdbcTemplate;
+ private Class clazz;
+ @SuppressWarnings(value = "unchecked")
public BaseDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
+ clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
- * 通用插入
+ * 通用插入,自增列需要添加 {@link Pk} 注解
*
* @param t 对象
* @param ignoreNull 是否忽略 null 值
* @return 操作的行数
*/
protected Integer insert(T t, Boolean ignoreNull) {
+ String table = getTableName(t);
+
+ List filterField = getField(t, ignoreNull);
+
+ List columnList = getColumns(filterField);
+
+ String columns = StrUtil.join(Const.SEPARATOR_COMMA, columnList);
+
+ // 构造占位符
+ String params = StrUtil.repeatAndJoin("?", columnList.size(), Const.SEPARATOR_COMMA);
+
+ // 构造值
+ Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();
+
+ String sql = StrUtil.format("INSERT INTO {table} ({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params));
+ log.debug("【执行SQL】SQL:{}", sql);
+ log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
+ return jdbcTemplate.update(sql, values);
+ }
+
+ /**
+ * 通用根据主键删除
+ *
+ * @param pk 主键
+ * @return 影响行数
+ */
+ protected Integer deleteById(P pk) {
+ String tableName = getTableName();
+ String sql = StrUtil.format("DELETE FROM {table} where id = ?", Dict.create().set("table", tableName));
+ log.debug("【执行SQL】SQL:{}", sql);
+ log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(pk));
+ return jdbcTemplate.update(sql, pk);
+ }
+
+ /**
+ * 通用根据主键更新,自增列需要添加 {@link Pk} 注解
+ *
+ * @param t 对象
+ * @param pk 主键
+ * @param ignoreNull 是否忽略 null 值
+ * @return 操作的行数
+ */
+ protected Integer updateById(T t, P pk, Boolean ignoreNull) {
+ String tableName = getTableName(t);
+
+ List filterField = getField(t, ignoreNull);
+
+ List columnList = getColumns(filterField);
+
+ List columns = columnList.stream().map(s -> StrUtil.appendIfMissing(s, " = ?")).collect(Collectors.toList());
+ String params = StrUtil.join(Const.SEPARATOR_COMMA, columns);
+
+ // 构造值
+ List