使用工具类
最近头一次写项目用到了几个工具类写下来以后继续改进现阶段只满足基本的使用功能不是很完善
1.上传文件
public class FileUtil {
private FileUtil() {
}
private static final String TARGET_DIRECTORY = "upload/user";
public static String fileUpload(String sourceFilePath) {
String data = LocalDate.now().toString();
String pictureName = sourceFilePath.substring(sourceFilePath.lastIndexOf(File.separatorChar) + 1);
File childDirectory = new File(TARGET_DIRECTORY, data);
if (!childDirectory.exists()) {
childDirectory.mkdirs();
}
String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
String fileName = uuidStr +"-"+pictureName;
File targetFilePath = new File(childDirectory, fileName);
String path = "";
try (
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFilePath));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFilePath));
) {
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
path = targetFilePath.getPath();
System.out.println("文件上传成功 " + path);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
}
2.输入工具
public class InputUtil {
private InputUtil() {
}
private static Scanner input;
static {
input = new Scanner(System.in);
}
//提供2个静态的方法
public static int inputInt() {
return input.nextInt();
}
public static float inputFloat(){
return input.nextFloat();}
public static double inputDouble(){
return input.nextDouble();}
public static BigDecimal inputBigDecimal (){
return input.nextBigDecimal();}
public static int inputInt(String regex, String msg) {
while (true) {
String str = input.next();
if (str.matches(regex)) {
return Integer.parseInt(str);
}
System.out.println(msg);
}
}
public static String inputStr() {
input.nextLine();
return input.nextLine();
}
public static String inputNextStr() {
return input.next();
}
}
3.链接数据库
3.1 jdbc.properties
username=root
password=root
url=jdbc:mysql://192.168.12.250:3306/sms?useSSL=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
3.2 PrpUtil
public class PropUtil {
PropUtil() {
}
private static Properties properties;
static {
properties = new Properties();
try {
properties.load(PropUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
Class.forName(PropUtil.getValue("driver"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return properties.getProperty(key," ");
}
}
3.3 DBHelper
public class DBHelper {
private DBHelper() {
}
private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal() {
@Override
@SneakyThrows
protected Object initialValue() {
return DriverManager.getConnection(PropUtil.getValue("url"), PropUtil.getValue("password"), PropUtil.getValue("username"));
}
};
public static Connection getCon() {
return THREAD_LOCAL.get();
}
public static void closeResources(Connection connection,PreparedStatement ps,ResultSet rs) {
Objects.requireNonNull(connection);
Objects.requireNonNull(ps);
Objects.requireNonNull(rs);
try {
rs.close();
ps.close();
connection.close();
THREAD_LOCAL.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeResources(Connection connection,PreparedStatement ps) {
Objects.requireNonNull(connection);
Objects.requireNonNull(ps);
try {
ps.close();
connection.close();
THREAD_LOCAL.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeResources(Connection connection) {
Objects.requireNonNull(connection);
try {
connection.close();
THREAD_LOCAL.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.Service响应
4.1 CodeEnum
public enum CodeEnum {
SUCCESS("success",200),
ERROR("error",403);
private String msg;
private Integer code;
public String getMsg(){
return msg;}
public Integer getCode(){
return code;}
CodeEnum(String msg,Integer code){
this.msg = msg;
this.code = code;
}
}
4.2 ServerResponseResult
@Data
public class ServerResponseResult<D> {
private String message;
private Integer status;
private D data;
public static <D> ServerResponseResult<D> success(D data) {
return new ServerResponseResult<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS.getCode(), data);
}
public static <D> ServerResponseResult<D> success(String msg, D data) {
return new ServerResponseResult<>(msg, CodeEnum.SUCCESS.getCode(), data);
}
public static <D> ServerResponseResult<D> success() {
return new ServerResponseResult<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS.getCode());
}
public static <D> ServerResponseResult<D> error(){
return new ServerResponseResult<>(CodeEnum.ERROR.getMsg(),CodeEnum.ERROR.getCode());
}
public ServerResponseResult(String message, Integer status, D data) {
this.message = message;
this.status = status;
this.data = data;
}
public ServerResponseResult(String message, Integer status) {
this.message = message;
this.status = status;
}
public ServerResponseResult() {
}
}
5.阿里数据库连接池Druid.
阿里数据库连接池Druid.
配置文件
username=root
password=root
url=jdbc:mysql://192.168.12.250:3306/test?useSSL=true&characterEncoding=utf8
driverClassName=com.mysql.jdbc.Driver
initalSize=8
maxActive=801
主代码
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class DBHelpers {
private DBHelpers() {
}
private static DataSource dataSource;
static {
Properties properties = new Properties();
try {
properties.load(DBHelpers.class.getClassLoader().getResourceAsStream("jdbc.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getCon(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static DataSource getDataSource(){
return dataSource;
}
}
6.ajax数据返回格式
public enum ReturnCode {
UNAME_OK(20000,"用户名可用"),
UNAME_WRONG(20001,"用户名重复"),
REQ_SUCCESS(10000,"操作成功"),
NO_DATA(10001,"没有数据");
private Integer rcode;
private String rmsg;
ReturnCode(Integer rcode, String rmsg) {
this.rcode = rcode;
this.rmsg = rmsg;
}
public Integer getRcode() {
return rcode;
}
public String getRmsg() {
return rmsg;
}
}
返回数据实体
public class ReturnEntity {
private Integer code;
private String msg;
private Object data;
public ReturnEntity() {
}
public ReturnEntity(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}