美文网首页
2、支付模块构建

2、支付模块构建

作者: 沁园Yann | 来源:发表于2020-06-09 17:03 被阅读0次

    1、右键点击父工程,新建子模块


    image.png
    image.png
    image.png

    2、创建子模块后,在父工程pom文件中可以看到子模块声明,将这个声明移一下位置


    image.png

    3、在子模块 pom 文件中声明需要用到的依赖包

    <dependencies>
            <!--包含了sleuth+zipkin-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>
            <!--eureka-client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <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>
            <!--mysql-connector-java-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--jdbc-->
            <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>
    

    4、在子模块 \src\main\resources 文件夹下创建配置文件 application.yml ,并写入相关配置。


    image.png
    server:
      port: 8001
    
    spring:
      application:
        name: cloud-payment-service
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
        driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
        url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: root
        password: 123456
    
    mybatis:
      mapperLocations: classpath:mapper/*.xml    #扫描mapper文件目录
      type-aliases-package: com.yann.springcloud.entities    # 所有Entity别名类所在包
    

    5、创建主启动类
    新建package,并在包下创建启动类


    image.png
    @SpringBootApplication
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class, args);
        }
    }
    

    6、创建订单实体和响应数据实体


    image.png

    订单实体

    @Data
    @NoArgsConstructor//无参构造
    @AllArgsConstructor//全参构造
    public class Payment {
        private Long id;
        private String serial;
    }
    

    响应数据实体

    @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);
        }
    }
    

    7、创建订单Dao接口

    @Mapper
    public interface PaymentDao {
        public int create(Payment payment);
        public Payment getPaymentById(@Param("id") Long id);
    }
    

    8、编写订单服务接口和实现类

    public interface PaymentService {
        public int create(Payment payment);
        public Payment getPaymentById(@Param("id") Long id);
    }
    
    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Resource
        private PaymentDao paymentDao;
    
        @Override
        public int create(Payment payment) {
            return paymentDao.create(payment);
        }
    
        @Override
        public Payment getPaymentById(Long id) {
            return paymentDao.getPaymentById(id);
        }
    }
    

    9、在resource下创建mapper文件夹,并创建PaymentMapper.xml

    <?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.atguigu.springcloud.dao.PaymentDao">
      <!-- useGeneratedKeys="true" 插入成功返回一个整数,数字大于0表示插入成功 -->
        <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
            insert into payment(serial)  values(#{serial});
        </insert>
    
        <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <id column="serial" property="serial" jdbcType="VARCHAR"/>
        </resultMap>
      <!-- resultMap="BaseResultMap" 结果数据映射 -->
        <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
            select * from payment where id=#{id};
        </select>
    </mapper>
    

    10、创建控制器测试

    @RestController
    @Slf4j
    public class PaymentController {
        @Resource
        private PaymentService paymentService;
    
        @Value("${server.port}")
        private String serverPort;
    
        @Resource
        private DiscoveryClient discoveryClient;
    
        @PostMapping(value = "/payment/create")
        public CommonResult create(@RequestBody Payment payment)
        {
            int result = paymentService.create(payment);
            log.info("*****插入结果:"+result);
    
            if(result > 0)
            {
                return new CommonResult(200,"插入数据库成功,serverPort: "+serverPort,result);
            }else{
                return new CommonResult(444,"插入数据库失败",null);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:2、支付模块构建

          本文链接:https://www.haomeiwen.com/subject/clomvhtx.html