微服務模塊
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>