一:首先创建父级项目
后面的项目模块都在此工程中:
不使用骨架创建。
Settings -> File Encodings里面的项目的编码格式为UTF-8
2.配置POM依赖
首先要加 pom 这个。
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
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
m
y
s
q
l
.
v
e
r
s
i
o
n
<
/
v
e
r
s
i
o
n
>
<
s
c
o
p
e
>
r
u
n
t
i
m
e
<
/
s
c
o
p
e
>
<
/
d
e
p
e
n
d
e
n
c
y
>
<
!
−
−
d
r
u
i
d
−
−
>
<
d
e
p
e
n
d
e
n
c
y
>
<
g
r
o
u
p
I
d
>
c
o
m
.
a
l
i
b
a
b
a
<
/
g
r
o
u
p
I
d
>
<
a
r
t
i
f
a
c
t
I
d
>
d
r
u
i
d
<
/
a
r
t
i
f
a
c
t
I
d
>
<
v
e
r
s
i
o
n
>
{mysql.version}</version> <scope>runtime</scope> </dependency> <!-- druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>
mysql.version</version><scope>runtime</scope></dependency><!−−druid−−><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>{druid.version}
org.mybatis.spring.boot
mybatis-spring-boot-starter
m
y
b
a
t
i
s
.
s
p
r
i
n
g
.
b
o
o
t
.
v
e
r
s
i
o
n
<
/
v
e
r
s
i
o
n
>
<
/
d
e
p
e
n
d
e
n
c
y
>
<
!
−
−
j
u
n
i
t
−
−
>
<
d
e
p
e
n
d
e
n
c
y
>
<
g
r
o
u
p
I
d
>
j
u
n
i
t
<
/
g
r
o
u
p
I
d
>
<
a
r
t
i
f
a
c
t
I
d
>
j
u
n
i
t
<
/
a
r
t
i
f
a
c
t
I
d
>
<
v
e
r
s
i
o
n
>
{mybatis.spring.boot.version}</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>
mybatis.spring.boot.version</version></dependency><!−−junit−−><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>{junit.version}
log4j
log4j
${log4j.version}
org.springframework.boot
spring-boot-maven-plugin
true
true
二:服务提供者
1.右键父工程,新建子模块(new->Module),不使用骨架创建
2)编写POM文件
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
3.编写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/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.yousian.pojo# 所有pojo别名类所在包
4.编写启动类
@SpringBootApplication
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
5.编写业务类
5.1 创建数据表
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=utf8payment
5.2创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
private Integer code;
private String messgae;
private T data;
/**
* 查询为空的时候使用的构造器
* @param code
* @param messgae
*/
public CommonResult(Integer code, String messgae){
this(code, messgae, null);
}
}
5.3创建dao
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param(“id”) Long id);
}
创建mapper映射文件(resources/mapper) PaymentMapper.xml
<resultMap id="BaseResultMap" type="com.kriss.entity.Payment">
<id column="id" property="id" jdbcType="BIGINT"></id>
<result column="serial" property="serial" jdbcType="VARCHAR"></result>
</resultMap>
<select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
5.5创建service
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param(“id”) Long id);
}
@Service
@SuppressWarnings(value = “all”)//压制警告
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
5.6.创建controller
@RestController //必须是这个注解,因为是模拟前后端分离的restful风格的请求,要求每个方法返回 json
@Slf4j
public class PaymentController {
@Autowired
PaymentService paymentService;
@PostMapping(value = “/payment/create”)
public CommonResult create(Payment payment){
int result = paymentService.create(payment);
if(result > 0){
return new CommonResult(200, “插入数据库成功”, result);
}
return new CommonResult(444, “插入数据库失败”, null);
}
@GetMapping(value = "/payment/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("****查询结果:" + payment);
if(payment != null){
return new CommonResult(200, "查询成功", payment);
}
return new CommonResult(444, "没有对应id的记录", null);
}
}
最后在浏览器测试:localhost:8001/payment/1
在postman中测试添加localhost:8001/payment/create
测试成功的话一个创建过程好了