2020最新版SpringCloud(H版&alibaba)框架开发教程(一)

微服务模块

1.建module

2.改pom

3.写yml

4.主启动

5.业务类

PaymentDao:
/**
 * @Author: cws
 * @Date: 9:37 2020/8/12
 * @Description:  微服务模块读和写的操作接口
 * @Version v1.0
 */
@Mapper
public interface PaymentDao {

    public Payment getPaymentById(@Param("id") Long id);

    public int create(Payment payment);
}
entities.Payment:
/**
* @Author: cws * @Date: 0:46 2020/8/12 * @Description: * @Version v1.0 */ @Data @AllArgsConstructor //有参构造 @NoArgsConstructor //无参构造 public class Payment implements Serializable { private Long id; private String serial; }
entities.CommonResult:
/**
* @Author: cws * @Date: 0:50 2020/8/12 * @Description: 返回前端的通用json实体串 * @Version v1.0 */ @Data @AllArgsConstructor @NoArgsConstructor public class CommonResult<T> { //404 not found private Integer code; //返回成功或失败的编码 private String message; //返回成功或失败的信息 private T data; //前台显示的属性,传什么就返回什么 public CommonResult(Integer code, String message) { this(code , message , null); //如果失败就返回null } }
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"> <insert id="create" parameterType="com.atguigu.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment (serial) values (#{serial}) </insert> <!--搜索的映射 column数据库的名字,property是java实体类里面的名字(别名),jdbcType是数据库里面的数据类型 --> <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" resultMap="BaseResultMap" parameterType="Long"> select id , serial from payment where id = #{id} </select> </mapper>
PaymentServiceImpl:
/**
* @Author: cws * @Date: 11:11 2020/8/12 * @Description: * @Version v1.0 */ @Service public class PaymentServiceImpl implements PaymentService { @Autowired private PaymentDao paymentDao; @Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } @Override public int create(Payment payment) { return paymentDao.create(payment); } }

 

PaymentService:
/**
* @Author: cws * @Date: 11:10 2020/8/12 * @Description: * @Version v1.0 */ public interface PaymentService { public Payment getPaymentById(@Param("id") Long id); public int create(Payment payment); }

 

controller:
/**
* @Author: cws * @Date: 11:13 2020/8/12 * @Description: * @Version v1.0 */ @RestController @Slf4j //打印日志 public class PaymentController { @Autowired private PaymentService paymentService; @PostMapping(value = "/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(404, "插入失败" , null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { Payment result = paymentService.getPaymentById(id); if (result!=null) { return new CommonResult(200, "查询成功", result); } else { return new CommonResult(404, "查询失败" , null); } } }

消费者订单模块

配置容器:
/**
 * @Author: cws
 * @Date: 13:59 2020/8/12
 * @Description:
 * @Version v1.0
 */
@Configuration
public class ApplicationContextConfig {

    @Bean    //依赖注入容器   获取对象
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

 

controller:
/**
 * @Author: cws
 * @Date: 14:08 2020/8/12
 * @Description:
 * @Version v1.0
 */
@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create( Payment payment) {
        log.info("*******消费者启动创建订单*******");
        CommonResult commonResult = restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);//地址  参数  对象类型
        log.info("=================="+commonResult);
        return commonResult;
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        log.info("===========================");
        CommonResult object = restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        log.info("=============="+object+"=======上面的路径要注意  缺一个/都识别不了======");
        return object;
    }
}

 

主启动:
/**
 * @Author: cws
 * @Date: 13:45 2020/8/12
 * @Description:
 * @Version v1.0
 */
@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}

注:实体CommonResult和Payment一样

工程重构

 

<!-- 项目坐标   在想调用的子项目pom文件上打上坐标调用-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

 

posted @ 2020-08-12 11:09  白描先生  阅读(636)  评论(0编辑  收藏  举报
}); });