SpringCloud学习笔记1——入门篇

@入门篇

入门篇为过渡,还不涉及SpringCloud相关内容。

一、准备工作

1.使用IDEA创建Maven父工程

SpringCloud学习笔记1——入门篇_第1张图片

2.设置

ctrl+alt+s打开设置

①配置字符集编码

SpringCloud学习笔记1——入门篇_第2张图片

②配置注解生效激活

SpringCloud学习笔记1——入门篇_第3张图片

③配置Java编译版本

SpringCloud学习笔记1——入门篇_第4张图片

④文件类型过滤(可选)

SpringCloud学习笔记1——入门篇_第5张图片

3.修改项目

①删除src目录,使项目只保留pom.xml

删除前
SpringCloud学习笔记1——入门篇_第6张图片
删除后
SpringCloud学习笔记1——入门篇_第7张图片

②修改pom文件

先添加如下内容

<packaging>pompackaging>

如图
SpringCloud学习笔记1——入门篇_第8张图片

然后将之后的内容使用如下内容替换


    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>12maven.compiler.source>
        <maven.compiler.target>12maven.compiler.target>
        <junit.version>4.12junit.version>
        <lombok.version>1.18.10lombok.version>
        <log4j.version>1.2.17log4j.version>
        <mysql.version>8.0.18mysql.version>
        <druid.version>1.1.16druid.version>
        <mybatis.spring.boot.version>2.1.1mybatis.spring.boot.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-project-info-reports-pluginartifactId>
                <version>3.0.0version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.2.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Hoxton.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.1.0.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
                <scope>runtimescope>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.spring.boot.version}version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
            
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>${log4j.version}version>
            dependency>
        dependencies>
    dependencyManagement>

最终pom文件的内容



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.hry.springcloudgroupId>
    <artifactId>springcloud01artifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>12maven.compiler.source>
        <maven.compiler.target>12maven.compiler.target>
        <junit.version>4.12junit.version>
        <lombok.version>1.18.10lombok.version>
        <log4j.version>1.2.17log4j.version>
        <mysql.version>8.0.18mysql.version>
        <druid.version>1.1.16druid.version>
        <mybatis.spring.boot.version>2.1.1mybatis.spring.boot.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-project-info-reports-pluginartifactId>
                <version>3.0.0version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.2.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Hoxton.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.1.0.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
                <scope>runtimescope>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.spring.boot.version}version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
            
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>${log4j.version}version>
            dependency>
        dependencies>
    dependencyManagement>

project>

二、构建项目(支付模块)

1.创建module

SpringCloud学习笔记1——入门篇_第9张图片
表示支付模块,使用端口号为8001
SpringCloud学习笔记1——入门篇_第10张图片
SpringCloud学习笔记1——入门篇_第11张图片
创建完之后查看父工程的pom文件多了module
SpringCloud学习笔记1——入门篇_第12张图片

2.修改pom文件

SpringCloud学习笔记1——入门篇_第13张图片
打开module payment8001的pom文件,原始如上图所示,添加如下依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

8001pom文件完整的内容如下


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud01artifactId>
        <groupId>com.hry.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-provider-payment8001artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>

3.编写yml文件

①新建application.yml文件

SpringCloud学习笔记1——入门篇_第14张图片

②编写yml

server:
  port: 8001    #将端口号设为8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver    #mysql驱动包
    url: jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.hry.springcloud.entities  #所有entity别名所在包

如果 driver-class-name报错,可以注释掉父工程中Mysql坐标中的scope
SpringCloud学习笔记1——入门篇_第15张图片

4.创建主启动类

①新建启动类

SpringCloud学习笔记1——入门篇_第16张图片

②编码

使用自定义代码补全live template配置请移步至这篇文章
SpringCloud学习笔记1——入门篇_第17张图片

package com.hry.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}

5.业务代码

①数据库创建语句

CREATE TABLE `payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `serial` varchar(200) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

②创建实体entities类

在entities包下创建Payment实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

如果id和serial提示 never used 需要在IDEA中安装Lombok插件
SpringCloud学习笔记1——入门篇_第18张图片
创建一个CommonResult实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    /**
     * 返回结果 例如404,200
     */
    private Integer code;

    /**
     * 信息  例如success
     */
    private String message;
    
    /**
     * 结果集 信息体对象
     */
    private T data;

    public CommonResult(Integer code,String message){
        this(code,message,null);
    }
}

结构如下
SpringCloud学习笔记1——入门篇_第19张图片

③创建dao层

(1)创建PaymentDao接口
/**
 * 这里的注解不是@Repository  是因为Repository在某些时候插入会出现问题
 */
@Mapper
public interface PaymentDao {
    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);
}

结构如下SpringCloud学习笔记1——入门篇_第20张图片

(2)创建Mapper.xml文件


<mapper namespace="com.hry.springcloud.dao.PaymentDao">
    
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO payment(serial) VALUES(#{serial});
    insert>

    
    <resultMap id="BaseResultMap" type="com.hry.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>

    resultMap>
    
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        SELECT * FROM payment WHERE id=#{id};
    select>
mapper>

结构
SpringCloud学习笔记1——入门篇_第21张图片

④创建service层

PaymentService 接口

package com.hry.springcloud.service;

import com.hry.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

public interface PaymentService {

    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);
}

PaymentService实现类

@Service
public class PaymentServiceImpl implements PaymentService {


    /**
     * @Autowired 也可以
     */
    @Resource
    private PaymentDao paymentDao;

    public int create(Payment payment){
        return paymentDao.create(payment);
    }

    public Payment getPaymentById(@Param("id") Long id){
        return paymentDao.getPaymentById(id);
    }
}

⑤创建controller层

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment){
        int result = paymentService.create(payment);
        log.info("*****插入结果:"+result);
        if (result > 0){
            return new CommonResult(200,"插入数据库成功",result);
        }else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果:"+payment);
        if (payment != null){
            return new CommonResult(200,"查询成功",payment);
        }else {
            return new CommonResult(444,"没有对应记录,查询ID"+id,null);
        }
    }
}

6.测试

在数据库表中手动插入一条数据:
SpringCloud学习笔记1——入门篇_第22张图片
运行SpringBoot启动类,出现错误

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: 
	

注释子module的pom文件中eureka 的坐标


       

运行启动类后打开浏览器在地址栏输入

http://localhost:8001/payment/get/1

报错

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

问题出现原因:MySQL8.0以上版本没有给定服务器时区
解决办法:①父工程pom中换成MySQL版本换成5.7 ②修改yml文件(推荐)
在spring.datasource.url最后加入&serverTimezone=Asia/Shanghai或者&serverTimezone=UTC

配置文件修正后代码如下

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

测试一下查询,成功了
SpringCloud学习笔记1——入门篇_第23张图片
测试添加,又报错了,报错原因是浏览器使用get请求
SpringCloud学习笔记1——入门篇_第24张图片
解决办法:使用postman模拟请求
打开postman,复制浏览器地址,请求改为POST,成功
SpringCloud学习笔记1——入门篇_第25张图片
查看数据库表,成功了
SpringCloud学习笔记1——入门篇_第26张图片

至此,第一节课完成!

你可能感兴趣的:(SpringBoot,Java,Web,SpringCloud)