Integrating Alipay Payment into an SSM (Spring + Spring MVC + MyBatis) Framework
This tutorial walks through the complete process of configuring a sandbox Alipay environment, setting up keys and URLs, defining the database schema, implementing DAO and service layers, and integrating Alipay payment flow into a Java SSM project with detailed code examples and screenshots.
This guide explains how to implement Alipay payment functionality within an SSM (Spring, Spring MVC, MyBatis) project. It is divided into two main parts: testing the Alipay sandbox environment and integrating Alipay into the SSM framework.
1. Alipay sandbox testing – Download the official Alipay demo, import it into Eclipse, and configure AlipayConfig with your app_id, gateway URL, merchant private key, and Alipay public key. Generate a 2048‑bit RSA key pair using the provided tool, copy the generated keys into the configuration UI, and set the server asynchronous notification URL ( notify_url ) and synchronous return URL ( return_url ). Run the demo, use the sandbox buyer account, and verify that the payment succeeds and the signature verification passes.
2. Integration into SSM
Project architecture – Spring + Spring MVC + MyBatis, MySQL database, Tomcat 9, JDK 9, IDE IntelliJ/IDEA. The application supports both Alipay and WeChat payments.
Database schema – Four tables are created: user , orders , flow , and product . Example DDL:
drop table if exists user;
create table user (
id varchar(20) not null,
username varchar(128),
sex varchar(20),
primary key (id)
) comment '用户表';
create table `flow` (
`id` varchar(20) NOT NULL,
`flow_num` varchar(20) DEFAULT NULL COMMENT '流水号',
`order_num` varchar(20) DEFAULT NULL COMMENT '订单号',
`product_id` varchar(20) DEFAULT NULL COMMENT '产品主键ID',
`paid_amount` varchar(11) DEFAULT NULL COMMENT '支付金额',
`paid_method` int(11) DEFAULT NULL COMMENT '支付方式\n 1:支付宝\n 2:微信',
`buy_counts` int(11) DEFAULT NULL COMMENT '购买个数',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
primary key (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='流水表';
-- similar DDL for `orders` and `product` tablesDAO layer – Example OrdersMapper interface with typical CRUD methods:
public interface OrdersMapper {
int countByExample(OrdersExample example);
int deleteByExample(OrdersExample example);
int deleteByPrimaryKey(String id);
int insert(Orders record);
int insertSelective(Orders record);
List
selectByExample(OrdersExample example);
Orders selectByPrimaryKey(String id);
int updateByExampleSelective(@Param("record") Orders record, @Param("example") OrdersExample example);
int updateByExample(@Param("record") Orders record, @Param("example") OrdersExample example);
int updateByPrimaryKeySelective(Orders record);
int updateByPrimaryKey(Orders record);
}Service layer – OrdersService defines business operations such as creating an order, updating order status, and retrieving an order by ID:
public interface OrdersService {
void saveOrder(Orders order);
void updateOrderStatus(String orderId, String alpayFlowNum, String paidAmount);
Orders getOrderById(String orderId);
}Controller layer – AlipayController builds the Alipay request, sets return and notify URLs, and returns the generated HTML form to the browser:
@RequestMapping(value = "/goAlipay", produces = "text/html; charset=UTF-8")
@ResponseBody
public String goAlipay(String orderId, HttpServletRequest request, HttpServletResponse response) throws Exception {
Orders order = orderService.getOrderById(orderId);
Product product = productService.getProductById(order.getProductId());
AlipayClient alipayClient = new DefaultAlipayClient(
AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key,
"json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
String out_trade_no = orderId;
String total_amount = order.getOrderAmount();
String subject = product.getName();
String body = "用户订购商品个数:" + order.getBuyCounts();
String timeout_express = "1c";
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"timeout_express\":\"" + timeout_express + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
String result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}Front‑end JSP page – products.jsp displays a product list using JSTL, provides a "Buy" link that calls /alipay/goConfirm.action?productId=... , and includes a hidden field with the context path. The page also contains a simple JavaScript block that reads the hidden field.
Payment flow – After clicking "Buy", the user is taken to an order confirmation page, the order is persisted, and the goAlipay endpoint redirects the browser to Alipay's payment gateway. Upon successful payment, Alipay sends an asynchronous notification to notify_url and redirects the user back to return_url , where the application updates the order status and records the payment flow.
Problem solving – The tutorial notes that the sandbox gateway differs from the production gateway; using the wrong gateway leads to app_id errors. The correct sandbox gateway URL must be configured.
All source code, configuration screenshots, and database scripts are provided, enabling readers to reproduce the entire Alipay integration in their own SSM projects.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.