002-cloud8-8001子模块-改pom-建yml-建库建表
1 <dependencies> 2 3 <!--web/actuator这两个一般一起使用,写在一起--> 4 <dependency> 5 <groupId>org.springframework.boot</groupId> 6 <artifactId>spring-boot-starter-web</artifactId> 7 </dependency> 8 <!--监控--> 9 <dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-actuator</artifactId> 12 </dependency> 13 14 <!--Mybatis和SpringBoot的整合--> 15 <dependency> 16 <groupId>org.mybatis.spring.boot</groupId> 17 <artifactId>mybatis-spring-boot-starter</artifactId> 18 </dependency> 19 20 <dependency> 21 <groupId>com.alibaba</groupId> 22 <artifactId>druid</artifactId> 23 <!--如果没写版本,从父层面找,找到了就直接用,全局统一--> 24 </dependency> 25 26 <!--mysql-connector-java--> 27 <dependency> 28 <groupId>mysql</groupId> 29 <artifactId>mysql-connector-java</artifactId> 30 </dependency> 31 <!--jdbc--> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-jdbc</artifactId> 35 </dependency> 36 <!--热部署--> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-devtools</artifactId> 40 <scope>runtime</scope> 41 <optional>true</optional> 42 </dependency> 43 <dependency> 44 <groupId>org.projectlombok</groupId> 45 <artifactId>lombok</artifactId> 46 <optional>true</optional> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-starter-test</artifactId> 51 <scope>test</scope> 52 </dependency> 53 </dependencies>
这里,需要先去建立一个db2019的数据库,设置的名字密码要保持一致。推荐用navicat软件,轻便简单。
1 # 微服务端口号 2 server: 3 port: 8001 4 5 # 微服务名称 6 spring: 7 application: 8 name: cloud-payment-service 9 10 datasource: 11 type: com.alibaba.druid.pool.DruidDataSource # 数据源 12 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 这里报错就在左侧External Libraries栏找mysql下的Driver 13 url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false 14 username: root 15 password: 123456 16 17 18 mybatis: 19 mapper-locations: classpath:mapper/*.xml # 扫描类路径下mapper文件夹下的.xml配置文件 20 type-aliases-package: com.atguigu.springcloud.entities # 该包所有Entity类,取默认别名
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 4 @SpringBootApplication 5 public class PaymentMain8001 { 6 public static void main(String[] args) { 7 SpringApplication.run(PaymentMain8001.class,args); 8 } 9 10 }
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=utf8
这里要注意的是,有些工具是需要单引号,有些是需要双引号,而navicat里,字段,表名都是不需要引号的。
1 import lombok.AllArgsConstructor; 2 import lombok.Data; 3 import lombok.NoArgsConstructor; 4 5 import java.io.Serializable; 6 7 /** 先在IDEA搜lombok插件安装,否则这三个用不了*/ 8 @Data 9 @AllArgsConstructor 10 @NoArgsConstructor 11 public class Payment implements Serializable { 12 private Long id; //Long 非long 13 private String serial; 14 }
1 package com.atguigu.springcloud.entities; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 8 @Data 9 @AllArgsConstructor 10 @NoArgsConstructor 11 public class CommonResult<T> { 12 13 //404 not_found:Integer String 14 private Integer code; 15 private String message; 16 17 //这不是针对某一个实体类的Josn串封装类 18 private T data; 19 20 //当T是null时,定义一个两个参数的构造 21 public CommonResult(Integer code, String message){ 22 this(code, message, null); //数据体 默认为null 23 } 24 25 }
1 /** 2 * 如果是用mybatis 建议用Mapper注解, repository插入时容易翻车 3 */ 4 @Mapper 5 public interface PaymentDao { 6 // 常用的读写方法 7 public int create(Payment payment); 8 public Payment getPaymentById(@Param("id") Long id); 9 10 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.atguigu.springcloud.dao.PaymentDao"> 7 8 9 </mapper>
1 <!--这里 Payment报错 可以写全路径。 似乎是yml中的配置未生效 不确定是否还有其他问题--> 2 <insert id="create" parameterType="com.atguigu.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id"> 3 insert into payment(serial) values(#{serial}); 4 </insert> 5 6 <!--定义一个结果接和实体类的映射表--> 7 <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> 8 <id column="id" property="id" jdbcType="BIGINT"/> 9 <id column="serial" property="serial" jdbcType="VARCHAR"/> 10 </resultMap> 11 12 <!-- 这里是long Long会报错 不知道是为何--> 13 <select id="getPaymentById" parameterType="long" resultMap="BaseResultMap"> 14 select * from payment where id=#{id}; 15 </select>
这里 Payment 和Long 报错 改一下就好,具体见代码。
【不过后期测试,即使Payment标红,项目也可以启动,也能正常使用该SQL给数据库插入,暂且搁置】
import com.atguigu.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; public interface PaymentService { public int create( Payment payment); public Payment getPaymentById(@Param("id") Long id); } import com.atguigu.springcloud.dao.PaymentDao; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.service.PaymentService; import org.apache.ibatis.annotations.Param; 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); } }
实现类需要添加@Service注解
1 package com.atguigu.springcloud.controller; 2 3 import com.atguigu.springcloud.entities.CommonResult; 4 import com.atguigu.springcloud.entities.Payment; 5 import com.atguigu.springcloud.service.PaymentService; 6 import lombok.extern.slf4j.Slf4j; 7 import org.springframework.web.bind.annotation.*; 8 9 import javax.annotation.Resource; 10 11 @RestController 12 @Slf4j 13 public class PaymentController { 14 @Resource 15 private PaymentService paymentService; 16 17 @PostMapping(value = "/payment/create") 18 public CommonResult create(Payment payment) { 19 int result = paymentService.create(payment); 20 log.info("*****插入结果:" + result); 21 if (result > 0) { 22 return new CommonResult(200, "插入数据库成功", result); 23 } 24 return new CommonResult(444, "插入数据库失败", null); 25 } 26 27 @GetMapping(value = "/payment/get/{id}") //写操作POST 读get 还有pull等等 28 public CommonResult getPaymentById(@PathVariable("id") Long id) { 29 30 Payment payment = paymentService.getPaymentById(id); 31 log.info("*****查询结果:" + payment); 32 if (payment != null) { 33 return new CommonResult(200, "查询数据库成功", payment); 34 } else { 35 return new CommonResult(444, "查询ID:" + id + "没有对应记录", null); 36 } 37 } 38 39 }
@RestController=@Controller+@ResponseBody @SLF4J是日志 这里有使用到RESFful接口风格设计思想。
关于浏览器只有get方式:建议下载utools,它里面有很多开发必备的小插件,包括postman。内网穿透等等。简直神器。
注:我的项目刚开始报错【At least one base package must be specified】,参考【MapperScan注解的使用】给主程序添加了扫包注解后正常运行。
后来,把那个注解删除后,还是正常的。
这里,似乎是yml中配置的路径并未生效的缘故。之前在【Payment】那里也报错了。也许是IDEA自动识别的问题? 后期深入了解。
---小尾巴助手:欢迎批评、评论、论辩,如果觉得写的还行,麻烦点个推荐,虽然我暂时也不知道它有什么用。[202007]

浙公网安备 33010602011771号