④. 支付模块(cloud-provider-payment8001)
①. pom.xml
<dependencies> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <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>
②. application.yml
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities
③. PaymentMapper.xml
(两种获取到mybatis中主键自增的方式)
<!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"> <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"></id> <result column="serial" property="serial" jdbcType="VARCHAR"></result> </resultMap> <!-- (1). useGeneratedKeys默认是false,keyProperty实体类对应主键的属性,keyColumn表中的主键 (2). 使用selectKey进行 <insert id="createPayment" parameterType="com.xiaozhi.Payment"> /* 总体解释:将插入数据的主键返回到Payment对象中。 具体解释: a. SELECT LAST_INSERT_ID():得到刚 insert进去记录的主键值,只适用与自增主键 b. keyProperty:将查询到主键值设置到 parameterType指定的对象的那个属性 c. order:SELECT LAST_INSERT_ID() 执行顺序,相对于insert语句来说它的执行顺序 e. resultType:指定 SELECTLAST_INSERT_ID() 的结果类型 */ <selectKey resultType="int" order="AFTER" keyProperty="id"> select LAST_INSERT_ID() </selectKey> insert into payment(serial) values (#{serial}) </insert> --> <insert id="createPayment" parameterType="com.atguigu.springcloud.entities.Payment"> <!--返回值这里是long,是因为实体类中的数据类型是:private Long id--> <selectKey resultType="long" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into payment(serial) values (#{serial}) </insert> <!-- <insert id="createPayment" parameterType="com.atguigu.springcloud.entities.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>
④. controller
@SuppressWarnings("all") @RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Autowired private PaymentService paymentService; //新增 @PostMapping("/create") public CommonResult create(@RequestBody Payment payment){ int result = paymentService.create(payment); if(result>0){ return new CommonResult(200,"新增成功!",payment); } return new CommonResult(404,"新增失败!",null); } //通过id进行查询 @GetMapping("/get/{id}") public CommonResult getPaymentById(@PathVariable Long id){ Payment payment = paymentService.getPaymentById(id); log.info("****查询结果为:****"); if(payment!=null){ return new CommonResult(200,"查询成功",payment); } return new CommonResult(444,"查询失败",null); } }