支付模块构建
创建微服务模块套路
建Module
此时父工程POM多了一个
改POM
在子工程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> <!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <!-- <dependency> <groupId>com.atguigu.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.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>
写YML
在Resouse文件夹下新建一个application.yml文件
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 url: jdbc:mysql://waiwanga.mysql.rds.aliyuncs.com:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: xxxx mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.ylc.cloud.entities # 所有Entity别名类所在包
主启动
package com.ylc.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
业务类
SQL:
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=utf8mb4
实体类:
实体类Payment
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
JSON封装体CommonResult:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @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); } }
DAO
接口PaymentDao
package com.ylc.cloud.dao; import com.ylc.cloud.entities.Payment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
MyBatis映射文件PaymentMapper.xml,路径:resources/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.ylc.cloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap id="BaseResultMap" type="com.lun.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id}; </select> </mapper>
useGeneratedKeys是允许主键自动生成
Service
接口PaymentService
package service; import com.ylc.cloud.entities.Payment; import org.apache.ibatis.annotations.Param; public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
实现类
package service.impl; import com.ylc.cloud.dao.PaymentDao; import com.ylc.cloud.entities.Payment; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create(Payment payment) { return paymentDao.create(payment); } public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }
Controller
package com.ylc.cloud.controller; import com.ylc.cloud.entities.CommonResult; import com.ylc.cloud.entities.Payment; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import service.PaymentService; import javax.annotation.Resource; @Slf4j @RestController public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommonResult creat(@RequestBody Payment payment) { int result=paymentService.create(payment); log.info("*****插入结果:"+result); if(result > 0) { return new CommonResult(200,"插入数据库成功,: "+result); }else{ return new CommonResult(444,"插入数据库失败",null); } } }
测试
Description: A component required a bean of type 'service.PaymentService' that could not be found.
找不到Service,原因是因为项目启动时,只有@SpringBootApplication 所在的包被扫描,在本例中,启动类是MainApplication.java, 也就是MainApplication.java类所在的这个包,而其他的controller和service以及mapper在其他的包里,所以并没有被扫描。解决方法是,把启动类放在外层包
Postman测试:
http://localhost:8001/payment/create?serial=lun2
热部署Devtools
开发时使用、生产环境关闭
1.在项目中添加devtools
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
2.复制到总工程pom
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins>
3.设置自动构建项目
4.并行编译独立模块
ctrl+shirt+Alt+/快捷键,打开Registry
重启idea以后,每次修改项目都会帮我们直接重新启动,方便调试程序