1. 程式人生 > 其它 >工具類封裝

工具類封裝

package com.coder.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 返回資料封裝
 * @author 王令
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private boolean status;
    private int code;
    private String msg;
    private Object data;


    private static final String SUCCESS = "success";
    private static final String FAIL = "fail";

    private static final int SUCCESS_CODE = 0;
    private static final int FAIL_CODE = 1;

    public static Result ok(){
        return ok(SUCCESS);
    }

    public static Result ok(int code){
        return ok(code, SUCCESS);
    }

    public static Result ok(String msg){
        return ok(msg, null);
    }

    public static Result ok(Object data){
        return ok(SUCCESS, data);
    }

    public static Result ok(int code, String msg){
        return ok(code, msg, null);
    }

    public static Result ok(int code, Object data){
        return ok(code, SUCCESS, data);
    }

    public static Result ok(String msg, Object data){
        return ok(SUCCESS_CODE, msg, data);
    }

    public static Result ok(int code, String msg, Object data){
        return new Result(true, code, msg, data);
    }

    public static Result fail(){
        return fail(FAIL_CODE);
    }

    public static Result fail(int code){
        return fail(code, FAIL);
    }

    public static Result fail(String msg){
        return fail(FAIL_CODE, msg);
    }

    public static Result fail(int code, String msg){
        return fail(code, msg, null);
    }

    public static Result fail(int code, String msg, Object data){
        return new Result(false, code, msg, data);
    }
}

package com.coder.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * 德魯伊工具類封裝
 * @author 王令
 */
public class DruidUtils {
    private static DataSource dataSource = null;

    static {
        try {
            Properties ppt = new Properties();
            ppt.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(ppt);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static PreparedStatement getPps(Connection connection, String sql, List<Object> params){
        PreparedStatement pps = null;
        try {
             pps = connection.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                pps.setObject(i + 1, params.get(i));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }

    public static boolean update(String sql, List<Object> params){
        boolean result = false;
        try (
                Connection connection = getConnection();
                PreparedStatement pps = getPps(connection, sql, params);
                ){
            result = pps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void close(Connection connection){
        close(connection, null);
    }

    public static void close(Connection connection, PreparedStatement pps){
        close(connection, pps, null);
    }

    public static void close(Connection connection, PreparedStatement pps, ResultSet resultSet){
        try {
            if (ObjectUtil.isNotNull(connection)) {
                connection.close();
            }
            if (ObjectUtil.isNotNull(pps)){
                pps.close();
            }
            if (ObjectUtil.isNotNull(resultSet)){
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
掙錢養媳婦兒^.^