#rest风格,新增和更新,区别post,put
#mysql中id的自动生成
#API安全
#如果加上,创建时间和更新时间。 那么在响应操作时,应主动触发修改该字段
#如果加上,是否有效。 那么在查询时,应主动过滤无效
#时间格式转化
#配置SwaggerUI为默认入口
#返回值格式考虑
#异常处理
#ddl默认用utf-8
#swaggerUI时间格式修改
#修改数据状态时,首先check是否存在
#集成redis
#考虑不用Dto,直接通过jsonIgnore或者其他方式过滤掉隐藏属性
------------------------------------------------------------------
*列表分页
*查询条件处理
*熟悉JPA的使用
调用示例
数据初始化
调出Druid面板
更新保存&添加保存,考虑调用方式
-----------------------------------------------------
org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE UTF-8 UTF-8 1.8 1.8 1.8 2.7.0 true org.springframework.cloud spring-cloud-dependencies Dalston.SR4 pom import org.apache.commons commons-lang3 ${commons-lang3.version} com.alibaba druid 1.0.29 org.springframework.boot spring-boot-starter-data-jpa org.mybatis.spring.boot mybatis-spring-boot-starter com.github.pagehelper pagehelper 4.1.6 mysql mysql-connector-java org.springframework.boot spring-boot-starter-data-redis io.springfox springfox-swagger2 ${springfox.version} io.springfox springfox-swagger-ui ${springfox.version} com.fasterxml.jackson.module jackson-module-parameter-names com.fasterxml.jackson.datatype jackson-datatype-jdk8 com.fasterxml.jackson.datatype jackson-datatype-jsr310 org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator xxx-api-petStore org.springframework.boot spring-boot-maven-plugin api.xxx.com.petstore.PetStoreAPIApplication exec repackage org.mybatis.generator mybatis-generator-maven-plugin 1.3.5
# server server: port: 9124 # spring spring: #profiles profiles: active: dev #jackson jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss #datasource datasource: # driver config type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver # connection pool config 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 maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,log4j connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #jpa jpa: database-platform: api.xxx.com.petstore.config.MySQL5DialectUTF8 #redis redis: pool: max-idle: 8 min-idle: 0 max-active: 8 max-wait: -1 #mybatis mybatis: type-aliases-package: api.xxx.com.petstore.entity mapper-locations: classpath*:/sqlMapperXml/*.xml configuration: map-underscore-to-camel-case: true use-generated-keys: true default-fetch-size: 100 default-statement-timeout: 25000 cache-enabled: true aggressive-lazy-loading: true lazy-loading-enabled: false
spring: #datasource datasource: url: jdbc:mysql://192.168.0.105:3306/db_petstore?useUnicode=yes&characterEncoding=UTF-8 username: xxx password: xxx #jpa jpa: # hibernate: # ddl-auto: create show-sql: true #redis redis: database: 4 host: 192.168.0.123 password: xxx # host: 127.0.0.1 port: 6379 #mybatis mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logback %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n /data/logs/restapi-petstore/restapi-petstore.%d{yyyy-MM-dd}.log 90 %d [%thread] %-5level %logger{36} H:${HOSTNAME} - SC:%X{optionalParam} %msg%n UTF-8 true
==========================
@Getter @Setter @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class BaseEntity { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name="system-uuid",strategy = "uuid") @Column(name = "id", length = 32) private String id; @CreatedDate @JsonFormat(pattern = DateUtils.FORMAT_DATE_TIME) private Date createdDate; @LastModifiedDate @JsonFormat(pattern = DateUtils.FORMAT_DATE_TIME) private Date modifiedDate; @Column(name = "is_active") @JsonIgnore private boolean isActive = true; }
=================================================
configs of java file
=================================================
public class MySQL5DialectUTF8 extends MySQL5InnoDBDialect { @Override public String getTableTypeString() { return " ENGINE=InnoDB DEFAULT CHARSET=utf8"; } } @Configuration @EnableJpaAuditing @EnableTransactionManagement @EnableSpringDataWebSupport @MapperScan("api.xxx.com.petstore.mapper") public class AppConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("dialect", "mysql"); p.setProperty("supportMethodsArguments", "true"); p.setProperty("autoRuntimeDialect", "true"); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); p.setProperty("returnPageInfo", "always"); p.setProperty("params", "count=countSql"); pageHelper.setProperties(p); return pageHelper; } } @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean @SuppressWarnings("deprecation") public Docket petStoreApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(petStoreApiInfo()) .useDefaultResponseMessages(false) .select() .apis( input -> { Class> declaringClass = input.declaringClass(); String pkg = declaringClass.getPackage().getName(); if(!pkg.matches("api.xxx.com.petstore.*")) //非本项目,排除 return false; if (declaringClass == BasicErrorController.class)// 排除 return false; if(declaringClass.isAnnotationPresent(RestController.class)) // 被注解的类 return true; if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法 return true; return false; }) .build(); } private ApiInfo petStoreApiInfo() { return new ApiInfoBuilder() .title("xxx后台API") .description("xxx后台接口支持") .version("1.0") .contact(new Contact("xxxChengdu RD", "http://xxx.com", "[email protected]")) .build(); } } @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry){ registry.addRedirectViewController("/", "/swagger-ui.html"); } }