尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)

学习地址:https://www.bilibili.com/video/av94137107?p=1
新建maven聚合工程
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第1张图片
Maven父工程,pom.xml记录

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
 
    <groupId>org.xzq.springcloud</groupId>
    <artifactId>cloud2020</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <!--统一管理jar包和版本-->
    <properties>
        <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.verison>1.1.16</druid.verison>
        <mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison>
    </properties>
 
    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- Druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.verison}</version>
            </dependency>
            <!-- mybatis-springboot整合 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.verison}</version>
            </dependency>
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <!--junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!-- log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

开发环境设置记录:
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第2张图片
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第3张图片
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第4张图片
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第5张图片

新建Module:provider-payment8001
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第6张图片
provider-payment8001的pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8081
spring:
  application:
    name: provider-payment8001
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url:  jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123465
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.gx.springcloud.entity
  

启动注解: @SpringBootApplication
尚硅谷2020最新版周阳SpringCloud(H版&alibaba)框架开发教程 学习笔记(一)_第7张图片
创建sql

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

写entity

package com.gx.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment {

    private Long id;
    private String serial;

}

写返回前端的entity

package com.gx.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private boolean flag;
    private String  mes;
    private T       data;
    public Result(boolean flag,String  mes){
        this(flag,mes,null);
    }

}

写dao

package com.gx.springcloud.dao;

import com.gx.springcloud.entity.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PaymentDao {
    int create(Payment payment);
    Payment selectPaymentById(@Param("id") long id);
}

写xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gx.springcloud.dao.paymentDao">
    <resultMap id="BaseResultMap" type="com.gx.springcloud.entity.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>
    <insert id="create" parameterType="com.gx.springcloud.entity.Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial) values (#{serial})
    </insert>
    <select id="selectPaymentById" resultMap="BaseResultMap">
        select * from payment where id = #{id}
    </select>
</mapper>

在mybatis的配置文件中,有个叫keyProperty和useGeneratedKeys的属性。useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。

具体用法:useGeneratedKeys=”true” keyProperty=”对应的主键的对象”。

写service

package com.gx.springcloud.service;

import com.gx.springcloud.entity.Payment;

public interface PaymentService {
    int create(Payment payment);
    Payment selectPaymentById(long id);
}

写serviceImpl

package com.gx.springcloud.serviceImpl;

import com.gx.springcloud.dao.PaymentDao;
import com.gx.springcloud.entity.Payment;
import com.gx.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    PaymentDao paymentDao;

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

    public Payment selectPaymentById(long id){
        return paymentDao.selectPaymentById(id);
    }
    
}

写controller

package com.gx.springcloud.controller;

import com.gx.springcloud.entity.Payment;
import com.gx.springcloud.entity.Result;
import com.gx.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author GX
 * @Date 2020-03-20 10:33
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/create")
    public Result create(Payment payment){
        Result result = null;
        try{
            int num = paymentService.create(payment);
            if(num>0){
                result=new Result(true,"插入数据成功");
                log.info("******插入"+payment+"成功******");
            }else{
                result=new Result(false,"插入数据失败");
                log.info("******插入"+payment+"失败******");
            }
        }catch (Exception e){
            result=new Result(false,"插入数据失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/get/{id}")
    public Result getPaymentById(@PathVariable Long id){
        Result result = null;
        try{
            Payment payment = paymentService.selectPaymentById(id);
            if(payment != null){
                result=new Result(true,"获取数据成功",payment);
                log.info("******根据id:"+id+"获取"+payment+"成功******");
            }else{
                result=new Result(false,"获取数据失败");
                log.info("******根据id:"+id+"获取数据失败******");
            }
        }catch (Exception e){
            result=new Result(false,"获取数据失败");
            e.printStackTrace();
        }
        return result;
    }

}

你可能感兴趣的:(springcloud,springboot,maven)