目录结构
* ├──├── Readme.md
* ├── main // 单体项目
* │ ├──java // java文件目录,忽略顶层包结构
* │ │ ├──config // 存放项目的配置文件,如Spring配置、数据库配置等
* │ │ ├──constant // 存放项目中使用到的常量信息,如枚举类型、常量类等。
* │ │ ├──controller // 存放控制器类,负责接收请求并调用相应的服务进行处理。
* │ │ ├──domain // 存放实体类。
* │ │ │ ├──dto // 存放Dto实体类。
* │ │ │ ├──entity // 存放Entity实体类。
* │ │ │ ├──vo // 存放Vo实体类。
* │ │ ├──job // 存放定时任务相关的类,用于定时执行特定的任务。
* │ │ ├──mapper // 存放访问数据层的接口文件,用于实现数据库操作。
* │ │ ├──service // 存放服务层相关的类,负责实现业务逻辑。
* │ │ │ ├──impl // 存放服务层接口的具体实现类。
* │ │ ├──util // 存放工具类,包含项目中通用的工具方法。
* │ ├──resource // 资源目录文件目录。
* │ │ ├──mapper // 存放MyBatis等持久层框架的映射文件,描述数据库表与Java对象之间的映射关系。
* ├── test // 单元测试目录
Rule 1. controller 编写规则
- Cntroller文件中只能引用Service文件,不可引用任何Mapper文件
- Controller文件中只处理参数校验、实体封装和转化、Service方法调用、异常捕捉、日志记录以及权限控制,不可参与其他业务逻辑处理
Rule 2. service 编写规则
- Service文件中只能引用自己的Mapper文件和其他Service文件,不可引用其他的Mapper文件,且Service文件中不可进行循环依赖
- Service文件中主要进行业务逻辑处理、事务管理、异常捕捉以及Service和Mapper方法调用等
- Service文件处理异常时,不可在接口方法上面直接throws,但是可以在方法内部对异常进行throw
Rule 3. 实体类编写规则
- Controller:入参和出参的实体类都放在 domain --> vo 文件夹中
- 入参,其中新增、修改和删除方法的实体类使用Command结尾,查询使用Query结尾,也可以使用普通入参,但是最多可以有5个参数,超过5个必须定义实体类。
- 出参,使用Result结尾,多于两个参数,必须定义实体类。如果底层的出参是Entity或Dto,且都能满足前端需要,可直接把Entity或Dto作为出参。
- Service:
- 入参,可以用Vo、Dto、普通入参,其中Dto实体类放在 domain --> dto 文件夹中,文件结尾都用 Dto,同样普通入参超过5个必须定义实体类。
- 出参,可以用Dto,Entity,如果Entity文件包含返回结果的所有字段,才可用Entity文件进行返回,其余情况需要定义Dto实体类。
- Mapper:
- 入参,可以用Entity、Dto、Vo、Map、普通参数,其中Entity实体类放在 domain --> entity 文件夹中,文件结尾都用 Entity。可结合实际情况选择入参类型,但尽量使用已存在的实体,不建议再创建新的实体类。
- 出参,可以用Entity,Dto,如果单表返回可用Entity,多表联合返回只能使用Dto。
Rule4. resource 环境规则
名称 | 环境文件 | 描述 |
---|---|---|
基础环境 | application.yml | 为数据库,redis基础配置等,在实际开发中还可以增加其它内容 |
开发环境 | application-dev.yml | 开发环境配置 |
测试环境 | application-test.yml | 测试环境配置 |
修复环境 | application-fix.yml | 修复环境配置 |
生产环境 | application-prod.yml | 生产环境配置 |
- 基础环境
yml
server:
port: 8080
spring:
profiles:
active: @profiles.active@
include: zebra
flyway:
baseline-on-migrate: true
# schemas: flyway
table: flyway_schema_history_服务名
enabled: false
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_HOST:00.00.00.00}:${DB_PORT:3306}/${DB_NAME:db}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true&queryTimeout=2400&nullCatalogMeansCurrent=true
username: ${DB_USERNAME:postgres}
password: ${DB_PASSWORD:jinma}
type: com.alibaba.druid.pool.DruidDataSource
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
redis:
host: ${REDIS_HOST:00.00.00.00}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:0000}
timeout: 10s
lettuce:
pool:
max-active: 10
min-idle: 5
enabled: true
mybatis-plus:
global-config:
db-config:
# 1 代表已删除,不配置默认是1,也可修改配置
logic-delete-value: 1
# 0 代表未删除,不配置默认是0,也可修改配置
logic-not-delete-value: 0
- 开发环境
yml
zebra:
web:
swagger:
enabled: true
monitor:
ko-time:
enabled: true
admin:
enabled: true
arthas:
enabled: true
telnet-port: 3658
http-port: 8563
ip: 127.0.0.1
database:
monitor:
enabled: true
slow-sql-millis: 3000
log:
http:
enabled: true
cleaned: false
sentry:
dsn: http://4bb37d2ad9b487dffda1bbc5ef222031@xxxx/2
enabled: true
knife4j:
openapi:
title: demo
description: demo
version: v1.0.0
concat: zebra
- 测试环境
yml
zebra:
web:
swagger:
enabled: true
monitor:
ko-time:
enabled: true
admin:
enabled: true
arthas:
enabled: true
telnet-port: 3658
http-port: 8563
ip: 127.0.0.1
database:
monitor:
enabled: true
slow-sql-millis: 3000
log:
http:
enabled: true
cleaned: false
sentry:
dsn: http://4bb37d2ad9b487dffda1bbc5ef222031@xxx/2
enabled: true
knife4j:
openapi:
title: demo
description: demo
version: v1.0.0
concat: zebra
- 修复环境
yml
zebra:
web:
swagger:
enabled: true
monitor:
ko-time:
enabled: true
admin:
enabled: true
arthas:
enabled: true
telnet-port: 3658
http-port: 8563
ip: 127.0.0.1
database:
monitor:
enabled: true
slow-sql-millis: 3000
log:
http:
enabled: true
cleaned: false
sentry:
dsn: http://4bb37d2ad9b487dffda1bbc5ef222031@xx/2
enabled: true
knife4j:
openapi:
title: demo
description: demo
version: v1.0.0
concat: zebra
- 生产环境
yml
zebra:
web:
swagger:
enabled: false
monitor:
ko-time:
enabled: false
admin:
enabled: false
arthas:
enabled: false
telnet-port: 3658
http-port: 8563
ip: 127.0.0.1
database:
monitor:
enabled: false
slow-sql-millis: 3000
log:
http:
enabled: false
cleaned: false
sentry:
dsn: http://4bb37d2ad9b487dffda1bbc5ef222031@xxx/2
enabled: false
- zebra环境
yml
zebra:
web:
swagger:
enabled: ${zebra.web.swagger.enabled}
monitor:
ko-time:
pointcut: execution(public * com..*.*(..))
enabled: ${zebra.monitor.ko-time.enabled}
admin:
enabled: ${zebra.monitor.admin.enabled}
arthas:
enabled: ${zebra.monitor.arthas.enabled}
telnet-port: ${zebra.monitor.arthas.telnet-port}
http-port: ${zebra.monitor.arthas.http-port}
ip: ${zebra.monitor.arthas.ip}
database:
monitor:
enabled: ${zebra.database.monitor.enabled}
slow-sql-millis: ${zebra.database.monitor.slow-sql-millis}
log:
http:
enabled: ${zebra.log.http.enabled}
cleaned: ${zebra.log.cleaned}
sentry:
dsn: ${zebra.log.sentry.dsn}
enabled: ${zebra.log.sentry.enabled}
Rule5. maven pom 环境配置
xml
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env.version>1.0.0-SNAPSHOT</env.version>
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env.version>1.0.0-alpha</env.version>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<id>fix</id>
<properties>
<env.version>1.0.1-SNAPSHOT</env.version>
<profiles.active>fix</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env.version>1.0.0</env.version>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>