Skip to content

Commit 1084dc4

Browse files
committed
update jpa-druid
1 parent 642cdad commit 1084dc4

File tree

7 files changed

+117
-13
lines changed

7 files changed

+117
-13
lines changed

spring-boot-jpa-druid/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<description>spring-boot-jpa-druid</description>
1616

1717
<properties>
18+
<druid.version>1.1.20</druid.version>
1819
<java.version>1.8</java.version>
1920
</properties>
2021

@@ -34,13 +35,16 @@
3435
<scope>runtime</scope>
3536
</dependency>
3637

37-
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
3838
<dependency>
3939
<groupId>com.alibaba</groupId>
4040
<artifactId>druid-spring-boot-starter</artifactId>
41-
<version>1.1.20</version>
41+
<version>${druid.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<optional>true</optional>
4247
</dependency>
43-
4448
<dependency>
4549
<groupId>org.springframework.boot</groupId>
4650
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.springcloud.springbootjpadruid.controller;
2+
3+
import com.springcloud.springbootjpadruid.model.UserModel;
4+
import com.springcloud.springbootjpadruid.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Sort;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.List;
10+
11+
@RestController
12+
public class UserController {
13+
14+
@Autowired
15+
UserRepository userRepository;
16+
17+
/**
18+
* 查询用户列表
19+
* @return
20+
*/
21+
@GetMapping("/user")
22+
public List<UserModel> user() {
23+
return userRepository.findAll(Sort.by("id").descending());
24+
}
25+
26+
/**
27+
* 新增或更新用户信息
28+
* @param user
29+
* @return
30+
*/
31+
@PostMapping("/user")
32+
public UserModel user(@RequestBody UserModel user) {
33+
return userRepository.save(user);
34+
}
35+
36+
/**
37+
* 根据id删除用户
38+
* @param id
39+
* @return
40+
*/
41+
@DeleteMapping("/user")
42+
public String deleteUserById(String id) {
43+
userRepository.deleteById(id);
44+
return "success";
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.springcloud.springbootjpadruid.model;
2+
3+
import lombok.Data;
4+
import org.hibernate.annotations.GenericGenerator;
5+
6+
import javax.persistence.*;
7+
8+
/**
9+
* @Date: 2019/9/17 16:49
10+
* @Version: 1.0
11+
* @Desc:
12+
*/
13+
@Entity
14+
@Data
15+
@Table(name = "user")
16+
public class UserModel {
17+
@Id
18+
@GeneratedValue(generator = "paymentableGenerator")
19+
@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
20+
@Column(name = "ID", nullable=false, length=36)
21+
private String id;
22+
@Column(name = "nick_name")
23+
private String nickName;
24+
@Column(nullable = false)
25+
private int age;
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.springcloud.springbootjpadruid.repository;
2+
3+
import com.springcloud.springbootjpadruid.model.UserModel;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @Date: 2019/9/17 16:51
10+
* @Version: 1.0
11+
* @Desc:
12+
*/
13+
public interface UserRepository extends JpaRepository<UserModel, String> {
14+
UserModel getByIdIs(String id);
15+
16+
UserModel findByNickName(String nickName);
17+
18+
int countByAge(int age);
19+
20+
List<UserModel> findByNickNameLike(String nickName);
21+
}

spring-boot-jpa-druid/src/main/resources/application-decrypt.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
spring:
22
datasource:
33
type: com.alibaba.druid.pool.DruidDataSource
4-
url: jdbc:mysql://172.16.96.112:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
4+
url: jdbc:mysql://192.168.0.128:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
55
username: root
66
# 加密后密文,原密码为 123456
7-
password: pJRm0wPfnOkjULnm8tCx5JFQHGCtgSs2MNkwWziN7luLIU2mmc5V3x7nylJEYMSjpKNoawal8Pe7oty0dSKSkg==
7+
password: Y464AerH8tabxQg5DlkUej6gQ64KY73ahgiPyaB0vguLBLjUEEkVu6VBueiXxcnMfVjh1Nbd+lJNUTnS1a3/xg==
88
driverClassName: com.mysql.cj.jdbc.Driver
99
druid:
1010
filter:
1111
config:
1212
enabled: true
13-
connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKo5mHbMnE3/7DHKulE4lEQ/eq1vaWoO75WsydEENVaQwHLBpKO9VcE2aCxLhjkmcuHDwqBaQjMRAYctwmgGyaUCAwEAAQ==
13+
connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIddoZ2mbjHu+mG+KYu6IEgBDDnGr5s+wZaPA2MXoqcXmUIZ6154MXNIAEOjkZaHy1rQ5uku2yO8Hw09hBcv9g8CAwEAAQ==
1414
# 连接池的配置信息
1515
# 初始化时建立物理连接的个数
1616
initial-size: 3
@@ -39,11 +39,13 @@ spring:
3939
# 当值大于0时poolPreparedStatements会自动修改为true
4040
max-pool-prepared-statement-per-connection-size: 20
4141
# 合并多个DruidDataSource的监控数据
42-
use-global-data-source-stat: true
42+
use-global-data-source-stat: false
4343
# 配置扩展插件
4444
filters: stat,wall,slf4j
4545
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
4646
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
47+
# 定时输出统计信息到日志中,并每次输出日志会导致清零(reset)连接池相关的计数器。
48+
time-between-log-stats-millis: 300000
4749
# 配置DruidStatFilter
4850
web-stat-filter:
4951
enabled: true
@@ -63,4 +65,4 @@ spring:
6365
# 登录名
6466
login-username: admin
6567
# 登录密码
66-
login-password: admin
68+
login-password: admin

spring-boot-jpa-druid/src/main/resources/application-pass.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spring:
22
datasource:
33
type: com.alibaba.druid.pool.DruidDataSource
4-
url: jdbc:mysql://172.16.96.112:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
4+
url: jdbc:mysql://192.168.0.128:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
55
username: root
66
password: 123456
77
driverClassName: com.mysql.cj.jdbc.Driver
@@ -34,11 +34,13 @@ spring:
3434
# 当值大于0时poolPreparedStatements会自动修改为true
3535
max-pool-prepared-statement-per-connection-size: 20
3636
# 合并多个DruidDataSource的监控数据
37-
use-global-data-source-stat: true
37+
use-global-data-source-stat: false
3838
# 配置扩展插件
3939
filters: stat,wall,slf4j
4040
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
41-
connect-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
41+
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
42+
# 定时输出统计信息到日志中,并每次输出日志会导致清零(reset)连接池相关的计数器。
43+
time-between-log-stats-millis: 300000
4244
# 配置DruidStatFilter
4345
web-stat-filter:
4446
enabled: true

spring-boot-jpa-druid/src/main/resources/application.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ spring:
99
database: mysql
1010
show-sql: true
1111
generate-ddl: true
12-
database-platform: org.hibernate.dialect.MySQL5Dialect
12+
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
1313
hibernate:
14-
ddl-auto: update
14+
ddl-auto: update
15+
properties:
16+
hibernate:
17+
format_sql: true

0 commit comments

Comments
 (0)