SpringBoot 一些常用配置【持续更新】


Springboot 一些常用配置

工具类

ID生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.obox.obox.Utils;


import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
*
* 各类id生成工具
*/
public class IdUtil {

/**
* 生成用户ID
* @return
*/
public static String Uid (){

String uid = "U" + UUID.randomUUID().toString().hashCode();
return uid ;
}

/**
* 生成商家ID
* @return
*/
public static String Bid(){


String bid = "B" + UUID.randomUUID().toString().hashCode() ;
return bid ;

}

/**
* 生成商品ID
* @return
*/
public static String Gid(){

String gid = "G" + UUID.randomUUID().toString().hashCode() ;
return gid ;

}

/**
* 生成订单ID
* @return
*/
public static String Olid(){


int hashCodeV = UUID.randomUUID().toString().hashCode();
if (hashCodeV < 0){
hashCodeV = - hashCodeV ;
}
return "OL"+ hashCodeV;

}

/**
* 生成红包ID
* @return
*/
public static String Cid(){

String cid = "C" + UUID.randomUUID().toString().hashCode() ;
return cid ;
}

/**
* 生成轮播图ID
* @return
*/
public static String Sid(){

String sid = "S" + UUID.randomUUID().toString().hashCode() ;
return sid ;
}

/**
* 生成主界面小图标ID
* @return
*/
public static String Hid(){

String hid = "H" + UUID.randomUUID().toString().hashCode() ;
return hid ;
}

/**
* 生成二维码小图标
* @return
*/
public static String Qrid(){
int hashCodeV = UUID.randomUUID().toString().hashCode();
if (hashCodeV < 0){
hashCodeV = - hashCodeV ;
}
return "OR"+ hashCodeV;
}


}

图片处理器(只留下图片名)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.obox.obox.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PicUtils {

public static List<String> CutPicStr(String str){


File file = new File(str);
File[] fs = file.listFiles();
List<String> paths = new ArrayList<>() ;
for(File f:fs){

if(!f.isDirectory()) {

paths.add(f.getName()) ;
}
}


return paths;
}
}

时间格式化器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.obox.obox.Utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

public static String getTime(){
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime() ;
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
String s = sdf.format(date) ;

return s ;
}


public static String getTime(Date date){
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
String s = sdf.format(date) ;
return s ;
}

}

二维码生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.obox.obox.service.QR;


import com.alibaba.fastjson.JSON;

/**
*
* 二维码生成服务
*/
public class QRService {

private static String URL = "http://qr.liantu.com/api.php?text=" ;


/**
* 二维码图片链接
* @param id
* @return
*/
public static String GenerateQR(String id){

String OR_Code_Url = URL + id ;

return OR_Code_Url ;

}

}

服务使用

文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.obox.obox.controller.file;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.obox.obox.Utils.IdUtil;
import com.obox.obox.model.token.NoToken;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping(value = "/file")
public class UploadFileController {

private final String ip = "http://118.31.120.92/wechat/";
private final String spath = "/root/local/user/obox/pic/" ;

/**
* 用户头像上传
* @param file
* @return
*/
@PostMapping("uploadusericon")
public Object UserIconUpload(@RequestParam(value = "file",required = true) MultipartFile file) {


String cata = "user/usericon/" + IdUtil.Uid() + "/" ;
String docname = file.getOriginalFilename() ;

if (Objects.isNull(file) || file.isEmpty()) {

return "文件为空,请重新上传";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(spath + cata + docname);
//如果没有files文件夹,则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(spath+cata));
}
//文件写入指定路径
Files.write(path, bytes);

} catch (IOException e) {
e.printStackTrace();
}

Map<String, Object> url = new HashMap<>() ;
url.put("url",ip+cata+docname) ;
return url ;

}

