一、josn數據轉實體類的工具類(JsonToBeanUtil)
- 工具類(可根據自己需求更改部分代碼)
public class JsonToCityUtil { private JsonToCityUtil() {}; private static JsonToCityUtil jsonToCityUtil; public static JsonToCityUtil getInstance() { if (jsonToCityUtil == null) { jsonToCityUtil = new JsonToCityUtil(); } return jsonToCityUtil; } public List<CityObj> readJson() throws Exception { List<CityObj> list = new ArrayList<>(); // 讀取 json 文件 File file = ResourceUtils.getFile("classpath:static/cityJson.json"); String jsonData = jsonRead(file); JSONArray array = JSONArray.parseArray(jsonData); //裏面的參數名可自行更改自己所需的 for (int i = 0; i < array.size(); i++) { JSONObject jsonObject2 = array.getJSONObject(i); String city_name = jsonObject2.getString("city_name"); String city_code = jsonObject2.getString("city_code"); String pid = jsonObject2.getString("pid"); String _id = jsonObject2.getString("_id"); String id = jsonObject2.getString("id"); // 把 讀取到 jsonObject2 弄成一個個的對象 CityObj cityObj = new CityObj(); cityObj.setCity_name(city_name); cityObj.setCity_code(city_code); cityObj.setPid(Integer.parseInt(pid)); cityObj.setId(Integer.parseInt(id)); cityObj.set_id(Integer.parseInt(_id)); System.out.println(cityObj); // 把對象 放到了集合中.. list.add(cityObj); } return list; } private String jsonRead(File file) { FileInputStream is = null; StringBuilder stringBuilder = null; try { /** * 文件有內容纔去讀文件 */ is = new FileInputStream(file); InputStreamReader streamReader = new InputStreamReader(is, "utf-8"); BufferedReader reader = new BufferedReader(streamReader); String line; stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { // stringBuilder.append(line); stringBuilder.append(line); } reader.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return String.valueOf(stringBuilder); } }
- 實體類及json數據(例子)
@Data
相當於getter和setter方法
josn文件數據:@Data public class CityObj implements Serializable { private int _id; private int id; private int pid; private String city_code; private String city_name; }
key,value值可自定義[ { "_id": 1989, "id": 2667, "pid": 317, "city_code": "101110201", "city_name": "三原縣" }, { "_id": 1990, "id": 2668, "pid": 317, "city_code": "101110205", "city_name": "涇陽縣" } ]
二、redis工具類
@Component
public class RedisClient {
@Autowired
private StringRedisTemplate redisTpl; //jdbcTemplate
/**
* 功能描述:設置key-value到redis中
*
* @param key
* @param value
* @return
*/
public boolean set(String key, String value) {
try {
redisTpl.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 功能描述:通過key獲取緩存裏面的值
*
* @param key
* @return
*/
public String get(String key) {
return redisTpl.opsForValue().get(key);
}
@Autowired
private StringRedisTemplate redisTemplate;
/* *//**
* 通過字符串key獲取值
*
* @param key 鍵
* @return 值
*//*
public String get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
*//**
* 普通緩存放入
*
* @param key 鍵
* @param value 值
* @return true成功 false失敗
*//*
public boolean set(String key, String value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}*/
/**
* 功能描述:設置某個key過期時間
*
* @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
* @return
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 遞增
*
* @param key 鍵
* @return
*/
public long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 遞減
*
* @param key 鍵
* @param delta 要減少幾
* @return
*/
public long decr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, -delta);
}
}
三、jedis連接池工具類
- 工具類
/** * 連接池工具類 */ public class JedisUtil { private static JedisPool jedisPool; static { InputStream is = JedisUtil.class.getClassLoader().getResourceAsStream("jedis.properties"); Properties properties = new Properties(); try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Integer.valueOf(properties.getProperty("maxIdle"))); config.setMaxTotal(Integer.valueOf(properties.getProperty("maxTotal"))); jedisPool = new JedisPool(config,properties.getProperty("host"),Integer.valueOf(properties.getProperty("port"))); } public static Jedis getJedis(){ return jedisPool.getResource(); } }
- jedis.properties文件
host=127.0.0.1 port=6379 maxTotal=50 maxIdle=10