@Component
public class GenerateToken {
@Autowired
private RedisUtil redisUtil;
/**
* 生成令牌
*
* @param prefix
* 令牌key前缀
* @param redisValue
* redis存放的值
* @return 返回token
*/
public String createToken(String keyPrefix, String redisValue) {
return createToken(keyPrefix, redisValue, null);
}
/**
* 生成令牌
*
* @param prefix
* 令牌key前缀
* @param redisValue
* redis存放的值
* @param time
* 有效期
* @return 返回token
*/
public String createToken(String keyPrefix, String redisValue, Long time) {
if (StringUtils.isEmpty(redisValue)) {
new Exception("redisValue Not nul");
}
String token = keyPrefix + UUID.randomUUID().toString().replace("-", "");
redisUtil.setString(token, redisValue, time);
return token;
}
/**
* 根据token获取redis中的value值
*
* @param token
* @return
*/
public String getToken(String token) {
if (StringUtils.isEmpty(token)) {
return null;
}
String value = redisUtil.getString(token);
return value;
}
/**
* 移除token
*
* @param token
* @return
*/
public Boolean removeToken(String token) {
if (StringUtils.isEmpty(token)) {
return null;
}
return redisUtil.delKey(token);
}
}
redis进行事务操作
@Component
@Scope(ConfigurableListableBeanFactory.SCOPE_PROTOTYPE)
public class RedisDataSoureceTransaction {
@Autowired
private RedisUtil redisUtil;
/**
* 数据源事务管理器
*/
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
/**
* 开始事务 采用默认传播行为
*
* @return
*/
public TransactionStatus begin() {
// 手动begin数据库事务
// 1.开启数据库的事务 事务传播行为
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
// 2.开启redis事务
redisUtil.begin();
return transaction;
}
/**
* 提交事务
*
* @param transactionStatus
* 事务传播行为
* @throws Exception
*/
public void commit(TransactionStatus transactionStatus) throws Exception {
if (transactionStatus == null) {
throw new Exception("transactionStatus is null");
}
// 支持Redis与数据库事务同时提交
dataSourceTransactionManager.commit(transactionStatus);
}
/**
* 回滚事务
*
* @param transactionStatus
* @throws Exception
*/
public void rollback(TransactionStatus transactionStatus) throws Exception {
if (transactionStatus == null) {
throw new Exception("transactionStatus is null");
}
// 1.回滚数据库事务 redis事务和数据库的事务同时回滚
dataSourceTransactionManager.rollback(transactionStatus);
// // 2.回滚redis事务
// redisUtil.discard();
}
// 如果redis的值与数据库的值保持不一致话
}
强转工具类
public class TypeCastHelper {
private static final String DEFAULT_STRING = "";
private static final int DEFAULT_INT = 0;
private static final long DEFAULT_LONG = 0L;
private static final double DEFAULT_DOUBLE = 0.0D;
private static final float DEFAULT_FLOAT = 0.0F;
private static final boolean DEFAULT_BOOLEAN = Boolean.FALSE;
public static String toString(Object obj, String defaultValue) {
return obj != null ? obj.toString() : defaultValue;
}
public static String toString(Object obj) {
return toString(obj, DEFAULT_STRING);
}
public static String toString(Integer intValue, String defaultValue) {
return intValue != null ? String.valueOf(intValue) : defaultValue;
}
public static String toString(Integer intValue) {
return toString(intValue, DEFAULT_STRING);
}
public static String toString(Long longValue, String defaultValue) {
return longValue != null ? String.valueOf(longValue) : defaultValue;
}
public static String toString(Long longValue) {
return toString(longValue, DEFAULT_STRING);
}
public static String toString(Boolean booleanValue, String defaultValue) {
return booleanValue != null ? String.valueOf(booleanValue) : defaultValue;
}
public static String toString(Boolean booleanValue) {
return toString(booleanValue, DEFAULT_STRING);
}
public static String toString(Double doubleValue, String defaultValue) {
return null != doubleValue ? String.valueOf(doubleValue) : defaultValue;
}
public static String toString(Double doubleValue) {
return toString(doubleValue, DEFAULT_STRING);
}
public static String toString(Float floatValue, String defaultValue) {
return null != floatValue ? String.valueOf(floatValue) : defaultValue;
}
public static String toString(Float floatValue) {
return toString(floatValue, DEFAULT_STRING);
}
public static int toInt(Object objectValue, int defaultValue) {
int intValue = defaultValue;
if (null != objectValue) {
try {
intValue = Integer.parseInt(toString(objectValue, toString(defaultValue)));
} catch (NumberFormatException numberFormatException) {
System.err.println(numberFormatException.getMessage());
}
}
return intValue;
}
public static int toInt(Object objectValue) {
return toInt(objectValue, DEFAULT_INT);
}
public static int toInt(String stringValue, int defaultValue) {
int intValue = defaultValue;
if (null != stringValue && stringValue.length() > 0) {
try {
intValue = Integer.parseInt(stringValue);
} catch (NumberFormatException numberFormatException) {
System.err.println(numberFormatException.getMessage());
}
}
return intValue;
}
public static int toInt(Long longValue, int defaultValue) {
int intValue = defaultValue;
if (null != longValue) {
intValue = longValue.intValue();
}
return intValue;
}
public static int toInt(Long longValue) {
return toInt(longValue, DEFAULT_INT);
}
public static int toInt(Boolean booleanValue, int defaultValue) {
int intValue = defaultValue;
if (null != booleanValue) {
intValue = booleanValue ? 1 : 0;
}
return intValue;
}
public static int toInt(Boolean booleanValue) {
return toInt(booleanValue, DEFAULT_INT);
}
public static int toInt(Double doubleValue, int defaultValue) {
int intValue = defaultValue;
if (null != doubleValue) {
intValue = doubleValue.intValue();
}
return intValue;
}
public static int toInt(Double doubleValue) {
return toInt(doubleValue, DEFAULT_INT);
}
public static int toInt(Float floatValue, int defaultValue) {
int intValue = defaultValue;
if (null != floatValue) {
intValue = floatValue.intValue();
}
return intValue;
}
public static int toInt(Float floatValue) {
return toInt(floatValue, DEFAULT_INT);
}
public static long toLong(Object objectValue, long defaultValue) {
long longValue = defaultValue;
if (null != objectValue) {
try {
longValue = Long.parseLong(toString(objectValue, toString(defaultValue)));
} catch (NumberFormatException numberFormatException) {
System.err.println(numberFormatException.getMessage());
}
}
return longValue;
}
public static long toLong(Object objectValue) {
return toLong(objectValue, DEFAULT_LONG);
}
public static long toLong(String stringValue, long defaultValue) {
long longValue = defaultValue;
if (null != stringValue && stringValue.length() > 0) {
longValue = Long.parseLong(stringValue);
}
return longValue;
}
public static long toLong(String stringValue) {
return toLong(stringValue, DEFAULT_LONG);
}
public static long toLong(Integer intValue, long defaultValue) {
long longValue = defaultValue;
if (null != intValue) {
longValue = intValue.longValue();
}
return longValue;
}
public static long toLong(Integer intValue) {
return toLong(intValue, DEFAULT_LONG);
}
public static long toLong(Boolean booleanValue, long defaultValue) {
long longValue = defaultValue;
if (null != booleanValue) {
longValue = booleanValue ? 1L : 0L;
}
return longValue;
}
public static long toLong(Boolean booleanValue) {
return toLong(booleanValue, DEFAULT_LONG);
}
public static long toLong(Double doubleValue, long defaultValue) {
long longValue = defaultValue;
if (null != doubleValue) {
longValue = doubleValue.longValue();
}
return longValue;
}
public static long toLong(Double doubleValue) {
return toLong(doubleValue, DEFAULT_LONG);
}
public static long toLong(Float floatValue, long defaultValue) {
long longValue = defaultValue;
if (null != floatValue) {
longValue = floatValue.longValue();
}
return longValue;
}
public static long toLong(Float floatValue) {
return toLong(floatValue, DEFAULT_LONG);
}
public static boolean toBoolean(Object objectValue, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (null != objectValue) {
booleanValue = Boolean.valueOf(toString(objectValue, toString(defaultValue)));
}
return booleanValue;
}
public static boolean toBoolean(Object objectValue) {
return toBoolean(objectValue, DEFAULT_BOOLEAN);
}
public static boolean toBoolean(String stringValue, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (null != stringValue && stringValue.length() > 0) {
booleanValue = Boolean.valueOf(stringValue);
}
return booleanValue;
}
public static boolean toBoolean(String stringValue) {
return toBoolean(stringValue, DEFAULT_BOOLEAN);
}
public static boolean toBoolean(Integer intValue, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (null != intValue) {
booleanValue = intValue == 1;
}
return booleanValue;
}
public static boolean toBoolean(Integer intValue) {
return toBoolean(intValue, DEFAULT_BOOLEAN);
}
public static boolean toBoolean(Long longValue, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (null != longValue) {
booleanValue = longValue.longValue() == 1L;
}
return booleanValue;
}
public static boolean toBoolean(Long longValue) {
return toBoolean(longValue, DEFAULT_BOOLEAN);
}
public static float toFloat(Object objectValue, float defaultValue) {
float floatValue = defaultValue;
if (null != objectValue) {
try {
floatValue = Float.valueOf(toString(objectValue, toString(defaultValue)));
} catch (NumberFormatException numberFormatException) {
System.err.println(numberFormatException);
}
}
return floatValue;
}
public static float toFloat(Object objectValue) {
return toFloat(objectValue, DEFAULT_FLOAT);
}
public static float toFloat(String stringValue, float defaultValue) {
float floatValue = defaultValue;
if (null != stringValue && stringValue.length() > 0) {
try {
floatValue = Float.valueOf(stringValue);
} catch (NumberFormatException numberFormatException) {
System.err.println(numberFormatException.getMessage());
}
}
return floatValue;
}
public static float toFloat(String stringValue) {
return toFloat(stringValue, DEFAULT_FLOAT);
}
public static float toFloat(Integer intValue, float defaultValue) {
float floatValue = defaultValue;
if (null != intValue) {
floatValue = intValue.floatValue();
}
return floatValue;
}
public static float toFloat(Integer intValue) {
return toFloat(intValue, DEFAULT_FLOAT);
}
public static float toFloat(Long longValue, float defaultValue) {
float floatValue = defaultValue;
if (null != longValue) {
floatValue = longValue.floatValue();
}
return floatValue;
}
public static float toFloat(Long longValue) {
return toFloat(longValue, DEFAULT_FLOAT);
}
public static float toFloat(Double doubleValue, float defaultValue) {
float floatValue = defaultValue;
if (null != doubleValue) {
floatValue = doubleValue.floatValue();
}
return floatValue;
}
public static float toFloat(Double doubleValue) {
return toFloat(doubleValue, DEFAULT_FLOAT);
}
public static double toDouble(Object objectValue, double defaultValue) {
double doubleValue = defaultValue;
if (null != objectValue) {
doubleValue = Double.parseDouble(toString(objectValue, toString(defaultValue)));
}
return doubleValue;
}
public static double toDouble(Object objectValue) {
return toDouble(objectValue, DEFAULT_DOUBLE);
}
public static double toDouble(String stringValue, double defaultValue) {
double doubleValue = defaultValue;
if (null != stringValue && stringValue.length() > 0) {
doubleValue = Double.parseDouble(stringValue);
}
return doubleValue;
}
public static double toDouble(String stringValue) {
return toDouble(stringValue, DEFAULT_DOUBLE);
}
public static double toDouble(Integer intValue, double defaultValue) {
double doubleValue = defaultValue;
if (null != intValue) {
doubleValue = intValue.doubleValue();
}
return doubleValue;
}
public static double toDouble(Integer intValue) {
return toDouble(intValue, DEFAULT_DOUBLE);
}
public static double toDouble(Long longValue, double defaultValue) {
double doubleValue = defaultValue;
if (null != longValue) {
doubleValue = longValue.doubleValue();
}
return doubleValue;
}
public static double toDouble(Long longValue) {
return toDouble(longValue, DEFAULT_DOUBLE);
}
public static double toDouble(Float floatValue, double defaultValue) {
double doubleValue = defaultValue;
if (null != floatValue) {
doubleValue = floatValue.doubleValue();
}
return doubleValue;
}
public static double toDouble(Float floatValue) {
return toDouble(floatValue, DEFAULT_DOUBLE);
}
}
君子志邦 发布了307 篇原创文章 · 获赞 15 · 访问量 6万+ 私信 关注