目录
一、父工程Progect空间创建
1.New Project
2.聚合总父工程名字
3.选择Maven版本
4.工程名称
5.设置字符编码
6.注解生效激活
7.Java编译版本
8.File Type过滤
二、父工程pom文件
1.添加打包方式为pom
2.删除src文件夹(干干净净~)
3. 配置依赖
4.Maven复习
①DependencyManagement和Dependencies
②如何跳过单元测试
三、Rest微服务工程构建
分析
1.构建步骤
①建立module
⭐父工程中pom的变化
②修改pom
③写YML
④主启动类
业务类
Ⅰ、建表
Ⅱ、entities
Ⅲ、dao
Ⅳ、service
Ⅴ、controller
2.测试
①查询数据
②插入数据
⭐约定 > 配置 > 编码
4.0.0
org.weng.springColud
springColudA
1.0-SNAPSHOT
pom
UTF-8
12
12
4.12
1.18.10
1.2.17
5.1.47
1.1.16
1.3.0
org.apache.maven.plugins
maven-project-info-reports-plugin
3.0.0
org.springframework.boot
spring-boot-dependencies
2.2.2.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR1
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.1.0.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
runtime
com.alibaba
druid
${druid.version}
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.spring.boot.version}
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.springframework.boot
spring-boot-maven-plugin
true
true
(多出的部分)
cloud2022
com.atxupt.springcloud
1.0-SNAPSHOT
4.0.0
cloud-provider-payment8001
8
8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.apache.maven.plugins
maven-project-info-reports-plugin
3.4.0
org.springframework.boot
spring-boot-dependencies
2.2.2.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR1
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.8.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
runtime
com.alibaba
druid
1.1.10
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
com.alibaba
druid-spring-boot-starter
1.2.1
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
在resources下新建文件application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
#数据库url
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false
# 数据库访问账户
username: root
# 数据库访问密码
password:
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.atxupt.springcloud.entities #所有Entity别名类所在包
import org.springframework.boot.SpringApplication;
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
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
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor //全参
@NoArgsConstructor //空参
public class Payment {
private long id; //long:数据库中id的数据类型为bigint
private String serial;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
//404 not_found
private Integer code; //404
private String message; //not found
private T data;
public CommonResult(Integer code,String message){
this(code,message,null);
}
}
①接口PaymentDao
import com.atxupt.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper //推荐使用Mapper
public interface PaymentDao {
//各种功能
//......
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
②mybatis的映射文件PaymentMapper.xml
insert into payment(serial) values (#{serial})
import com.atxupt.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
@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);
}
}
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@RequestMapping("/payment/create")
public CommonResult create(Payment payment){
int result = paymentService.create(payment);
log.info("****** insert result : " + result);
if (result > 0){
return new CommonResult(200,"Success",result);
}else {
return new CommonResult(444,"False!",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("****** insert result : " + payment);
if (payment != null){
return new CommonResult(200,"Success",payment);
}else {
return new CommonResult(444,"Null!" + id,null);
}
}
}