- DAO层
PaymentDao
/** * @author lxy * @version 1.0 * @Description 支付dao类 * @date 2021/12/23 11:06 */ @Mapper @Repository public interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
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.rg.springcloud.dao.PaymentDao"> <!--设置useGeneratedKeys参数值为true,在执行添加记录之后可以获取到数据库自动生成的主键ID。--> <insert id="create" parameterType="com.rg.springcloud.pojo.Payment" useGeneratedKeys="true" keyProperty="id"> INSERT INTO payment(serial) VALUES(#{serial}) </insert> <resultMap id="BaseResultMap" type="com.rg.springcloud.pojo.Payment"> <!--jdbcType这个参数的应用场景就是,当执行mapping文件的时候,有个映射的参数为空,那么无法确定他的类型,这个时候就需要jdbcType来确定类型。--> <id column="id" property="id" jdbcType="BIGINT"/> <result column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" resultMap="BaseResultMap" parameterType="long"> SELECT * FROM payment WHERE id = #{id} </select> </mapper>
- service层
public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
@Service public class PaymentServiceImpl implements PaymentService { @Autowired private PaymentDao paymentDao; @Override public int create(Payment payment) { return paymentDao.create(payment); } @Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }
- controller层
/** * @author lxy * @version 1.0 * @Description PaymentController * @date 2021/12/23 11:27 */ @Slf4j @RestController public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommonResult create(Payment payment){ // ??? 为啥这里不需要@RequestBody呢?? int result = paymentService.create(payment); log.info("*****插入操作返回结果*:"+result); if(result>0) { return new CommonResult(200, "插入数据成功", result); }else { return new CommonResult(444, "插入数据失败"); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id) { Payment payment = paymentService.getPaymentById(id); log.info("*****查询结构:{}", payment); if(payment!=null){ return new CommonResult(200, "查询成功", payment); }else{ return new CommonResult(444, "没有对应记录,查询ID:" + id); } } }
- 测试
- 小结
SpringBoot创建模块的五个步骤:
1.建module
2.改POM
3.写YML
4.主启动
5.业务类
1.2.2 热部署Devtools
1.Adding devtools to your project
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
2.Adding plugin to your pom.xml
下段配置我们粘贴进聚合父类总工程的pom.xml里
<build> <!--finalName:工程名称--> <finalName>cloud2021</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build>
3.Enabling automatic build
4.Update the value of
5.重启IDEA
6.进行测试
1.2.3 cloud-consumer-order80:微服务消费者订单Module模块
- 建cloud-consumer-order80
- 改POM
<?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>cloud2021</artifactId> <groupId>com.rg.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order80</artifactId> <dependencies> <!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.rg.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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.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>
- 写YML
server: port: 80
- 主启动
@SpringBootApplication public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
业务类
pojo
同cloud-provider-payment8001模块的pojo
首说RestTemplate
RestTemplate提供了多种便捷访问远程Http服务的方法,
是一种简单便捷的访问restful服务模板类,是Spring提供的==用于访问Rest服务==的客户端模板工具集
官网:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
使用:
使用restTemplate访问restful接口非常的简单粗暴无脑。
(url, requestMap, ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
config配置类
@Configuration public class ApplicationContextConfig { //向SpringBoot容器中加入该组件 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }