時(shí)間:2022-08-06 22:27:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2022-08-06 22:27:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
spring-boot-starter-web spring-boot-starter-data-redis mybatis-spring-boot-starter mysql-connector-java
基本目錄結(jié)構(gòu) controller service impl mapper utils domain config interceptorer dto
<!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- MySQL的JDBC驅(qū)動(dòng)包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 引入第三方數(shù)據(jù)源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> 加入配置文件 #可以自動(dòng)識(shí)別 #spring.datasource.driver-class-name =com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/movie?useUnicode=true&characterEncoding=utf-8 spring.datasource.username =root spring.datasource.password =password spring.datasource.type =com.alibaba.druid.pool.DruidDataSource 開(kāi)啟類增加mapper掃描 @MapperScan("net.xdclass.xdvideo.mapper")
參考語(yǔ)法 mybatis - MyBatis 3<!-- 分頁(yè)插件依賴 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency> 增加配置文件 @Configuration public class MyBatisConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum","true"); p.setProperty("rowBoundsWithCount","true"); p.setProperty("reasonable","true"); pageHelper.setProperties(p); return pageHelper; }
基本原理:通過(guò)mybatis plugin 增加攔截器,然后拼裝分頁(yè) { id:888, name:'小D', expire:10000 } funtion 加密(object, appsecret){ xxxx return base64( token); } function 解密(token ,appsecret){ xxxx //成功返回true,失敗返回false }
2、優(yōu)點(diǎn):<!-- JWT相關(guān) --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.7.0</version> </dependency>
開(kāi)發(fā)生產(chǎn)token的方法#微信開(kāi)放平臺(tái)配置 wxopen.appid= wxopen.appsecret= #重定向url wxopen.redirect_url=http://test/pub/api/v1/wechat/user/callback1 wechatConfig里面增加屬性/** * 微信開(kāi)放平臺(tái)二維碼連接 */ private final static String OPEN_QRCODE_URL= "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect";
2、拼接URL <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <!-- gson工具,封裝http的時(shí)候使用 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency>
String accessToken = request.getHeader("token"); if(accessToken == null){ accessToken = request.getParameter("token"); } if (accessToken != null ) { Claims claims = JWTUtils.checkJWT(accessToken); Integer id = (Integer)claims.get("id"); String name = (String)claims.get("name"); request.setAttribute("user_id",id); request.setAttribute("name",name); //普通用戶 return true; } return false;
3、配置攔截器@Configuration InterceptorConfig implements WebMvcConfigurer @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/api/v1/*/**"); WebMvcConfigurer.super.addInterceptors(registry);
4、響應(yīng)前端數(shù)據(jù)public static void sendJsonMessage(HttpServletResponse response, Object obj) throws Exception { Gson g = new Gson(); response.setContentType("application/json; charset=utf-8"); PrintWriter writer = response.getWriter(); writer.print(g.toJson(obj)); writer.close(); response.flushBuffer(); }
appid:公眾號(hào)唯一標(biāo)識(shí)appsecret:公眾號(hào)的秘鑰mch_id:商戶號(hào),申請(qǐng)微信支付的時(shí)候分配的key:支付交易過(guò)程生成簽名的秘鑰,設(shè)置路徑 微信商戶平臺(tái)(pay.weixin.qq.com)-->賬戶中心-->賬戶設(shè)置-->API安全-->密鑰設(shè)置
3、和微信支付交互方式Dao層VideoOrder增刪改查開(kāi)發(fā) @Insert("") @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") //keyProperty java對(duì)象的屬性;keyColumn表示數(shù)據(jù)庫(kù)的字段 int insert(VideoOrder order); @Select("SELECT * FROM video_order WHERE id = #{id}") VideoOrder findById(int id); @Select("SELECT * FROM video_order WHERE id = #{id}") VideoOrder findByOutTradeNo(String outTradeNo); @Delete("DELETE FROM video_order WHERE id =#{id}") int delete(int id);
//生成 uuid, 即用來(lái)標(biāo)識(shí)一筆單,也用做 nonce_str public static String generateUUID() { return UUID.randomUUID().toString().replaceAll("-", "") .substring(0, 32); } //MD5工具類 public static String MD5(String data) { try { java.security.MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } catch (Exception exception) { } return null; }
2、WXPayUtil包含方法 xmlToMap,mapToXml,createSign,isCorrectPaySign#微信商戶平臺(tái)wxpay.mer_id=1503808832wxpay.key=xdclasss20182018xdclass2018x018dwxpay.callback=16web.tunnel.qydev.com/pub/api/v1/wechat/order/callback1
第一步,設(shè)所有發(fā)送或者接收到的數(shù)據(jù)為集合M,將集合M內(nèi)非空參數(shù)值的參數(shù)按照參數(shù)名ASCII碼從小到大排序(字典序),使用URL鍵值對(duì)的格式(即key1=value1&key2=value2…)拼接成字符串stringA。第二步,在stringA最后拼接上key得到stringSignTemp字符串,并對(duì)stringSignTemp進(jìn)行MD5運(yùn)算,再將得到的字符串所有字符轉(zhuǎn)換為大寫(xiě),得到sign值signValue。key設(shè)置路徑:微信商戶平臺(tái)(pay.weixin.qq.com)-->賬戶設(shè)置-->API安全-->密鑰設(shè)置參數(shù)SortedMap<String, String> params = new TreeMap<>(); params.put("appid", wxPayConfig.getAppId()); //公眾賬號(hào)ID params.put("mch_id", wxPayConfig.getMchId()); //商戶號(hào) params.put("nonce_str", CommonUtil.generateNonceStr()); //隨機(jī)字符串 params.put("body", videoOrder.getVideoTitle()); // 商品描述 params.put("out_trade_no", videoOrder.getOutTradeNo()); //商戶訂單號(hào),商戶系統(tǒng)內(nèi)部訂單號(hào),要求32個(gè)字符內(nèi),只能是數(shù)字、大小寫(xiě)字母_-|* 且在同一個(gè)商戶號(hào)下唯一 params.put("total_fee", videoOrder.getTotalFee().toString()); //標(biāo)價(jià)金額 分params.put("spbill_create_ip", videoOrder.getIp()); params.put("notify_url", wxPayConfig.getDomain()+wxPayConfig.getCallbackUrl()); //通知地址 params.put("trade_type", "NATIVE"); //交易類型 JSAPI 公眾號(hào)支付 NATIVE 掃碼支付 APP APP支付 //生成簽名 String sign = WXPayUtil.createSign(params, wxPayConfig.getKey());params.put("sign", sign); //參數(shù)轉(zhuǎn)xmlString requestXMl = WXPayUtil.mapToXml(params);生成簽名后,通過(guò)工具去校驗(yàn)https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=20_1
<xml><return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx5beac15ca207c40c]]></appid> <mch_id><![CDATA[1503809911]]></mch_id> <nonce_str><![CDATA[Go5gDC2CYL5HvizG]]></nonce_str> <sign><![CDATA[BC62592B9A94F5C914FAAD93ADE7662B]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx262207318328044f75c9ebec2216783076]]></prepay_id> <trade_type><![CDATA[NATIVE]]></trade_type> <code_url><![CDATA[weixin://wxpay/bizpayurl?pr=hFq9fX6]]></code_url> </xml>
3、獲取code_url加入依賴 <!-- google二維碼生成包 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>2.0</version> </dependency>
2、使用微信掃碼完成支付//讀取回調(diào)輸入流 StringBuffer sb = new StringBuffer(); InputStream inputStream = request.getInputStream(); String line ; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((line = in.readLine()) != null){ sb.append(line); } in.close(); inputStream.close(); //解析xml成map //{transaction_id=4200000141201805266700247361, nonce_str=cbb67accd8044c9ea48f3318b99d88e4, bank_type=CFT, openid=oiNKG03vVY4PHlGUEwT-ztFo8K8Y, sign=0575804DBE4E9FFF6545046FA062BC4C, fee_type=CNY, mch_id=1503809911, cash_fee=1, out_trade_no=78902e694bab485b8e7745b61e05dbfe, appid=wx5beac15ca207c40c, total_fee=1, trade_type=NATIVE, result_code=SUCCESS, time_end=20180526162759, is_subscribe=Y, return_code=SUCCESS} Map<String, String> callbackMap = WXPayUtil.xmlToMap(sb.toString()); //獲取有序map SortedMap<String, String> sortedMap = CommonUtil.getSortedMap(callbackMap) ; 回調(diào)數(shù)據(jù): <xml><appid><![CDATA[wx5beac15ca207c40c]]></appid><bank_type><![CDATA[CFT]]></bank_type><cash_fee><![CDATA[10]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[1503809911]]></mch_id><nonce_str><![CDATA[de019d5f1e5d40649cd76de33f18b13e]]></nonce_str><openid><![CDATA[oiNKG03vVY4PHlGUEwT-ztFo8K8Y]]></openid><out_trade_no><![CDATA[4d8cea4a916440368583edaf82488624]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[FA799B7DF70C2BAC558E839E01EF341A]]></sign><time_end><![CDATA[20180626230347]]></time_end><total_fee>10</total_fee><trade_type><![CDATA[NATIVE]]></trade_type><transaction_id><![CDATA[4200000142201806264038572903]]></transaction_id></xml> 轉(zhuǎn)成map: {transaction_id=4200000142201806264038572903, nonce_str=de019d5f1e5d40649cd76de33f18b13e, bank_type=CFT, openid=oiNKG03vVY4PHlGUEwT-ztFo8K8Y, sign=FA799B7DF70C2BAC558E839E01EF341A, fee_type=CNY, mch_id=1503809911, cash_fee=10, out_trade_no=4d8cea4a916440368583edaf82488624, appid=wx5beac15ca207c40c, total_fee=10, trade_type=NATIVE, result_code=SUCCESS, time_end=20180626230347, is_subscribe=Y, return_code=SUCCESS}
2、注意點(diǎn):response.setContentType("text/xml"); response.getWriter().println("success");
關(guān)鍵詞:支付,目錄,實(shí)戰(zhàn),教育,項(xiàng)目
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。