时间:2022-08-06 22:27:02 | 来源:网站运营
时间:2022-08-06 22:27:02 来源:网站运营
spring-boot-starter-web spring-boot-starter-data-redis mybatis-spring-boot-starter mysql-connector-java
基本目录结构 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驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 引入第三方数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> 加入配置文件 #可以自动识别 #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 开启类增加mapper扫描 @MapperScan("net.xdclass.xdvideo.mapper")
参考语法 mybatis - MyBatis 3<!-- 分页插件依赖 --> <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; }
基本原理:通过mybatis plugin 增加拦截器,然后拼装分页 { id:888, name:'小D', expire:10000 } funtion 加密(object, appsecret){ xxxx return base64( token); } function 解密(token ,appsecret){ xxxx //成功返回true,失败返回false }
2、优点:<!-- JWT相关 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.7.0</version> </dependency>
开发生产token的方法#微信开放平台配置 wxopen.appid= wxopen.appsecret= #重定向url wxopen.redirect_url=http://test/pub/api/v1/wechat/user/callback1 wechatConfig里面增加属性/** * 微信开放平台二维码连接 */ 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的时候使用 --> <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、响应前端数据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:公众号唯一标识appsecret:公众号的秘钥mch_id:商户号,申请微信支付的时候分配的key:支付交易过程生成签名的秘钥,设置路径 微信商户平台(pay.weixin.qq.com)-->账户中心-->账户设置-->API安全-->密钥设置
3、和微信支付交互方式Dao层VideoOrder增删改查开发 @Insert("") @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") //keyProperty java对象的属性;keyColumn表示数据库的字段 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, 即用来标识一笔单,也用做 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#微信商户平台wxpay.mer_id=1503808832wxpay.key=xdclasss20182018xdclass2018x018dwxpay.callback=16web.tunnel.qydev.com/pub/api/v1/wechat/order/callback1
第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置参数SortedMap<String, String> params = new TreeMap<>(); params.put("appid", wxPayConfig.getAppId()); //公众账号ID params.put("mch_id", wxPayConfig.getMchId()); //商户号 params.put("nonce_str", CommonUtil.generateNonceStr()); //随机字符串 params.put("body", videoOrder.getVideoTitle()); // 商品描述 params.put("out_trade_no", videoOrder.getOutTradeNo()); //商户订单号,商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|* 且在同一个商户号下唯一 params.put("total_fee", videoOrder.getTotalFee().toString()); //标价金额 分params.put("spbill_create_ip", videoOrder.getIp()); params.put("notify_url", wxPayConfig.getDomain()+wxPayConfig.getCallbackUrl()); //通知地址 params.put("trade_type", "NATIVE"); //交易类型 JSAPI 公众号支付 NATIVE 扫码支付 APP APP支付 //生成签名 String sign = WXPayUtil.createSign(params, wxPayConfig.getKey());params.put("sign", sign); //参数转xmlString requestXMl = WXPayUtil.mapToXml(params);生成签名后,通过工具去校验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、使用微信扫码完成支付//读取回调输入流 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) ; 回调数据: <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> 转成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、注意点:response.setContentType("text/xml"); response.getWriter().println("success");
关键词:支付,目录,实战,教育,项目