From 8bc3c99b8afeb76573bafa7a7ec51b551f500662 Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期六, 12 十二月 2020 17:05:42 +0800 Subject: [PATCH] 优化 --- src/main/java/com/hx/util/gaode/AddressCode.java | 42 ++++++++++++++ src/main/java/com/hx/common/service/impl/CommonServiceImpl.java | 6 ++ src/main/java/com/hx/util/StringUtils.java | 10 +++ src/main/java/com/hx/common/service/impl/CommonDaoImpl.java | 12 +++ src/main/java/com/hx/util/HttpUtil.java | 30 +++++++-- src/main/java/com/hx/common/service/CommonService.java | 8 ++ src/main/java/com/hx/util/gaode/GaoDeMapUtil.java | 53 +++++++++++++++++ src/main/java/com/hx/common/dao/CommonDao.java | 7 ++ 8 files changed, 159 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/hx/common/dao/CommonDao.java b/src/main/java/com/hx/common/dao/CommonDao.java index 138a0c5..3a4ccad 100644 --- a/src/main/java/com/hx/common/dao/CommonDao.java +++ b/src/main/java/com/hx/common/dao/CommonDao.java @@ -27,6 +27,13 @@ */ <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟 + * @param mapperClass mapper绫� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist * @param mapperClass mapper绫� * @param sqlSentence 鏌ヨ鍙傛暟绫� diff --git a/src/main/java/com/hx/common/service/CommonService.java b/src/main/java/com/hx/common/service/CommonService.java index 64164ec..2d35f9f 100644 --- a/src/main/java/com/hx/common/service/CommonService.java +++ b/src/main/java/com/hx/common/service/CommonService.java @@ -27,6 +27,14 @@ */ <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟 + * @param mapperClass mapper绫� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence); + + /**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist * @param mapperClass mapper绫� * @param sqlSentence 鏌ヨ鍙傛暟绫� diff --git a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java index 388fcd3..16ef362 100644 --- a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java @@ -28,12 +28,20 @@ return sqlSessionFactory.openSession().insert(getStatement(mapperClass,"insert"),entity); } - /**鑾峰彇鏉℃暟*/ + /**鑾峰彇鏉℃暟 + * @return*/ @Override - public <T extends Serializable> int selectCount(Class<?> mapperClass,SqlSentence sqlSentence) { + public <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence) { return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCount"),sqlSentence); } + /**鑾峰彇鏉℃暟 + * @return*/ + @Override + public <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence) { + return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCountSql"),sqlSentence); + } + /**鑾峰彇鍒楄〃*/ @Override public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) { diff --git a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java index 7d911eb..c2822ec 100644 --- a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java @@ -31,6 +31,12 @@ return commonDao.selectCount(mapperClass,sqlSentence); } + /**鑾峰彇鏉℃暟*/ + @Override + public <T extends Serializable> int selectCountSql(Class<?> mapperClass,SqlSentence sqlSentence) { + return commonDao.selectCountSql(mapperClass,sqlSentence); + } + /**鑾峰彇鍒楄〃*/ @Override public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) { diff --git a/src/main/java/com/hx/util/HttpUtil.java b/src/main/java/com/hx/util/HttpUtil.java index 26caf6f..f64532c 100644 --- a/src/main/java/com/hx/util/HttpUtil.java +++ b/src/main/java/com/hx/util/HttpUtil.java @@ -6,6 +6,8 @@ import org.springframework.web.multipart.MultipartFile; import javax.activation.MimetypesFileTypeMap; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; @@ -23,6 +25,27 @@ /**鍥炶溅鎹㈣,鐢ㄤ簬涓�琛岀殑缁撳熬*/ private static final String LINE_END = "\r\n"; + public static String getInputStream(HttpServletRequest request) throws Exception { + ServletInputStream stream = null; + BufferedReader reader = null; + StringBuffer sb = new StringBuffer(); + try { + stream = request.getInputStream(); + // 鑾峰彇鍝嶅簲 + reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + //logger.error(e); + throw new RuntimeException("璇诲彇杩斿洖鏀粯鎺ュ彛鏁版嵁娴佸嚭鐜板紓甯革紒"); + } finally { + reader.close(); + } + //logger.info("杈撳叆娴佽繑鍥炵殑鍐呭锛�" + sb.toString()); + return sb.toString(); + } public static String post(String requestUrl, String accessToken, String params) throws Exception { @@ -162,13 +185,6 @@ // //System.out.println("HttpURLUtil:"+wxMsgXml); } catch (Exception e) { e.printStackTrace(); - obj = new JSONObject(); - try { - obj.put("status", 1); - obj.put("errMsg", e.getMessage()); - } catch (JSONException e1) { - e1.printStackTrace(); - } } finally { if (con != null) { con.disconnect(); diff --git a/src/main/java/com/hx/util/StringUtils.java b/src/main/java/com/hx/util/StringUtils.java index 6710290..a25bd93 100644 --- a/src/main/java/com/hx/util/StringUtils.java +++ b/src/main/java/com/hx/util/StringUtils.java @@ -228,6 +228,16 @@ return null == str || str.trim().length() == 0 || "undefined".equals(str); } + /**鍒ゆ柇绌�*/ + public static boolean isNull(String str) { + return null == str || str.trim().length() == 0 || "undefined".equals(str); + } + + /**鍒ゆ柇闈炵┖*/ + public static boolean noNull(String str) { + return null != str && str.trim().length() != 0 && !"undefined".equals(str); + } + /** * 鍒ゆ柇瀛楃鍐呭涓槸鍚﹀瓨鍦ㄤ竴涓负绌虹殑瀛楃锟�? * <p> diff --git a/src/main/java/com/hx/util/gaode/AddressCode.java b/src/main/java/com/hx/util/gaode/AddressCode.java new file mode 100644 index 0000000..c634312 --- /dev/null +++ b/src/main/java/com/hx/util/gaode/AddressCode.java @@ -0,0 +1,42 @@ +package com.hx.util.gaode; + +/** + * 鑾峰彇鍦板潃缁忕含搴� + * 鍝嶅簲瀹炰綋鍖� + */ +public class AddressCode { + + /**缁忓害*/ + private String lng; + /**缁村害*/ + private String lat; + /**鍩庡競缂栫爜*/ + private String cityCode; + + public AddressCode() { + } + + public String getLng() { + return lng; + } + + public void setLng(String lng) { + this.lng = lng; + } + + public String getLat() { + return lat; + } + + public void setLat(String lat) { + this.lat = lat; + } + + public String getCityCode() { + return cityCode; + } + + public void setCityCode(String cityCode) { + this.cityCode = cityCode; + } +} diff --git a/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java b/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java new file mode 100644 index 0000000..cdf9cf4 --- /dev/null +++ b/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java @@ -0,0 +1,53 @@ +package com.hx.util.gaode; + +import com.hx.util.HttpUtil; +import com.hx.util.StringUtils; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +/** + * 楂樺痉鍦板浘宸ュ叿 + * @author mgchen + * + */ +public class GaoDeMapUtil { + + /**鑾峰彇缁忕含搴�*/ + public static String URL_CODE = "https://restapi.amap.com/v3/geocode/geo"; + + /** + * 鑾峰彇鍦板潃鐨勭粡绾害 + * @param address + * @param key + * @return + */ + public static AddressCode addressCode(String address,String key) { + AddressCode addressCode = null; + //URL_CODE = URL_CODE+"?key="+key+"&address="+URLEncoder.encode(address,"UTF-8");//鍦板潃缂栬瘧 + URL_CODE = URL_CODE+"?key="+key+"&address="+address; + JSONObject data = HttpUtil.HttpURLUtil(URL_CODE,null); + if(data.optInt("status",0) == 1){ + JSONArray geocodes = data.getJSONArray("geocodes"); + if(geocodes.size()==1){ + JSONObject locationObj = geocodes.getJSONObject(0); + String location = locationObj.optString("location"); + if(StringUtils.noNull(location)){ + String[] locations = location.split(","); + if(locations.length != 2){ + return null; + } + addressCode = new AddressCode(); + addressCode.setLng(locations[0]); + addressCode.setLat(locations[1]); + addressCode.setCityCode(locationObj.optString("citycode")); + } + } + } + return addressCode; + } + + +} -- Gitblit v1.8.0