Skip to content

Commit 5a75fed

Browse files
committed
add:增加 JPA 案例
1 parent aef04a0 commit 5a75fed

File tree

13 files changed

+483
-20
lines changed

13 files changed

+483
-20
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
*.iml
44
out
55
gen
6-
SE/io/
7-
spring/src/test/
8-
spring/target/
6+
*/target

pom.xml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
<modules>
1919
<module>spring-boot-mybatis-plus</module>
2020
<module>java-api</module>
21+
<module>spring-boot-jpa</module>
2122
</modules>
2223

24+
<!-- 依赖版本管理 -->
2325
<properties>
24-
<junit.version>4.12</junit.version>
25-
<slf4j.version>1.7.26</slf4j.version>
2626
<mysql.version>5.1.48</mysql.version>
2727
<guava.version>28.1-jre</guava.version>
28-
<hutool.version>5.0.7</hutool.version>
28+
<hutool.version>5.1.0</hutool.version>
2929
<hibernate.validator.version>6.1.0.Final</hibernate.validator.version>
3030
</properties>
3131

32+
<!-- 统一依赖管理 -->
3233
<dependencyManagement>
3334
<dependencies>
3435
<dependency>
@@ -75,7 +76,13 @@
7576
</dependencies>
7677
</dependencyManagement>
7778

79+
<!-- 父工程默认依赖 -->
7880
<dependencies>
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-web</artifactId>
84+
</dependency>
85+
7986
<dependency>
8087
<groupId>org.springframework.boot</groupId>
8188
<artifactId>spring-boot-configuration-processor</artifactId>
@@ -94,16 +101,6 @@
94101
</exclusions>
95102
</dependency>
96103

97-
<dependency>
98-
<groupId>org.slf4j</groupId>
99-
<artifactId>slf4j-api</artifactId>
100-
</dependency>
101-
102-
<dependency>
103-
<groupId>org.slf4j</groupId>
104-
<artifactId>slf4j-log4j12</artifactId>
105-
</dependency>
106-
107104
<dependency>
108105
<groupId>mysql</groupId>
109106
<artifactId>mysql-connector-java</artifactId>

