建module改pom写yml主启动业务类
创建子module
自动新增父类pom自动生成该内容
子类pom文件如下:
cloud2020 com.hyf.springcloud 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8001 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
新建yml:如图位置创建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 # mysql驱动包 com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapperLocations: classpath:mapper
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
在公司的业务下面,前后端分离架构,后端程序员需要做的就是给前端传递一个JSON字符串。
开启业务类。
- 建表SQLentitiesdaoservicecontroller
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
entities:
@Data
@AllArgsConstructor//全参
@NoArgsConstructor//无参
public class Payment {
private Long id;
private String serial;
}
//用于封装结果集,封装返回结果参数
@Data
@AllArgsConstructor//全参
@NoArgsConstructor//无参
public class CommonResult {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code,String message){
this(code,message,null);
}
}
mapper:
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
insert into payment(serial) values(#{serial});
select * from where id=#{id}
service:
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
impl
@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:
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("***插入结果***"+result);
if(result>0){
return new CommonResult(200,"插入数据库成功",result);
}
return new CommonResult(400,"插入数据库失败",null);
}
@GetMapping(value = "/payment/getPaymentById/{id}")
public CommonResult getPaymentById(@PathVariable("id")Long id){
Payment result = paymentService.getPaymentById(id);
log.info("***插入结果***"+result);
if(result!=null){
return new CommonResult(200,"查询成功",result);
}
return new CommonResult(400,"没有对应记录,查询失败,ID:"+id,null);
}
}
运行:
前提:数据库需要数据
结果:
OK!成功,该JSON就是你传给前端的数据。
再来测试一下!
哎
为什么报错,查看报错信息。。。。
哦,原来是浏览器不支持。
那我们怎么办呢?
凉拌!
不支持我们就换一个
我们用postman好了
你们看,是不是成功啦。
那我们再试试其他的。
注意:url部分的?后面的都是postman自动为我们拼接的,而不是我们自己敲得,我们实在body中传入参数。但是我个人在测试这个方法的时候,踩了一个坑,关于Params和Body的事情,有兴趣的可以点击一下(点我)。
查看数据库:
第二节结束。



