04_2-Cloud-provider-payment8001 微服务提供者Module模块

1、建 module

2、改 pom

<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>    
    
    <!--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>    
    
    <!-- 热部署 devtools -->    
    <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>
3、写 yml

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

@SpringBootApplication
public class PaymentMain8001 {

    public static viod main(String[] args) {
    
        SpringApplication.run(PaymentMain8001.class, args);
    }
}
5、业务类
  • 1、建表

# 模板
CREATE TABLE `payment`  (

  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',

  `serial` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '支付流水号',

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '支付表' 
ROW_FORMAT = Dynamic;

# 真实
create table `payment` (
`id` bigint(20) not null auto_increment comment'id',
`serial` varchar(200) default '',
primary key(`id`)
)engine=innodb auto_increment=1 default charset=utf8;

  • 2、entities
    主实体Payment类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements {

    // 这里是因为在数据库中,id 的类型是 BIGINT
    private Long id;
    private String serial;
}

Json封装体CommonResult类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    
    private Integer codo;
    private String message;
    private T       data;
    
    public CommonResult(Integer code, String message) {
        
        this(code, message, null);
    }
}
  • 3、dao
    接口PaymentDao
@Mapper
public interface PaymentDao {

    public int create(Payment payment);
    
    public Payment getPaymentById(@Param("id") Long id);
}

mybatis的映射文件PaymentMapper.xml 在
src\main\resources\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.atzwx.springcloud.dao.PaymentDao">

    <resultMap id="BaseResultMap" type="com.atzwx.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT" />
        <id column="serial" property="serial" jdbcType="VARCHAR"  />
    </resultMap>
    
    <!-- public int create(Payment payment); -->
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" KeyProperty="id">
        insert into payment(serial) values(#{serial});   
    </insert>

    <!-- public Payment getPaymentById(@Param("id") Long id); -->
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id};
    </select>
</mapper>
  • 4、service
    接口PaymentService
@Service
public interface PaymentService {
    
    public int create(Payment payment);
    
    public Payment getPaymentById(@Param("id") Long id);
}

实现类

@Service
public 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);
    }
}
  • 5、controller
@RestController
@Slf4j
public class PaymentController {
    
    @Resource
    private PaymentService paymentService;
    
    @PostMapping(value = "/payment/create")
    public CommonResult<Payment> create(Payment payment) {
    
        int result = paymentService.create(payment);
        
        log.info("****插入结果" + result);
        
        if (result > 0) {
            return new CommonResult(200, "插入成功", payment);
        }else {
            return new CommonResult(100, "插入失败", null);
        }
    }
    
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVarialbe("id") Long id) {
        
        Payment payment = paymentService.getPaymentById(id);
        
        log.info("****查询结果" + payment);
        
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }else {
            return new CommonResult(100, "查询失败,没有对应记录,查询 ID" + id, null);
        }
    }
}
6、测试

  • 1、在浏览器中输入访问地址
http://localhost:8001/payment/get/1
结果:
{"code":200,"message":"查询成功","data":{"id":1,"serial":"zwx"}}
  • 2、使用 postman 软件 模拟 post 请求

  • 开启 Run DashBoard 功能
1、通过修改 idea 的 workspace.xml 的方式快速打开 Run DashBoard 窗口
2、可能由于 idea 版本不同,需要关闭重启
7、小总结
1、建 module
2、改 pom
3、写 yml
4、主启动
5、业务类
```![](https://img2020.cnblogs.com/blog/1595603/202008/1595603-20200811115836490-1467707594.png)
posted @ 2020-08-12 08:48  hellozwx  阅读(306)  评论(0编辑  收藏  举报