Skip to content

Commit 55a9e78

Browse files
committed
✨ 集成 Jdbc Template,并简易封装通用 Dao 层,通用增删改查完成
1 parent 9da75f9 commit 55a9e78

File tree

8 files changed

+358
-37
lines changed

8 files changed

+358
-37
lines changed
+9-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
/**
99
* <p>
10-
* 自增列注解
10+
* 主键注解
1111
* </p>
1212
*
1313
* @package: com.xkcoding.orm.jdbctemplate.annotation
14-
* @description: 自增列注解
14+
* @description: 主键注解
1515
* @author: yangkai.shen
1616
* @date: Created in 2018/10/15 11:23 AM
1717
* @copyright: Copyright (c) 2018
@@ -20,5 +20,11 @@
2020
*/
2121
@Retention(RetentionPolicy.RUNTIME)
2222
@Target({ElementType.FIELD})
23-
public @interface Auto {
23+
public @interface Pk {
24+
/**
25+
* 自增
26+
*
27+
* @return 自增主键
28+
*/
29+
boolean auto() default true;
2430
}

spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/constant/Const.java

+8
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
* @modified: yangkai.shen
1515
*/
1616
public interface Const {
17+
/**
18+
* 加密盐前缀
19+
*/
1720
String SALT_PREFIX = "::SpringBootDemo::";
21+
22+
/**
23+
* 逗号分隔符
24+
*/
25+
String SEPARATOR_COMMA = ",";
1826
}

spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/controller/UserController.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import com.xkcoding.orm.jdbctemplate.service.IUserService;
66
import lombok.extern.slf4j.Slf4j;
77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
1111

1212
/**
1313
* <p>
@@ -37,4 +37,28 @@ public Dict save(@RequestBody User user) {
3737
Boolean save = userService.save(user);
3838
return Dict.create().set("code", save ? 200 : 500).set("msg", save ? "成功" : "失败").set("data", save ? user : null);
3939
}
40+
41+
@DeleteMapping("/user/{id}")
42+
public Dict delete(@PathVariable Long id) {
43+
Boolean delete = userService.delete(id);
44+
return Dict.create().set("code", delete ? 200 : 500).set("msg", delete ? "成功" : "失败");
45+
}
46+
47+
@PutMapping("/user/{id}")
48+
public Dict update(@RequestBody User user, @PathVariable Long id) {
49+
Boolean update = userService.update(user, id);
50+
return Dict.create().set("code", update ? 200 : 500).set("msg", update ? "成功" : "失败").set("data", update ? user : null);
51+
}
52+
53+
@GetMapping("/user/{id}")
54+
public Dict getUser(@PathVariable Long id) {
55+
User user = userService.getUser(id);
56+
return Dict.create().set("code", 200).set("msg", "成功").set("data", user);
57+
}
58+
59+
@GetMapping("/user")
60+
public Dict getUser(User user) {
61+
List<User> userList = userService.getUser(user);
62+
return Dict.create().set("code", 200).set("msg", "成功").set("data", userList);
63+
}
4064
}

spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/orm/jdbctemplate/dao/UserDao.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.jdbc.core.JdbcTemplate;
77
import org.springframework.stereotype.Repository;
88

9+
import java.util.List;
10+
911
/**
1012
* <p>
1113
* User Dao
@@ -20,7 +22,7 @@
2022
* @modified: yangkai.shen
2123
*/
2224
@Repository
23-
public class UserDao extends BaseDao<User> {
25+
public class UserDao extends BaseDao<User, Long> {
2426

2527
@Autowired
2628
public UserDao(JdbcTemplate jdbcTemplate) {
@@ -36,4 +38,45 @@ public UserDao(JdbcTemplate jdbcTemplate) {
3638
public Integer insert(User user) {
3739
return super.insert(user, true);
3840
}
41+
42+
/**
43+
* 根据主键删除用户
44+
*
45+
* @param id 主键id
46+
* @return 操作影响行数
47+
*/
48+
public Integer delete(Long id) {
49+
return super.deleteById(id);
50+
}
51+
52+
/**
53+
* 更新用户
54+
*
55+
* @param user 用户对象
56+
* @param id 主键id
57+
* @return 操作影响行数
58+
*/
59+
public Integer update(User user, Long id) {
60+
return super.updateById(user, id, true);
61+
}
62+
63+
/**
64+
* 根据主键获取用户
65+
*
66+
* @param id 主键id
67+
* @return id对应的用户
68+
*/
69+
public User selectById(Long id) {
70+
return super.findOneById(id);
71+
}
72+
73+
/**
74+
* 根据查询条件获取用户列表
75+
*
76+
* @param user 用户查询条件
77+
* @return 用户列表
78+
*/
79+
public List<User> selectUserList(User user) {
80+
return super.findByExample(user);
81+
}
3982
}

0 commit comments

Comments
 (0)