Rest学习环境搭建:服务提供者

Rest 服务提供者

一.创建父工程

  1. 创建一个普通的maven工程即可。因为是父工程所以里面不用src,直接删除即可
  2. 写父工程的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>cn.lzm</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--打包方式要选pom-->
    <packaging>pom</packaging>

    <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>
        <lombok.version>1.18.14</lombok.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <!--父依赖-->
    <dependencyManagement>
        <dependencies>
           <!--spring cloud 依赖-->
            <!--scrope选择 import-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring boot 依赖-->
            <!--scrope选择 import-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--数据库-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.2.3</version>
            </dependency>

            <!--spring boot 启动器 mybatis-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.3</version>
            </dependency>

            <!--单元测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
             <!--日志测试-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!--log4j-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>

            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
</project>

二.编写第一个子模块,用于提供api

  1. 创建模块,在父模块下面,普通的maven项目即可,这个是springcloud-api,是提供api的
  2. 写springcloud-api的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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>cn.lzm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>API</artifactId>
    <!--当前module所需要的依赖,如果父依赖已经配置,则不需要写了-->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 写实体类
@Data
@NoArgsConstructor
@Accessors(chain = true) //支持链式写法
public class Person implements Serializable { // 一定要实现 Serializable 接口
    private int id;
    private int age;
    private String name;
    private String perm;
    //这个数据表示数据存在哪个,微服务,一个服务对应一个数据库,同一个信息可能存在不同的数据库
    private String db_source;
}

三.编写第二个子模块,用于编写服务提供者

1.编写 模块 springcloud-provider-person-8001,把端口号也写上,因为要开很多服务,要占很多端口
-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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>cn.lzm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-provider-person-8001</artifactId>
    <dependencies>
        <!--我们需要拿到api模块,所以要拿到它-->
        <dependency>
            <groupId>cn.lzm</groupId>
            <artifactId>API</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!--日志测试-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <!--mybatis 启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!--引用父类的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 编写配置文件,application.yml,mybatis.xml
  • application.yml
server:
  port: 8001

#mybatis
mybatis:
  type-aliases-package: cn.lzm.springcloud.pojo #给包起别名
  config-location: classpath:mybatis.xml      # mybatis配置文件的路径
  mapper-locations: classpath:mapper/*.xml   #mapper文件的路径

#spring配置
spring:
  application:
    name: provider-person-8001
  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver #实现了driver,提供更多功能
    url: jdbc:mysql://localhost:3306/tea?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=PRC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
  • mybatis.xml
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--因为配置完了会报错,所以暂时不配置了-->
<!--    <settings>-->
<!--        &lt;!&ndash;开启二级缓存&ndash;&gt;-->
<!--        <setting name="cacheEnabled " value="true"/>-->
<!--    </settings>-->
</configuration>
  1. 写controller ,service,mapper,mapper.xml 文件
  • mapper和 mapper.xml
@Mapper
@Repository
public interface PersonMapper {
    /**
     * 返回类型从另一个模块,用pom的方式拿过来的
     * @param id
     * @return
     */
    Person getPersonById(int id);

    void addPerson(Person person);
    List<Person> getAllPerson();
}
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.lzm.springcloud.dao.PersonMapper">
    <select id="getPersonById" parameterType="int" resultType="person">
        select * from person where id = #{id};
    </select>

    <select id="getAllPerson" resultType="person">
         select * from person;
    </select>
    
    <insert id="addPerson" parameterType="person">
        insert into person
        (name, age, perm, db_source)
        values
        (#{name},#{age},#{perm},{db_source})
    </insert>
</mapper>
  • service 和 service impl
public interface PersonService {
    Person getPersonById(int id);
    void addPerson(Person person);
    List<Person> getAllPerson();
}

package cn.lzm.springcloud.service;

import cn.lzm.springcloud.dao.PersonMapper;
import cn.lzm.springcloud.pojo.Person;
import cn.lzm.springcloud.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    PersonMapper personMapper;

    @Override
    public Person getPersonById(int id) {
        return personMapper.getPersonById(id);
    }

    @Override
    public void addPerson(Person person) {
        personMapper.addPerson(person);
    }

    @Override
    public List<Person> getAllPerson() {
        return personMapper.getAllPerson();
    }
}
  • controller,采用Restful
package cn.lzm.springcloud.controller;

import cn.lzm.springcloud.pojo.Person;
import cn.lzm.springcloud.service.PersonService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 关键,提供 Restful 服务
 */
@RestController
public class PersonController {
    @Autowired
    PersonService personService;

    @GetMapping("/person/{id}")
    public Person getPersonById(@PathVariable("id") int id) {
        return personService.getPersonById(id);
    }

    @PostMapping("/person/add")
    public String addPerson(Person person) {
         personService.addPerson(person);
         return"1";
    }
    @GetMapping("/person/getall")
    @ResponseBody
    public List<Person> getAllPerson() {
        return personService.getAllPerson();
    }
}

  1. 写 启动类 SpringApplication
package cn.lzm.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 */
@SpringBootApplication
public class PersonApplication {
    public static void main(String[] args) {
        SpringApplication.run(PersonApplication.class,args);
    }
}

  1. 跑起来测试一下,嘻嘻(#.#)
  • nice! o( ̄▽ ̄)d
posted @ 2020-11-29 15:23  阿肆啊  阅读(99)  评论(0编辑  收藏  举报