springboot-项目初始化常用配置

java

浏览数:1,633

2019-1-13

片段 1片段 2片段 3片段 4


application.properties配置

#mysql配置
spring.datasource.url=jdbc:mysql://localhost/wechat?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=soyeajr
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5

#mybatis配置 注意开启:@MapperScan(basePackages = "com.soyea.mapper")
mybatis.mapper-locations=classpath:mappers/*.xml
#mybatis.type-aliases-package=com.soyea.entity,com.soyea.common.dto,com.hg.common.vo
#mybatis.configuration.cache-enabled=true

#jackson配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8#应用根路径
server.context-path=/sell
server.port=8080

spring.jpa.show-sql=true
logging.level.org.springframework.web=debug


Ajax返回对象

package com.hg.common.result;

import lombok.Data;

import java.io.Serializable;

/**
 * ajax请求返回的对象
 */
@Data
public class AjaxResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    /**
     * 成功时调用
     * @param data
     * @param <T>
     * @return
     */
    public static<T> AjaxResult<T> success(T data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(ResultEnum.SUCCESS.getCode());
        ajaxResult.setMsg(ResultEnum.SUCCESS.getMessage());
        ajaxResult.setData(data);
        return ajaxResult;
    }

    /**
     * 成功时调用
     * @return
     */
    public static AjaxResult success(){
        return success(null);
    }

    /**
     * 失败时调用
     * @param code
     * @param msg
     * @return
     */
    public static AjaxResult error(Integer code, String msg) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMsg(msg);
        return ajaxResult;
    }

    /**
     * 失败时调用
     * @return
     */
    public static AjaxResult error(ResultEnum resultEnum) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(resultEnum.getCode());
        ajaxResult.setMsg(resultEnum.getMessage());
        return ajaxResult;
    }
}


返回json字符串 null值转空字符串

package com.enz.college.admin.common.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;

/**
 * 〈返回json空值去掉null和""〉
 */
@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
        // Include.Include.ALWAYS 默认
        // Include.NON_DEFAULT 属性为默认值不序列化
        // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
        // Include.NON_NULL 属性为NULL 不序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        // 字段保留,将null值转为""
//      objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
//          @Override
//          public void serialize(Object o, JsonGenerator jsonGenerator,
//                                SerializerProvider serializerProvider)
//                  throws IOException, JsonProcessingException {
//              jsonGenerator.writeString("");
//              jsonGenerator.writeNull();
//          }
//      });
        return objectMapper;
    }
}  


状态枚举类

package com.enz.goodscloud.navigation.constant.enums;

import lombok.Getter;

/**
 * 状态枚举
 */
@Getter
public enum StatusEnums {
    ENABLE(1, "启用"),
    UNABLE(0, "禁用");
    

    private Integer code;
    private String message;

    StatusEnums(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    
    public static StatusEnums stateOf(int index) {
        for (StatusEnums state : values()) {
            if (state.getCode() == index) {
                return state;
            }
        }
        return null;
    }
}