/**
* 商品信息上传
* @param file
* @return
*/
@PostMapping("uploadgoodspic")
public Object GoodspicUpload(@RequestParam(value = "file",required = true) MultipartFile file) {


String cata = "business/goods/" + IdUtil.Gid() + "/";
String docname = file.getOriginalFilename() ;

if (Objects.isNull(file) || file.isEmpty()) {

return "文件为空,请重新上传";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(spath + cata + docname);
//如果没有files文件夹,则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(spath+cata));
}
//文件写入指定路径
Files.write(path, bytes);

} catch (IOException e) {
e.printStackTrace();
}

Map<String, Object> url = new HashMap<>() ;
url.put("url",ip+cata+docname) ;
return url ;

}

/**
* 商品图标上传
* @param file
* @return
*/
@PostMapping("uploadgoodsicon")
public Object GoodsIconUpload(@RequestParam(value = "file",required = true) MultipartFile file) {


String cata = "business/goods/" + IdUtil.Gid() + "/";
String docname = file.getOriginalFilename() ;

if (Objects.isNull(file) || file.isEmpty()) {

return "文件为空,请重新上传";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(spath + cata + docname);
//如果没有files文件夹,则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(spath+cata));
}
//文件写入指定路径
Files.write(path, bytes);

} catch (IOException e) {
e.printStackTrace();
}

Map<String, Object> url = new HashMap<>() ;
url.put("url",ip+cata+docname) ;
return url ;

}

/**
* 商家背景上传
* @param file
* @return
*/
@PostMapping("uploadbusinesspic")
public Object BusinessPicUpload(@RequestParam(value = "file",required = true) MultipartFile file){

String cata = "business/businessman/" + IdUtil.Bid() + "/";
String docname = file.getOriginalFilename() ;

if (Objects.isNull(file) || file.isEmpty()) {

return "文件为空,请重新上传";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(spath + cata + docname);
//如果没有files文件夹,则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(spath+cata));
}
//文件写入指定路径
Files.write(path, bytes);

} catch (IOException e) {
e.printStackTrace();
}

Map<String, Object> url = new HashMap<>() ;
url.put("url",ip+cata+docname) ;
return url ;

}

/**
* 商家logo上传
* @param file
* @return
*/
@PostMapping("uploadbusinesslogo")
public Object BusinessLogoUpload(@RequestParam(value = "file",required = true) MultipartFile file){

String cata = "business/businessman/" + IdUtil.Bid() + "/";
String docname = file.getOriginalFilename() ;

if (Objects.isNull(file) || file.isEmpty()) {

return "文件为空,请重新上传";
}

try {
byte[] bytes = file.getBytes();
Path path = Paths.get(spath + cata + docname);
//如果没有files文件夹,则创建
if (!Files.isWritable(path)) {
Files.createDirectories(Paths.get(spath+cata));
}
//文件写入指定路径
Files.write(path, bytes);

} catch (IOException e) {
e.printStackTrace();
}

Map<String, Object> url = new HashMap<>() ;
url.put("url",ip+cata+docname) ;
return url ;

}


}

地表最强pom => 专治疑难杂症

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.obox</groupId>
<artifactId>obox</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>obox</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>


<!-- 微信支付 -->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>

<!-- Redis -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- RestTemplete -->

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.57</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>


</dependencies>

<!-- 微信支付-->
<licenses>
<license>
<name>The BSD 3-Clause License</name>
<url>https://opensource.org/licenses/BSD-3-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url></url>
<connection></connection>
<developerConnection></developerConnection>
</scm>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

</plugins>
</build>

<developers>
<developer>
<name>wxpay</name>
<email></email>
<url>https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=11_1</url>
</developer>
</developers>

<profiles>
<profile>
<id>release</id>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build>
<plugins>

</plugins>
</build>
</profile>
</profiles>



</project>

地表最强yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
spring:
datasource:
username: root
password: obox11..
url: jdbc:mysql://118.31.120.92:3306/obox
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

http:
encoding:
charset: UTF-8
cache:
type: redis

redis:
host: 118.31.120.92
port: 6379
database: 0
timeout: 60s

jedis:
pool:
max-active: -1
max-idle: 50
max-wait: -1s



initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true


server:
port: 8080