Skip to content

Commit a974f7a

Browse files
committed
springboot-jdbc-templete init
1 parent 4d6e799 commit a974f7a

File tree

19 files changed

+403
-0
lines changed

19 files changed

+403
-0
lines changed
50.3 KB
Loading
113 KB
Loading
95 KB
Loading
112 KB
Loading
114 KB
Loading
87 KB
Loading
20.8 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Navicat Premium Data Transfer
3+
4+
Source Server : localhost
5+
Source Server Type : MySQL
6+
Source Server Version : 50713
7+
Source Host : localhost:3306
8+
Source Schema : springboot-jdbc-templete
9+
10+
Target Server Type : MySQL
11+
Target Server Version : 50713
12+
File Encoding : 65001
13+
14+
Date: 15/06/2019 13:37:46
15+
*/
16+
17+
SET NAMES utf8mb4;
18+
SET FOREIGN_KEY_CHECKS = 0;
19+
20+
-- ----------------------------
21+
-- Table structure for tb_user
22+
-- ----------------------------
23+
DROP TABLE IF EXISTS `tb_user`;
24+
CREATE TABLE `tb_user` (
25+
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
26+
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
27+
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
28+
`ctime` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
29+
PRIMARY KEY (`id`) USING BTREE
30+
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
31+
32+
-- ----------------------------
33+
-- Records of tb_user
34+
-- ----------------------------
35+
INSERT INTO `tb_user` VALUES (4, 'tellsea', '654321', '2019-06-15 13:23:22');
36+
INSERT INTO `tb_user` VALUES (5, 'tellsea', '123456', '2019-06-15 13:24:33');
37+
INSERT INTO `tb_user` VALUES (6, 'tellsea', '123456', '2019-06-15 13:25:35');
38+
INSERT INTO `tb_user` VALUES (7, 'tellsea', '123456', '2019-06-15 13:27:23');
39+
INSERT INTO `tb_user` VALUES (8, 'tellsea', '123456', '2019-06-15 13:27:23');
40+
41+
SET FOREIGN_KEY_CHECKS = 1;

springboot-jdbc-templete/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-jdbc-templete</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-jdbc-templete</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<!-- spring boot -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
34+
<!-- jdbc -->
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-jdbc</artifactId>
38+
</dependency>
39+
40+
<!-- 数据库驱动,我是MySQL5.7,所以使用5.1.x的驱动,如果你是MySQL8,则改成8.0.x的版本 -->
41+
<dependency>
42+
<groupId>mysql</groupId>
43+
<artifactId>mysql-connector-java</artifactId>
44+
<version>5.1.46</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.projectlombok</groupId>
49+
<artifactId>lombok</artifactId>
50+
<version>1.18.8</version>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootJdbcTempleteApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootJdbcTempleteApplication.class, args);
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.tellsea.bean;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.Date;
8+
9+
@Data
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class User {
13+
14+
private Long id;
15+
16+
private String username;
17+
18+
private String password;
19+
20+
private Date ctime;
21+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cn.tellsea.controller;
2+
3+
import cn.tellsea.bean.User;
4+
import cn.tellsea.service.UserService;
5+
import cn.tellsea.utils.ResponseResult;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("/user")
13+
public class UserController {
14+
15+
@Autowired
16+
private UserService userService;
17+
18+
/**
19+
* 根据id查询用户
20+
*
21+
* @param id
22+
* @return
23+
*/
24+
@GetMapping("/get/{id}")
25+
public ResponseResult getUserById(@PathVariable(value = "id") Long id) {
26+
User user = userService.getUserById(id);
27+
if (user == null) {
28+
return new ResponseResult(200, "查询结果为空", null);
29+
}
30+
return new ResponseResult(200, "查询成功", user);
31+
}
32+
33+
/**
34+
* 查询用户列表
35+
*
36+
* @return
37+
*/
38+
@GetMapping("/list")
39+
public ResponseResult getUserList() {
40+
List<User> list = userService.getUserList();
41+
if (list == null || list.isEmpty()) {
42+
return new ResponseResult(200, "查询结果为空", null);
43+
}
44+
return new ResponseResult(200, "查询成功", list);
45+
}
46+
47+
/**
48+
* 新增用户
49+
*
50+
* @param user
51+
* @return
52+
*/
53+
@PostMapping("/add")
54+
public ResponseResult add(@RequestBody User user) {
55+
int count = userService.add(user);
56+
if (count == 0) {
57+
return new ResponseResult(500, "新增失败", null);
58+
}
59+
return new ResponseResult(200, "新增成功", null);
60+
}
61+
62+
/**
63+
* 根据id删除用户
64+
*
65+
* @param id
66+
* @return
67+
*/
68+
@GetMapping("/delete/{id}")
69+
public ResponseResult delete(@PathVariable(value = "id") Long id) {
70+
int count = userService.delete(id);
71+
if (count == 0) {
72+
return new ResponseResult(500, "删除失败", null);
73+
}
74+
return new ResponseResult(200, "删除成功", null);
75+
}
76+
77+
/**
78+
* 根据id修改用户信息
79+
*
80+
* @param user
81+
* @return
82+
*/
83+
@PostMapping("/update")
84+
public ResponseResult update(@RequestBody User user) {
85+
int count = userService.update(user);
86+
if (count == 0) {
87+
return new ResponseResult(500, "更新失败", null);
88+
}
89+
return new ResponseResult(200, "更新成功", null);
90+
}
91+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.tellsea.dao;
2+
3+
import cn.tellsea.bean.User;
4+
5+
import java.util.List;
6+
7+
public interface UserDao {
8+
9+
User getUserById(Long id);
10+
11+
List<User> getUserList();
12+
13+
int add(User user);
14+
15+
int update(User user);
16+
17+
int delete(Long id);
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.tellsea.dao.impl;
2+
3+
import cn.tellsea.bean.User;
4+
import cn.tellsea.dao.UserDao;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.BeanPropertyRowMapper;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.util.Date;
11+
import java.util.List;
12+
13+
@Repository // 用于标注数据访问组件
14+
public class UserDaoImpl implements UserDao {
15+
16+
@Autowired
17+
private JdbcTemplate jdbcTemplate;
18+
19+
@Override
20+
public User getUserById(Long id) {
21+
List<User> list = jdbcTemplate.query("select * from tb_user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
22+
if (list != null && list.size() > 0) {
23+
return list.get(0);
24+
} else {
25+
return null;
26+
}
27+
}
28+
29+
@Override
30+
public List<User> getUserList() {
31+
List<User> list = jdbcTemplate.query("select * from tb_user", new Object[]{}, new BeanPropertyRowMapper(User.class));
32+
if (list != null && list.size() > 0) {
33+
return list;
34+
} else {
35+
return null;
36+
}
37+
}
38+
39+
@Override
40+
public int add(User user) {
41+
return jdbcTemplate.update("insert into tb_user(username, password, ctime) values(?, ?, ?)",
42+
user.getUsername(), user.getPassword(), new Date());
43+
}
44+
45+
@Override
46+
public int update(User user) {
47+
return jdbcTemplate.update("update tb_user SET username = ? , password = ? WHERE id=?",
48+
user.getUsername(), user.getPassword(), user.getId());
49+
}
50+
51+
@Override
52+
public int delete(Long id) {
53+
return jdbcTemplate.update("delete from tb_user where id = ? ", id);
54+
}
55+
56+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.tellsea.service;
2+
3+
import cn.tellsea.bean.User;
4+
5+
import java.util.List;
6+
7+
public interface UserService {
8+
9+
User getUserById(Long id);
10+
11+
List<User> getUserList();
12+
13+
int add(User user);
14+
15+
int update(User user);
16+
17+
int delete(Long id);
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.bean.User;
4+
import cn.tellsea.dao.UserDao;
5+
import cn.tellsea.service.UserService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class UserServiceImpl implements UserService {
13+
14+
@Autowired
15+
private UserDao userDao;
16+
17+
@Override
18+
public User getUserById(Long id) {
19+
return userDao.getUserById(id);
20+
}
21+
22+
@Override
23+
public List<User> getUserList() {
24+
return userDao.getUserList();
25+
}
26+
27+
@Override
28+
public int add(User user) {
29+
return userDao.add(user);
30+
}
31+
32+
@Override
33+
public int update(User user) {
34+
return userDao.update(user);
35+
}
36+
37+
@Override
38+
public int delete(Long id) {
39+
return userDao.delete(id);
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.tellsea.utils;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class ResponseResult {
11+
12+
private Integer code;
13+
14+
private String msg;
15+
16+
private Object data;
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server:
2+
port: 8080
3+
spring:
4+
datasource:
5+
driver-class-name: com.mysql.jdbc.Driver
6+
url: jdbc:mysql://localhost:3306/springboot-jdbc-templete?useSSL=false
7+
username: root
8+
password: 123456

0 commit comments

Comments
 (0)