学习项目已上传至码云,如果有需要的朋友可以自行下载
项目下载地址:https://gitee.com/guodan2503/springcloud20
新建module,springcloud20——New——Module
这一步选择的一定是maven项目,Module SDK版本要与父项目保持一致,点击next
填写对应的Artifactid:cloud-payment,点击Finish完成子模块的创建
子模块创建完成之后父pom中会自动添加子模块信息
<!--子模块-->
<modules>
<module>cloud-payment</module>
</modules>
引入自己需要的依赖,最终使用的还是子模块中的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud20</artifactId>
<groupId>org.demo.springcloud20</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-payment</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在resources文件下创建application.yml,配置文件代码入下:
server:
port: 8001
Spring:
application:
name:cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.driver
url: jdbc:mysql://39.98.155.166:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username:
password:
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities
创建项目主启动PaymentApplication.java
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 项目启动类
*/
@SpringBootApplication
public class PaymentApplitation {
public static void main(String[] args) {
SpringApplication.run(PaymentApplitation.class,args);
}
}
在开始写业务类之前需要创建controller、entity、mapper、service、.xml文件,在此下面编写业务类
创建支付表
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '支付id',
`serial` VARCHAR(200) DEFAULT '' COMMENT '支付流水号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=210 DEFAULT CHARSET=utf8mb4 COMMENT='支付信息表';
<?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.springcloud.mapper.PaymentMapper">
<!--映射信息-->
<resultMap id="PaymentResult" type="com.springcloud.entity.PaymentEntity">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<!--新增支付信息-->
<insert id="savePayment" parameterType="com.springcloud.entity.PaymentEntity" useGeneratedKeys="true" keyProperty="id">
insert into payment (serial) values (#{serial})
</insert>
<!--通过支付id查询信息-->
<select id="selectPaymentById" parameterType="long" resultMap="PaymentResult">
select * from payment where id =#{id}
</select>
</mapper>
注意此代码中没有setter与getter,主要是引用了lombok插件省去setter与getter,改用lombok注解
package com.springcloud.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 支付实体类
* AllArgsConstructor 全参
* NoArgsConstructor 空参
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PaymentEntity implements Serializable {
/**
* 支付id
*/
private Long id;
/**
* 支付流水号
*/
private String serial;
}
package com.springcloud.mapper;
import com.springcloud.entity.PaymentEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 支付 数据持久层接口
*/
@Mapper
public interface PaymentMapper {
/**
* 新增支付信息
* @param payment 支付实体类
* @return 结果
*/
public int savePayment(PaymentEntity payment);
/**
* 通过支付id查询
* @param id 支付id
* @return 查询结果
*/
public PaymentEntity selectPaymentById(@Param("id") Long id);
}
service服务层代码
package com.springcloud.service;
import com.springcloud.entity.PaymentEntity;
import org.apache.ibatis.annotations.Param;
/**
* 支付 服务层
*/
public interface IPaymentService {
/**
* 新增支付信息
* @param payment 支付实体类
* @return 结果
*/
public int savePayment(PaymentEntity payment);
/**
* 通过支付id查询
* @param id 支付id
* @return 查询结果
*/
public PaymentEntity selectPaymentById(@Param("id") Long id);
}
impl 业务实现层
package com.springcloud.service.impl;
import com.springcloud.entity.PaymentEntity;
import com.springcloud.mapper.PaymentMapper;
import com.springcloud.service.IPaymentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 支付 业务实现层
*/
@Service
public class PaymentServiceImpl implements IPaymentService {
@Autowired
private PaymentMapper paymentMapper;
/**
* 新增支付信息
* @param payment 支付实体类
* @return 结果
*/
@Override
public int savePayment(PaymentEntity payment){
return paymentMapper.savePayment(payment);
}
/**
* 通过支付id查询
* @param id 支付id
* @return 查询结果
*/
@Override
public PaymentEntity selectPaymentById(@Param("id") Long id){
return paymentMapper.selectPaymentById(id);
}
}
package com.springcloud.controller;
import com.springcloud.entity.PaymentEntity;
import com.springcloud.service.IPaymentService;
import com.springcloud.util.AjaxResult;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 支付 调用类(控制层)
*/
@RestController
@Slf4j
public class PaymentController {
@Autowired
private IPaymentService paymentService;
/**
* 新增支付信息
* @param payment 支付实体类
* @return 结果
*/
@PostMapping(value = "/payment/save")
public AjaxResult savePayment(PaymentEntity payment){
int result = paymentService.savePayment(payment);
log.info("*******插入结果"+result);
if (result>0){
return new AjaxResult(200,"插入成功",result);
}else {
return new AjaxResult(500,"插入失败",null);
}
}
/**
* 通过id查询支付信息
* @param id 支付id
* @return 数据结果
*/
@GetMapping(value = "/payment/select/{id}")
public AjaxResult selectPaymentById(@PathVariable("id") Long id){
PaymentEntity payment = paymentService.selectPaymentById(id);
if (payment != null){
return new AjaxResult(200,"查询成功",payment);
}else {
return new AjaxResult(500,"查询失败",null);
}
}
}
启动项目
测试查询结果
借助postman测试插入结果
插入数据库结果
到此一个子项目创建完成,您的赞是对我最好的支持