spring-boot-jpa/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>JavaStudy</artifactId>
7+
<groupId>com.chachae</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-boot-jpa</artifactId>
13+
14+
<dependencies>
15+
<!-- spring-boot-starter-data-jpa -->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-data-jpa</artifactId>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chachae;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author chachae
8+
* @since 2020/1/7 22:54
9+
*/
10+
@SpringBootApplication
11+
public class JpaApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(JpaApplication.class);
15+
}
16+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.chachae.controller;
2+
3+
import com.chachae.entity.User;
4+
import com.chachae.service.UserService;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import javax.annotation.Resource;
9+
import java.util.List;
10+
11+
/**
12+
* @author chachae
13+
* @since 2020/1/7 23:15
14+
*/
15+
@RestController
16+
@RequestMapping("/user")
17+
public class UserController {
18+
19+
@Resource private UserService userService;
20+
21+
@GetMapping("/list")
22+
public ResponseEntity<List<User>> listAll() {
23+
List<User> users = this.userService.listAll();
24+
return ResponseEntity.ok(users);
25+
}
26+
27+
@GetMapping("/{id}")
28+
public ResponseEntity<User> listAll(@PathVariable Long id) {
29+
User user = this.userService.getUserById(id);
30+
return ResponseEntity.ok(user);
31+
}
32+
33+
@PostMapping("/save")
34+
public ResponseEntity<Long> saveUser(User user) {
35+
Long id = this.userService.insert(user);
36+
return ResponseEntity.ok(id);
37+
}
38+
39+
@DeleteMapping("/delete/{id}")
40+
public ResponseEntity<Long> saveUser(@PathVariable Long id) {
41+
id = this.userService.remove(id);
42+
return ResponseEntity.ok(id);
43+
}
44+
45+
@PutMapping("/update")
46+
public ResponseEntity<Long> updateUser(User user) {
47+
Long id = this.userService.update(user);
48+
return ResponseEntity.ok(id);
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chachae.entity;
2+
3+
import lombok.Data;
4+
5+
import javax.persistence.*;
6+
7+
/**
8+
* @author chachae
9+
* @since 2020/1/7 23:02
10+
*/
11+
@Data
12+
@Entity
13+
@Table(name = "t_user")
14+
public class User {
15+
16+
@Id
17+
// 主键生成策略:MYSQL自增ID
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private Long id;
20+
21+
private String userName;
22+
23+
private String password;
24+
25+
private Integer admin;
26+
27+
private Integer status;
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.chachae.exceptions;
2+
3+
import com.google.common.collect.Maps;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.RestControllerAdvice;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import java.util.Map;
10+
11+
/**
12+
* 全局异常处理
13+
*
14+
* @author chachae
15+
* @since 2020/1/7 23:29
16+
*/
17+
@RestControllerAdvice
18+
public class GlobalExceptionHandler {
19+
20+
private Map<String, Object> resMap = Maps.newHashMap();
21+
22+
/**
23+
* 处理运行时异常
24+
*
25+
* @param e 异常
26+
* @param request 前台请求
27+
* @return 返回异常信息
28+
*/
29+
@ExceptionHandler(RuntimeException.class)
30+
public ResponseEntity<Map<String, Object>> handlerRuntimeException(
31+
RuntimeException e, HttpServletRequest request) {
32+
resMap.put("msg", e.getMessage());
33+
resMap.put("url", request.getRequestURI());
34+
return ResponseEntity.ok(resMap);
35+
}
36+
37+
/**
38+
* 处理Exception 异常
39+
*
40+
* @param e 异常
41+
* @param request 前台请求
42+
* @return 返回异常信息
43+
*/
44+
@ExceptionHandler(Exception.class)
45+
public ResponseEntity<Map<String, Object>> handlerException(
46+
Exception e, HttpServletRequest request) {
47+
resMap.put("msg", e.getMessage());
48+
resMap.put("url", request.getRequestURI());
49+
return ResponseEntity.ok(resMap);
50+
}
51+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.chachae.repository;
2+
3+
import com.chachae.entity.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
/**
8+
* @author chachae
9+
* @since 2020/1/7 23:07
10+
*/
11+
@Repository("userDAO")
12+
public interface UserDAO extends JpaRepository<User, Long> {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chachae.service;
2+
3+
import com.chachae.entity.User;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author chachae
9+
* @since 2020/1/7 23:09
10+
*/
11+
public interface UserService {
12+
13+
/**
14+
* 查询全部
15+
*
16+
* @return List<User>
17+
*/
18+
List<User> listAll();
19+
20+
/**
21+
* 通过id 查询成员
22+
*
23+
* @param id 成员id
24+
* @return User
25+
*/
26+
User getUserById(Long id);
27+
28+
Long insert(User user);
29+
30+
Long remove(Long id);
31+
32+
Long update(User user);
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.chachae.service.impl;
2+
3+
import cn.hutool.core.util.ObjectUtil;
4+
import com.chachae.entity.User;
5+
import com.chachae.repository.UserDAO;
6+
import com.chachae.service.UserService;
7+
import org.springframework.stereotype.Service;
8+
9+
import javax.annotation.Resource;
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
/**
14+
* @author chachae
15+
* @since 2020/1/7 23:10
16+
*/
17+
@Service("userService")
18+
public class UserServiceImpl implements UserService {
19+
20+
@Resource private UserDAO userDAO;
21+
22+
@Override
23+
public List<User> listAll() {
24+
List<User> list = this.userDAO.findAll();
25+
if (ObjectUtil.isNotEmpty(list)) {
26+
list.forEach(entity -> entity.setPassword(null));
27+
return list;
28+
}
29+
throw new RuntimeException("用户不存在");
30+
}
31+
32+
@Override
33+
public User getUserById(Long id) {
34+
Optional<User> res = this.userDAO.findById(id);
35+
if (res.isPresent()) {
36+
User entity = res.get();
37+
entity.setPassword(null);
38+
return entity;
39+
}
40+
throw new RuntimeException("用户不存在");
41+
}
42+
43+
@Override
44+
public Long insert(User user) {
45+
User res = this.userDAO.save(user);
46+
return res.getId();
47+
}
48+
49+
@Override
50+
public Long remove(Long id) {
51+
this.userDAO.deleteById(id);
52+
return id;
53+
}
54+
55+
@Override
56+
public Long update(User user) {
57+
User res = this.userDAO.save(user);
58+
return res.getId();
59+
}
60+
}

0 commit comments

Comments
 (0)