java 工具类使用
BigDecimalUtil 金额计算工具类
- import java.math.BigDecimal;
- public class BigDecimalUtil {
- private BigDecimalUtil(){
- }
- public static BigDecimal add(double v1,double v2){
- BigDecimal b1 = new BigDecimal(Double.toString(v1));
- BigDecimal b2 = new BigDecimal(Double.toString(v2));
- return b1.add(b2);
- }
- public static BigDecimal sub(double v1,double v2){
- BigDecimal b1 = new BigDecimal(Double.toString(v1));
- BigDecimal b2 = new BigDecimal(Double.toString(v2));
- return b1.subtract(b2);
- }
- public static BigDecimal mul(double v1,double v2){
- BigDecimal b1 = new BigDecimal(Double.toString(v1));
- BigDecimal b2 = new BigDecimal(Double.toString(v2));
- return b1.multiply(b2);
- }
- public static BigDecimal div(double v1,double v2){
- BigDecimal b1 = new BigDecimal(Double.toString(v1));
- BigDecimal b2 = new BigDecimal(Double.toString(v2));
- return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//四舍五入,保留2位小数
- //除不尽的情况
- }
- }
joda-time 时间工具类
- import org.apache.commons.lang3.StringUtils;
- import org.joda.time.DateTime;
- import org.joda.time.format.DateTimeFormat;
- import org.joda.time.format.DateTimeFormatter;
- import java.util.Date;
- public class DateTimeUtil {
- //joda-time
- //str->Date
- //Date->str
- public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
- public static Date strToDate(String dateTimeStr,String formatStr){
- DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
- return dateTime.toDate();
- }
- public static String dateToStr(Date date,String formatStr){
- if(date == null){
- return StringUtils.EMPTY;
- }
- DateTime dateTime = new DateTime(date);
- return dateTime.toString(formatStr);
- }
- public static Date strToDate(String dateTimeStr){
- DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
- return dateTime.toDate();
- }
- public static String dateToStr(Date date){
- if(date == null){
- return StringUtils.EMPTY;
- }
- DateTime dateTime = new DateTime(date);
- return dateTime.toString(STANDARD_FORMAT);
- }
- public static void main(String[] args) {
- System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
- System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
- }
- }
或者jdk自带的时间处理类
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class DateUtils {
- public static String transferdate(Date date,String dateFormatparam){
- DateFormat dateFormat = new SimpleDateFormat(dateFormatparam);
- return dateFormat.format(date);
- }
- public static Date transferdate(String datastring,String dateFormatparam){
- Date date = new Date();
- DateFormat dateFormat = new SimpleDateFormat(dateFormatparam);
- try {
- date = dateFormat.parse(datastring);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return date;
- }
- public static void main(String[] args) {
- String stringDate = transferdate(new Date(), "yyyy-MM-dd hh:mm:ss");
- System.out.println(stringDate);
- Date date = transferdate("20190101", "yyyy-MM-dd");
- System.out.println(date);
- }
- }
FTP工具类
- import org.apache.commons.net.ftp.FTPClient;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.List;
- public class FTPUtil {
- private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);
- private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
- private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
- private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");
- public FTPUtil(String ip,int port,String user,String pwd){
- this.ip = ip;
- this.port = port;
- this.user = user;
- this.pwd = pwd;
- }
- public static boolean uploadFile(List<File> fileList) throws IOException {
- FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass);
- logger.info("开始连接ftp服务器");
- boolean result = ftpUtil.uploadFile("img",fileList);
- logger.info("开始连接ftp服务器,结束上传,上传结果:{}");
- return result;
- }
- private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
- boolean uploaded = true;
- FileInputStream fis = null;
- //连接FTP服务器
- if(connectServer(this.ip,this.port,this.user,this.pwd)){
- try {
- ftpClient.changeWorkingDirectory(remotePath);
- ftpClient.setBufferSize(1024);
- ftpClient.setControlEncoding("UTF-8");
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- ftpClient.enterLocalPassiveMode();
- for(File fileItem : fileList){
- fis = new FileInputStream(fileItem);
- ftpClient.storeFile(fileItem.getName(),fis);
- }
- } catch (IOException e) {
- logger.error("上传文件异常",e);
- uploaded = false;
- e.printStackTrace();
- } finally {
- fis.close();
- ftpClient.disconnect();
- }
- }
- return uploaded;
- }
- private boolean connectServer(String ip,int port,String user,String pwd){
- boolean isSuccess = false;
- ftpClient = new FTPClient();
- try {
- ftpClient.connect(ip);
- isSuccess = ftpClient.login(user,pwd);
- } catch (IOException e) {
- logger.error("连接FTP服务器异常",e);
- }
- return isSuccess;
- }
- private String ip;
- private int port;
- private String user;
- private String pwd;
- private FTPClient ftpClient;
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPwd() {
- return pwd;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- public FTPClient getFtpClient() {
- return ftpClient;
- }
- public void setFtpClient(FTPClient ftpClient) {
- this.ftpClient = ftpClient;
- }
- }
MD5Util
- import java.security.MessageDigest;
- public class MD5Util {
- private static String byteArrayToHexString(byte b[]) {
- StringBuffer resultSb = new StringBuffer();
- for (int i = 0; i < b.length; i++)
- resultSb.append(byteToHexString(b[i]));
- return resultSb.toString();
- }
- private static String byteToHexString(byte b) {
- int n = b;
- if (n < 0)
- n += 256;
- int d1 = n / 16;
- int d2 = n % 16;
- return hexDigits[d1] + hexDigits[d2];
- }
- /**
- * 返回大写MD5
- *
- * @param origin
- * @param charsetname
- * @return
- */
- private static String MD5Encode(String origin, String charsetname) {
- String resultString = null;
- try {
- resultString = new String(origin);
- MessageDigest md = MessageDigest.getInstance("MD5");
- if (charsetname == null || "".equals(charsetname))
- resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
- else
- resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
- } catch (Exception exception) {
- }
- return resultString.toUpperCase();
- }
- public static String MD5EncodeUtf8(String origin) {
- origin = origin + PropertiesUtil.getProperty("password.salt", "");
- return MD5Encode(origin, "utf-8");
- }
- private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
- "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
- }
或者这个
- import java.util.Random;
- import org.apache.commons.codec.binary.Hex;
- import java.security.NoSuchAlgorithmException;
- import java.security.MessageDigest;
- /**
- * MD5工具类,加盐
- */
- public class MD5Util {
- /**
- * 普通MD5
- * @param input
- * @return
- */
- public static String MD5(String input) {
- MessageDigest md5 = null;
- try {
- md5 = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- return "check jdk";
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- char[] charArray = input.toCharArray();
- byte[] byteArray = new byte[charArray.length];
- for (int i = 0; i < charArray.length; i++)
- byteArray[i] = (byte) charArray[i];
- byte[] md5Bytes = md5.digest(byteArray);
- StringBuffer hexValue = new StringBuffer();
- for (int i = 0; i < md5Bytes.length; i++) {
- int val = ((int) md5Bytes[i]) & 0xff;
- if (val < 16)
- hexValue.append("0");
- hexValue.append(Integer.toHexString(val));
- }
- return hexValue.toString();
- }
- /**
- * 加盐MD5
- * @param password
- * @return
- */
- public static String generate(String password) {
- Random r = new Random();
- StringBuilder sb = new StringBuilder(16);
- sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
- int len = sb.length();
- if (len < 16) {
- for (int i = 0; i < 16 - len; i++) {
- sb.append("0");
- }
- }
- String salt = sb.toString();
- password = md5Hex(password + salt);
- char[] cs = new char[48];
- for (int i = 0; i < 48; i += 3) {
- cs[i] = password.charAt(i / 3 * 2);
- char c = salt.charAt(i / 3);
- cs[i + 1] = c;
- cs[i + 2] = password.charAt(i / 3 * 2 + 1);
- }
- return new String(cs);
- }
- /**
- * 校验加盐后是否和原文一致
- * @param password
- * @param md5
- * @return
- */
- public static boolean verify(String password, String md5) {
- char[] cs1 = new char[32];
- char[] cs2 = new char[16];
- for (int i = 0; i < 48; i += 3) {
- cs1[i / 3 * 2] = md5.charAt(i);
- cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
- cs2[i / 3] = md5.charAt(i + 1);
- }
- String salt = new String(cs2);
- return md5Hex(password + salt).equals(new String(cs1));
- }
- /**
- * 获取十六进制字符串形式的MD5摘要
- */
- private static String md5Hex(String src) {
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- byte[] bs = md5.digest(src.getBytes());
- return new String(new Hex().encode(bs));
- } catch (Exception e) {
- return null;
- }
- }
- // 测试主函数
- public static void main(String args[]) {
- // 原文
- String plaintext = "testAddSalt123";
- System.out.println("原始:" + plaintext);
- System.out.println("普通MD5后:" + MD5Util.MD5(plaintext));
- // 获取加盐后的MD5值
- String ciphertext = MD5Util.generate(plaintext);
- System.out.println("加盐后MD5:" + ciphertext);
- System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, ciphertext));
- /**
- * 其中某次plaintext="testAddSalt123"字符串加盐后的MD5值
- */
- String[] tempSalt = { "15af3d445524108642c22767922a4a797278b85b1ac29f09", "67301583c322b2946397bd3c42f35775c303f3486f01c24d", "632d2828022e42cf3bc4c41801a69a313c01b67b3d295a52" };
- for (String temp : tempSalt) {
- System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, temp));
- }
- }
- }
PropertiesUtil
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Properties;
- public class PropertiesUtil {
- private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
- private static Properties props;
- static {
- String fileName = "mmall.properties";
- props = new Properties();
- try {
- props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
- } catch (IOException e) {
- logger.error("配置文件读取异常",e);
- }
- }
- public static String getProperty(String key){
- String value = props.getProperty(key.trim());
- if(StringUtils.isBlank(value)){
- return null;
- }
- return value.trim();
- }
- public static String getProperty(String key,String defaultValue){
- String value = props.getProperty(key.trim());
- if(StringUtils.isBlank(value)){
- value = defaultValue;
- }
- return value.trim();
- }
- }
SpringMVC封装返回信息
ServerResponse
- import org.codehaus.jackson.annotate.JsonIgnore;
- import org.codehaus.jackson.map.annotate.JsonSerialize;
- import java.io.Serializable;
- @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
- //保证序列化json的时候,如果是null的对象,key也会消失
- public class ServerResponse<T> implements Serializable {
- private int status;
- private String msg;
- private T data;
- private ServerResponse(int status){
- this.status = status;
- }
- private ServerResponse(int status,T data){
- this.status = status;
- this.data = data;
- }
- private ServerResponse(int status,String msg,T data){
- this.status = status;
- this.msg = msg;
- this.data = data;
- }
- private ServerResponse(int status,String msg){
- this.status = status;
- this.msg = msg;
- }
- @JsonIgnore
- //使之不在json序列化结果当中
- public boolean isSuccess(){
- return this.status == ResponseCode.SUCCESS.getCode();
- }
- public int getStatus(){
- return status;
- }
- public T getData(){
- return data;
- }
- public String getMsg(){
- return msg;
- }
- public static <T> ServerResponse<T> createBySuccess(){
- return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
- }
- public static <T> ServerResponse<T> createBySuccessMessage(String msg){
- return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
- }
- public static <T> ServerResponse<T> createBySuccess(T data){
- return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
- }
- public static <T> ServerResponse<T> createBySuccess(String msg,T data){
- return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
- }
- public static <T> ServerResponse<T> createByError(){
- return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
- }
- public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
- return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
- }
- public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
- return new ServerResponse<T>(errorCode,errorMessage);
- }
- }
ResponseCode
- public enum ResponseCode {
- SUCCESS(0,"SUCCESS"),
- ERROR(1,"ERROR"),
- NEED_LOGIN(10,"NEED_LOGIN"),
- ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");
- private final int code;
- private final String desc;
- ResponseCode(int code,String desc){
- this.code = code;
- this.desc = desc;
- }
- public int getCode(){
- return code;
- }
- public String getDesc(){
- return desc;
- }
- }
RedisUtil redisTemplate工具类
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.util.CollectionUtils;
- /**
- * 基于spring和redis的redisTemplate工具类
- * 针对所有的hash 都是以h开头的方法
- * 针对所有的Set 都是以s开头的方法 不含通用方法
- * 针对所有的List 都是以l开头的方法
- */
- public class RedisUtil {
- private RedisTemplate<String, Object> redisTemplate;
- public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- //=============================common============================
- /**
- * 指定缓存失效时间
- * @param key 键
- * @param time 时间(秒)
- * @return
- */
- public boolean expire(String key,long time){
- try {
- if(time>0){
- redisTemplate.expire(key, time, TimeUnit.SECONDS);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 根据key 获取过期时间
- * @param key 键 不能为null
- * @return 时间(秒) 返回0代表为永久有效
- */
- public long getExpire(String key){
- return redisTemplate.getExpire(key,TimeUnit.SECONDS);
- }
- /**
- * 判断key是否存在
- * @param key 键
- * @return true 存在 false不存在
- */
- public boolean hasKey(String key){
- try {
- return redisTemplate.hasKey(key);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 删除缓存
- * @param key 可以传一个值 或多个
- */
- @SuppressWarnings("unchecked")
- public void del(String ... key){
- if(key!=null&&key.length>0){
- if(key.length==1){
- redisTemplate.delete(key[0]);
- }else{
- redisTemplate.delete(CollectionUtils.arrayToList(key));
- }
- }
- }
- //============================String=============================
- /**
- * 普通缓存获取
- * @param key 键
- * @return 值
- */
- public Object get(String key){
- return key==null?null:redisTemplate.opsForValue().get(key);
- }
- /**
- * 普通缓存放入
- * @param key 键
- * @param value 值
- * @return true成功 false失败
- */
- public boolean set(String key,Object value) {
- try {
- redisTemplate.opsForValue().set(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 普通缓存放入并设置时间
- * @param key 键
- * @param value 值
- * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
- * @return true成功 false 失败
- */
- public boolean set(String key,Object value,long time){
- try {
- if(time>0){
- redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
- }else{
- set(key, value);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 递增
- * @param key 键
- * @param by 要增加几(大于0)
- * @return
- */
- public long incr(String key, long delta){
- if(delta<0){
- throw new RuntimeException("递增因子必须大于0");
- }
- return redisTemplate.opsForValue().increment(key, delta);
- }
- /**
- * 递减
- * @param key 键
- * @param by 要减少几(小于0)
- * @return
- */
- public long decr(String key, long delta){
- if(delta<0){
- throw new RuntimeException("递减因子必须大于0");
- }
- return redisTemplate.opsForValue().increment(key, -delta);
- }
- //================================Map=================================
- /**
- * HashGet
- * @param key 键 不能为null
- * @param item 项 不能为null
- * @return 值
- */
- public Object hget(String key,String item){
- return redisTemplate.opsForHash().get(key, item);
- }
- /**
- * 获取hashKey对应的所有键值
- * @param key 键
- * @return 对应的多个键值
- */
- public Map<Object,Object> hmget(String key){
- return redisTemplate.opsForHash().entries(key);
- }
- /**
- * HashSet
- * @param key 键
- * @param map 对应多个键值
- * @return true 成功 false 失败
- */
- public boolean hmset(String key, Map<String,Object> map){
- try {
- redisTemplate.opsForHash().putAll(key, map);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * HashSet 并设置时间
- * @param key 键
- * @param map 对应多个键值
- * @param time 时间(秒)
- * @return true成功 false失败
- */
- public boolean hmset(String key, Map<String,Object> map, long time){
- try {
- redisTemplate.opsForHash().putAll(key, map);
- if(time>0){
- expire(key, time);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- * @param key 键
- * @param item 项
- * @param value 值
- * @return true 成功 false失败
- */
- public boolean hset(String key,String item,Object value) {
- try {
- redisTemplate.opsForHash().put(key, item, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- * @param key 键
- * @param item 项
- * @param value 值
- * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
- * @return true 成功 false失败
- */
- public boolean hset(String key,String item,Object value,long time) {
- try {
- redisTemplate.opsForHash().put(key, item, value);
- if(time>0){
- expire(key, time);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 删除hash表中的值
- * @param key 键 不能为null
- * @param item 项 可以使多个 不能为null
- */
- public void hdel(String key, Object... item){
- redisTemplate.opsForHash().delete(key,item);
- }
- /**
- * 判断hash表中是否有该项的值
- * @param key 键 不能为null
- * @param item 项 不能为null
- * @return true 存在 false不存在
- */
- public boolean hHasKey(String key, String item){
- return redisTemplate.opsForHash().hasKey(key, item);
- }
- /**
- * hash递增 如果不存在,就会创建一个 并把新增后的值返回
- * @param key 键
- * @param item 项
- * @param by 要增加几(大于0)
- * @return
- */
- public double hincr(String key, String item,double by){
- return redisTemplate.opsForHash().increment(key, item, by);
- }
- /**
- * hash递减
- * @param key 键
- * @param item 项
- * @param by 要减少记(小于0)
- * @return
- */
- public double hdecr(String key, String item,double by){
- return redisTemplate.opsForHash().increment(key, item,-by);
- }
- //============================set=============================
- /**
- * 根据key获取Set中的所有值
- * @param key 键
- * @return
- */
- public Set<Object> sGet(String key){
- try {
- return redisTemplate.opsForSet().members(key);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 根据value从一个set中查询,是否存在
- * @param key 键
- * @param value 值
- * @return true 存在 false不存在
- */
- public boolean sHasKey(String key,Object value){
- try {
- return redisTemplate.opsForSet().isMember(key, value);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 将数据放入set缓存
- * @param key 键
- * @param values 值 可以是多个
- * @return 成功个数
- */
- public long sSet(String key, Object...values) {
- try {
- return redisTemplate.opsForSet().add(key, values);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- /**
- * 将set数据放入缓存
- * @param key 键
- * @param time 时间(秒)
- * @param values 值 可以是多个
- * @return 成功个数
- */
- public long sSetAndTime(String key,long time,Object...values) {
- try {
- Long count = redisTemplate.opsForSet().add(key, values);
- if(time>0) expire(key, time);
- return count;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- /**
- * 获取set缓存的长度
- * @param key 键
- * @return
- */
- public long sGetSetSize(String key){
- try {
- return redisTemplate.opsForSet().size(key);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- /**
- * 移除值为value的
- * @param key 键
- * @param values 值 可以是多个
- * @return 移除的个数
- */
- public long setRemove(String key, Object ...values) {
- try {
- Long count = redisTemplate.opsForSet().remove(key, values);
- return count;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- //===============================list=================================
- /**
- * 获取list缓存的内容
- * @param key 键
- * @param start 开始
- * @param end 结束 0 到 -1代表所有值
- * @return
- */
- public List<Object> lGet(String key,long start, long end){
- try {
- return redisTemplate.opsForList().range(key, start, end);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 获取list缓存的长度
- * @param key 键
- * @return
- */
- public long lGetListSize(String key){
- try {
- return redisTemplate.opsForList().size(key);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- /**
- * 通过索引 获取list中的值
- * @param key 键
- * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
- * @return
- */
- public Object lGetIndex(String key,long index){
- try {
- return redisTemplate.opsForList().index(key, index);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 将list放入缓存
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, Object value) {
- try {
- redisTemplate.opsForList().rightPush(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 将list放入缓存
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, Object value, long time) {
- try {
- redisTemplate.opsForList().rightPush(key, value);
- if (time > 0) expire(key, time);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 将list放入缓存
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, List<Object> value) {
- try {
- redisTemplate.opsForList().rightPushAll(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 将list放入缓存
- * @param key 键
- * @param value 值
- * @param time 时间(秒)
- * @return
- */
- public boolean lSet(String key, List<Object> value, long time) {
- try {
- redisTemplate.opsForList().rightPushAll(key, value);
- if (time > 0) expire(key, time);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 根据索引修改list中的某条数据
- * @param key 键
- * @param index 索引
- * @param value 值
- * @return
- */
- public boolean lUpdateIndex(String key, long index,Object value) {
- try {
- redisTemplate.opsForList().set(key, index, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 移除N个值为value
- * @param key 键
- * @param count 移除多少个
- * @param value 值
- * @return 移除的个数
- */
- public long lRemove(String key,long count,Object value) {
- try {
- Long remove = redisTemplate.opsForList().remove(key, count, value);
- return remove;
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- /** 获取集合长度
- * @param key
- * @return
- */
- public long getkeylistsize(String key){
- return redisTemplate.opsForList().size(key);
- }
- public long pushlist(String key,String value){
- return redisTemplate.opsForList().leftPush(key, value);
- }
- public Set<String> getkeys(String key){
- return redisTemplate.keys(key +"*");
- }
- }
java 工具类使用的更多相关文章
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
- 第一章 Java工具类目录
在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Java工具类:给程序增加版权信息
我们九天鸟的p2p网贷系统,基本算是开发完成了. 现在,想给后端的Java代码,增加版权信息. 手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...
- 常用高效 Java 工具类总结
一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...
- 几种高效的Java工具类推荐
本文将介绍了十二种常用的.高效的Java工具类 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类. 在开发中,使用这些工具类,不仅可以提高编码效率,还 ...
随机推荐
- [HG]走夜路 题解
前言 整个机房就我一个人在想动态规划. 想了半天发现一堆性质,结果由于DP中出现折线挂了. 题目描述 某NOIP普及组原题加强版. \(Jim\) 非常怕黑,他有一个手电筒,设手电筒的电量上限为 \( ...
- KMP 串的模式匹配 (25 分)
给定两个由英文字母组成的字符串 String 和 Pattern,要求找到 Pattern 在 String 中第一次出现的位置,并将此位置后的 String 的子串输出.如果找不到,则输出“Not ...
- 「CQOI2014」数三角形
题目链接 问题分析 可以先任意选\(3\)个数,然后减去三点共线的部分. 三点共线又分\(2\)种情况: 横的或者竖的.这一部分方案数是\(n\times{m\choose 3}+m\times {n ...
- Codeforces Round #201.C-Alice and Bob
C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 通过Flink实现个推海量消息数据的实时统计
背景 消息报表主要用于统计消息任务的下发情况.比如,单条推送消息下发APP用户总量有多少,成功推送到手机的数量有多少,又有多少APP用户点击了弹窗通知并打开APP等.通过消息报表,我们可以很直观地看到 ...
- C++入门经典-例2.1-利用实数精度进行实数比较
1:代码如下: // 2.1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" void main() { float eps = 0.000 ...
- 套接字之recvmsg系统调用
recvmsg系统调用允许用户指定msghdr结构来接收数据,可以将数据接收到多个缓冲区中,并且可以接收控制信息:接收信息过程与其他接收系统调用核心一致,都是调用传输层的接收函数进行数据接收: SYS ...
- 线性回归和正则化(Regularization)
python风控建模实战lendingClub(博主录制,包含大量回归建模脚本和和正则化解释,2K超清分辨率) https://study.163.com/course/courseMain.htm? ...
- 浏览器端-W3School-JavaScript:JavaScript RegExp 对象
ylbtech-浏览器端-W3School-JavaScript:JavaScript RegExp 对象 1.返回顶部 1. JavaScript RegExp 对象 RegExp 对象 RegEx ...
- 编辑器UEditor入门学习
优点:非常使用的富文本编辑器,对比于之前使用的summernote,比前者多出了更多的字体图标 废话少说,直接步骤: 1.导入资源(全部放在单独的文件下即可,下图为“UEditor”文件夹) 2.引用 ...