Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
4524f2df
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
4524f2df
编写于
12月 20, 2022
作者:
Q
qinyingjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:批处理
上级
f2a56f4c
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
335 addition
and
49 deletion
+335
-49
READ.md
READ.md
+6
-0
pom.xml
pom.xml
+11
-0
src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java
...va/com/kwan/springbootkwan/SpringBootKwanApplication.java
+2
-0
src/main/java/com/kwan/springbootkwan/config/CsvBatchJobConfig.java
...ava/com/kwan/springbootkwan/config/CsvBatchJobConfig.java
+83
-0
src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
...n/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
+110
-5
src/main/java/com/kwan/springbootkwan/controller/PersonController.java
.../com/kwan/springbootkwan/controller/PersonController.java
+21
-3
src/main/java/com/kwan/springbootkwan/controller/UserController.java
...va/com/kwan/springbootkwan/controller/UserController.java
+22
-0
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
+7
-6
src/main/java/com/kwan/springbootkwan/entity/Person.java
src/main/java/com/kwan/springbootkwan/entity/Person.java
+13
-35
src/main/java/com/kwan/springbootkwan/entity/Student.java
src/main/java/com/kwan/springbootkwan/entity/Student.java
+37
-0
src/main/resources/ValidationMessages.properties
src/main/resources/ValidationMessages.properties
+5
-0
src/main/resources/application.yaml
src/main/resources/application.yaml
+13
-0
src/main/resources/data.csv
src/main/resources/data.csv
+5
-0
未找到文件。
READ.md
浏览文件 @
4524f2df
...
...
@@ -27,6 +27,12 @@
-
web模块
-
发邮件
-
数据校验规则
-
乱码问题 WebMvcConfigurationSupport
-
批处理
## 三.链接
...
...
pom.xml
浏览文件 @
4524f2df
...
...
@@ -15,6 +15,9 @@
<description>
Demo project for Spring Boot
</description>
<properties>
<java.version>
1.8
</java.version>
<!-- 与1.8配置在一起,设置编码集-->
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.reporting.outputEncoding>
UTF-8
</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
...
...
@@ -113,6 +116,14 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-mail
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-validation
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-batch
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
...
...
src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java
浏览文件 @
4524f2df
package
com.kwan.springbootkwan
;
import
org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
...
...
@@ -6,6 +7,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
@EnableBatchProcessing
public
class
SpringBootKwanApplication
{
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/com/kwan/springbootkwan/config/CsvBatchJobConfig.java
0 → 100644
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.config
;
import
com.kwan.springbootkwan.entity.Person
;
import
org.springframework.batch.core.Job
;
import
org.springframework.batch.core.Step
;
import
org.springframework.batch.core.configuration.annotation.JobBuilderFactory
;
import
org.springframework.batch.core.configuration.annotation.StepBuilderFactory
;
import
org.springframework.batch.core.configuration.annotation.StepScope
;
import
org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider
;
import
org.springframework.batch.item.database.JdbcBatchItemWriter
;
import
org.springframework.batch.item.file.FlatFileItemReader
;
import
org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper
;
import
org.springframework.batch.item.file.mapping.DefaultLineMapper
;
import
org.springframework.batch.item.file.transform.DelimitedLineTokenizer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.io.ClassPathResource
;
import
javax.sql.DataSource
;
@Configuration
public
class
CsvBatchJobConfig
{
@Autowired
JobBuilderFactory
jobBuilderFactory
;
@Autowired
StepBuilderFactory
stepBuilderFactory
;
@Autowired
DataSource
dataSource
;
@Bean
@StepScope
FlatFileItemReader
<
Person
>
itemReader
()
{
FlatFileItemReader
<
Person
>
reader
=
new
FlatFileItemReader
<>();
reader
.
setLinesToSkip
(
1
);
reader
.
setResource
(
new
ClassPathResource
(
"data.csv"
));
reader
.
setLineMapper
(
new
DefaultLineMapper
<
Person
>()
{{
setLineTokenizer
(
new
DelimitedLineTokenizer
()
{
{
setNames
(
"id"
,
"username"
,
"address"
,
"gender"
);
//配置列与列之间的间隔符(将通过间隔符对每一行的数据进行切分),最后设置要映射的实体类属性即可
setDelimiter
(
","
);
}
});
setFieldSetMapper
(
new
BeanWrapperFieldSetMapper
<
Person
>()
{
{
setTargetType
(
Person
.
class
);
}
});
}});
return
reader
;
}
@Bean
public
JdbcBatchItemWriter
jdbcBatchItemWriter
()
{
JdbcBatchItemWriter
writer
=
new
JdbcBatchItemWriter
();
writer
.
setDataSource
(
dataSource
);
writer
.
setSql
(
"insert into person(id, username,address,gender)"
+
"values (:id, :username, :address, :gender)"
);
writer
.
setItemSqlParameterSourceProvider
(
new
BeanPropertyItemSqlParameterSourceProvider
<>());
return
writer
;
}
@Bean
Step
csvStep
()
{
return
stepBuilderFactory
.
get
(
"csvStep"
)
.<
Person
,
Person
>
chunk
(
2
)
.
reader
(
itemReader
())
.
writer
(
jdbcBatchItemWriter
())
.
build
();
}
@Bean
Job
csvJob
()
{
return
jobBuilderFactory
.
get
(
"csvJob"
)
.
start
(
csvStep
())
.
build
();
}
}
src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.config
;
import
org.springframework.boot.validation.MessageInterpolatorFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.context.support.ResourceBundleMessageSource
;
import
org.springframework.format.FormatterRegistry
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.StringHttpMessageConverter
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
org.springframework.validation.MessageCodesResolver
;
import
org.springframework.validation.Validator
;
import
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
import
org.springframework.web.method.support.HandlerMethodReturnValueHandler
;
import
org.springframework.web.servlet.HandlerExceptionResolver
;
import
org.springframework.web.servlet.config.annotation.*
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
/**
* 全局webconfig-跨域配置
...
...
@@ -14,7 +27,49 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* @date : 2022/12/19 16:11
*/
@Configuration
public
class
MyWebMvcConfig
implements
WebMvcConfigurer
{
public
class
MyWebMvcConfig
extends
WebMvcConfigurationSupport
implements
WebMvcConfigurer
{
@Override
public
void
extendMessageConverters
(
List
<
HttpMessageConverter
<?>>
converters
)
{
// 解决controller返回字符串中文乱码问题
for
(
HttpMessageConverter
<?>
converter
:
converters
)
{
if
(
converter
instanceof
StringHttpMessageConverter
)
{
((
StringHttpMessageConverter
)
converter
).
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
}
else
if
(
converter
instanceof
MappingJackson2HttpMessageConverter
)
{
((
MappingJackson2HttpMessageConverter
)
converter
).
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
}
}
}
@Override
public
void
configureHandlerExceptionResolvers
(
List
<
HandlerExceptionResolver
>
resolvers
)
{
WebMvcConfigurer
.
super
.
configureHandlerExceptionResolvers
(
resolvers
);
}
@Override
public
void
extendHandlerExceptionResolvers
(
List
<
HandlerExceptionResolver
>
resolvers
)
{
WebMvcConfigurer
.
super
.
extendHandlerExceptionResolvers
(
resolvers
);
}
@Override
public
Validator
getValidator
()
{
ResourceBundleMessageSource
messageSource
=
new
ResourceBundleMessageSource
();
messageSource
.
setDefaultEncoding
(
"utf-8"
);
// 读取配置文件的编码格式
messageSource
.
setCacheMillis
(-
1
);
// 缓存时间,-1表示不过期
messageSource
.
setBasename
(
"ValidationMessages"
);
// 配置文件前缀名,设置为Messages,那你的配置文件必须以Messages.properties/Message_en.properties...
LocalValidatorFactoryBean
factoryBean
=
new
LocalValidatorFactoryBean
();
MessageInterpolatorFactory
interpolatorFactory
=
new
MessageInterpolatorFactory
();
factoryBean
.
setMessageInterpolator
(
interpolatorFactory
.
getObject
());
factoryBean
.
setValidationMessageSource
(
messageSource
);
return
factoryBean
;
}
@Override
public
MessageCodesResolver
getMessageCodesResolver
()
{
return
WebMvcConfigurer
.
super
.
getMessageCodesResolver
();
}
/**
* 配置静态资源
*
...
...
@@ -26,6 +81,31 @@ public class MyWebMvcConfig implements WebMvcConfigurer {
.
addResourceLocations
(
"classpath:/static/"
);
}
@Override
public
void
configurePathMatch
(
PathMatchConfigurer
configurer
)
{
WebMvcConfigurer
.
super
.
configurePathMatch
(
configurer
);
}
@Override
public
void
configureContentNegotiation
(
ContentNegotiationConfigurer
configurer
)
{
WebMvcConfigurer
.
super
.
configureContentNegotiation
(
configurer
);
}
@Override
public
void
configureAsyncSupport
(
AsyncSupportConfigurer
configurer
)
{
WebMvcConfigurer
.
super
.
configureAsyncSupport
(
configurer
);
}
@Override
public
void
configureDefaultServletHandling
(
DefaultServletHandlerConfigurer
configurer
)
{
WebMvcConfigurer
.
super
.
configureDefaultServletHandling
(
configurer
);
}
@Override
public
void
addFormatters
(
FormatterRegistry
registry
)
{
WebMvcConfigurer
.
super
.
addFormatters
(
registry
);
}
/**
* 配置拦截器
*
...
...
@@ -51,4 +131,29 @@ public class MyWebMvcConfig implements WebMvcConfigurer {
.
maxAge
(
1800
)
.
allowedOrigins
(
"http://localhost:8081"
);
}
@Override
public
void
addViewControllers
(
ViewControllerRegistry
registry
)
{
WebMvcConfigurer
.
super
.
addViewControllers
(
registry
);
}
@Override
public
void
configureViewResolvers
(
ViewResolverRegistry
registry
)
{
WebMvcConfigurer
.
super
.
configureViewResolvers
(
registry
);
}
@Override
public
void
addArgumentResolvers
(
List
<
HandlerMethodArgumentResolver
>
resolvers
)
{
WebMvcConfigurer
.
super
.
addArgumentResolvers
(
resolvers
);
}
@Override
public
void
addReturnValueHandlers
(
List
<
HandlerMethodReturnValueHandler
>
handlers
)
{
WebMvcConfigurer
.
super
.
addReturnValueHandlers
(
handlers
);
}
@Override
public
void
configureMessageConverters
(
List
<
HttpMessageConverter
<?>>
converters
)
{
WebMvcConfigurer
.
super
.
configureMessageConverters
(
converters
);
}
}
src/main/java/com/kwan/springbootkwan/controller/PersonController.java
浏览文件 @
4524f2df
...
...
@@ -5,6 +5,9 @@ import com.kwan.springbootkwan.entity.User;
import
com.kwan.springbootkwan.service.IUserService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.batch.core.Job
;
import
org.springframework.batch.core.JobParameters
;
import
org.springframework.batch.core.launch.JobLauncher
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -22,6 +25,12 @@ import java.util.List;
@RestController
@RequestMapping
(
"/person"
)
public
class
PersonController
{
@Autowired
private
JobLauncher
jobLauncher
;
@Autowired
private
Job
job
;
/**
* {
* "name": "zhang san",
...
...
@@ -33,9 +42,18 @@ public class PersonController {
@GetMapping
(
"/person"
)
public
Person
person
()
{
Person
person
=
new
Person
();
person
.
set
N
ame
(
"zhang san"
);
person
.
set
Age
(
24
);
person
.
set
Birthday
(
new
Date
()
);
person
.
set
Usern
ame
(
"zhang san"
);
person
.
set
Id
(
24
);
person
.
set
Address
(
"湖北"
);
return
person
;
}
@GetMapping
(
"/hello"
)
public
void
hello
()
{
try
{
jobLauncher
.
run
(
job
,
new
JobParameters
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/controller/UserController.java
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.controller
;
import
com.kwan.springbootkwan.entity.Student
;
import
com.kwan.springbootkwan.entity.User
;
import
com.kwan.springbootkwan.service.IUserService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.ObjectError
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.UnsupportedEncodingException
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -38,4 +46,18 @@ public class UserController {
public
User
getUserById
(
@PathVariable
Integer
id
)
{
return
userService
.
getUserById
(
id
);
}
@PostMapping
(
value
=
"/student"
,
produces
=
"application/json;charset=utf-8"
)
public
List
<
String
>
addUser
(
@RequestBody
@Validated
Student
student
,
BindingResult
result
)
throws
UnsupportedEncodingException
{
List
<
String
>
errors
=
new
ArrayList
<>();
if
(
result
.
hasErrors
())
{
List
<
ObjectError
>
allErrors
=
result
.
getAllErrors
();
for
(
ObjectError
error
:
allErrors
)
{
errors
.
add
(
error
.
getDefaultMessage
());
}
}
log
.
info
(
"errors={}"
,
errors
);
return
errors
;
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.entity
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.util.Map
;
...
...
@@ -13,16 +14,16 @@ import java.util.Map;
*/
@Data
public
class
MailInfo
{
// 邮件接收人
@ApiModelProperty
(
value
=
"邮件接收人"
)
private
String
[]
receiver
;
// 邮件主题
@ApiModelProperty
(
value
=
"邮件主题"
)
private
String
subject
;
// 邮件的文本内容
@ApiModelProperty
(
value
=
"邮件的文本内容"
)
private
String
content
;
// 抄送人
@ApiModelProperty
(
value
=
"抄送人"
)
private
String
[]
cc
;
// 邮件附件的文件名
@ApiModelProperty
(
value
=
"邮件附件的文件名"
)
private
String
[]
attachFileNames
;
// 邮件内容内嵌图片
@ApiModelProperty
(
value
=
"邮件内容内嵌图片"
)
private
Map
<
String
,
String
>
imageMap
;
}
src/main/java/com/kwan/springbootkwan/entity/Person.java
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.entity
;
import
com.
fasterxml.jackson.annotation.JsonFormat
;
import
com.
baomidou.mybatisplus.annotation.TableName
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.util.Date
;
@Data
@TableName
(
value
=
"person"
)
@ApiModel
(
value
=
"用户基本信息"
)
public
class
Person
{
@ApiModelProperty
(
value
=
"姓名"
)
private
String
name
;
@ApiModelProperty
(
value
=
"年龄"
)
private
Integer
age
;
@ApiModelProperty
(
value
=
"生日"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
)
private
Date
birthday
;
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
Integer
getAge
()
{
return
age
;
}
public
void
setAge
(
Integer
age
)
{
this
.
age
=
age
;
}
public
Date
getBirthday
()
{
return
birthday
;
}
public
void
setBirthday
(
Date
birthday
)
{
this
.
birthday
=
birthday
;
}
}
@ApiModelProperty
(
value
=
"主键"
)
private
Integer
id
;
@ApiModelProperty
(
value
=
"名称"
)
private
String
username
;
@ApiModelProperty
(
value
=
"地址"
)
private
String
address
;
@ApiModelProperty
(
value
=
"性别"
)
private
String
gender
;
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/Student.java
0 → 100644
浏览文件 @
4524f2df
package
com.kwan.springbootkwan.entity
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
javax.validation.constraints.*
;
/**
* 数据校验
*
* @author : qinyingjie
* @version : 2.2.0
* @Size 示一个字符串的长度或者一个集合的大小,必须在某一个范围中; min 参数表示范围的下限; max参数表示范围的上限; message 表示校验失败时的提示信息。
* @NotNull 注解表示该字段不能为空。
* @DecimalMin 注解表示对应属性值的下限,@DecimalMax 注解表示对应属性值的上限
* @Email 注解表示对应属性格式是一个 Email
* @date : 2022/12/20 09:00
*/
@Data
public
class
Student
{
@ApiModelProperty
(
value
=
"主键"
)
private
Integer
id
;
@ApiModelProperty
(
value
=
"姓名"
)
@Size
(
min
=
5
,
max
=
10
,
message
=
"{user.name.size}"
)
private
String
name
;
@ApiModelProperty
(
value
=
"地址"
)
@NotNull
(
message
=
"{user.address.notnull}"
)
private
String
address
;
@ApiModelProperty
(
value
=
"年龄"
)
@DecimalMin
(
value
=
"1"
,
message
=
"{user.age.size}"
)
@DecimalMax
(
value
=
"200"
,
message
=
"{user.age.size}"
)
private
Integer
age
;
@ApiModelProperty
(
value
=
"邮箱"
)
@Email
(
message
=
"{user.email.pattern}"
)
@NotNull
(
message
=
"{user.email.notnull}"
)
private
String
email
;
}
src/main/resources/ValidationMessages.properties
0 → 100644
浏览文件 @
4524f2df
user.name.size
=
用户名的长度在5到10个字符之间
user.address.notnull
=
用户地址不能为空
user.age.size
=
年龄输入不正确
user.email.notnull
=
邮箱不能为空
user.email.pattern
=
不正确的电子邮件格式
\ No newline at end of file
src/main/resources/application.yaml
浏览文件 @
4524f2df
server
:
port
:
8761
servlet
:
encoding
:
force
:
true
charset
:
UTF-8
enabled
:
true
swagger
:
enable
:
true
#兼容swagger配置
spring
:
#项目启动时创建数据表的 SQL 脚本,该脚本由 Spring Batch 提供
#spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
# 在项目启动时执行建表 SQL
batch
:
initialize-schema
:
always
# 禁止 Spring Batch 自动执行,在 SpringBoot 中,默认情况,当项目启动时就会执行配置好的批理操作,添加了该配置后则不会自动执行,而需要用户手动触发执行
job
:
enabled
:
false
mail
:
host
:
smtp.qq.com
# 发送者的邮箱账号
...
...
src/main/resources/data.csv
0 → 100644
浏览文件 @
4524f2df
id,username,address,gender
1,张三,成都,男
2,李四,深圳,女
3,张三,成都,男
4,李四,深圳,女
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录