数据库
对payment表插入及获取
CREATE TABLE `payment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `serial` varchar(200) DEFAULT NULL COMMENT '支付流水号', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='支付表'
application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper
entities
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this(code,message,null);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}
dao
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
mapper
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cmk.springCloud.service.PaymentService">
<insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
insert into payment(serial) values (#{serial});
</insert>
<select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
<resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
service
@Mapper
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
public int create(Payment payment){
return paymentDao.create(payment);
}
public Payment getPaymentById(@Param("id") long id){
return paymentDao.getPaymentById(id);
}
}
controller
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult create(Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果"+result);
if (result>0){
return new CommonResult(200,"插入数据库成功",result);
}else{
return new CommonResult(400,"插入数据库失败",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") long id){
Payment result = paymentService.getPaymentById(id);
log.info("****查询结果"+result);
if (result !=null){
return new CommonResult(200,"查询数据库成功",result);
}else{
return new CommonResult(400,"查询数据库失败",null);
}
}
}
main
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}