springclouddemo5.1 service provider -user

** Learn from the station b springcloud project, now summarize the summary removal of a small error appearing in the video, some of the error-prone places were reminded

b outbound links: https://www.bilibili.com/video/av55629580?p=1
profile link:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
extraction code: 21ru

On a link:
https://blog.csdn.net/qq_40893824/article/details/103609138
next section link:
https://blog.csdn.net/qq_40893824/article/details/103618769

The following list summarizes:
Module1 (User) → ConfigServer / New Shared bootstrap.yml → → dev.yml-User
com.southwind.UserApplication.java → → inspection controler.UserHandler.java

user/entity/User.java→repository/UserRepository.java→resources/mapping/MenuRepository.xml→UserHandler.java→检查

By the user, delete, check the operation
implementation details:
1. Create a new module named user, as their pom file to add the code and menu!

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

2. Allocation: Re copy menu-dev.yml in confiserver / resources / shared but was renamed in user-dev.yml, and modify the port to 8040, name for the user, the other unchanged:

server:
  port: 8040
spring:
  application:
    name: user
  datasource:
    name: qwe
    url: jdbc:mysql://localhost:3306/qwe?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: com.southwind.entity
#  这个mybatis是在menu - resources - mapping写好后加的。

3.user read configserver copy the configuration menu / resources bootstrap.yml to the user / resources, change the name of user:

spring:
  application:
    name: user
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

4. In the user / java package record in com.southwind, start a new class southwind the UserApplication.java tagging:

package com.southwind;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.southwind.repository")
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

5. record the user / southwind Controller of packets, the new controller UserHandler.java inside, tagging:

package com.southwind.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserHandler {

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return "当前端口:"+this.port;
    }
}

6. Restart configserver start to check whether the user interface can be adjusted through: Go to http: // localhost: 8761
Here Insert Picture Description
and then enter http: // localhost: 8040 / user / index
Here Insert Picture Description
description can be adjusted through!

Interface for data:
7. In user / southwind create the package entity, the entity class created User.java, tagging:

package com.southwind.entity;

import lombok.Data;

@Data
public class User {
    private long id;
    private String username;
    private String password;
    private String nickname;
    private String gender;
    private String telephone;
    private Date registerdate;
    private String address;
}

Registerdate which is the date of registration, so you can see its type:
facing t_user Right-click
Here Insert Picture Description
Here Insert Picture Description
can see registerdate type is a Date
8. In southwind in the Year of the package repository, in which new interfaces UserRepository.java, adding the code:

package com.southwind.repository;

import com.southwind.entity.User;

import java.util.List;

public interface UserRepository {

    /*增*/
    public void save(User user);
    /*删*/
    public void deleteById(long id);
    /*查*/
    public List<User>findAll(int index ,int limit);
    public User findById(long id);
    /*改*/
    public void update(User user);
    /*计数*/
    public int count();
}

9. In the user / resources in the package record mapping, the copy menu / mapping of MenuRepository.xml to User / mapping, the renamed and UserRepository.xml, modify the code:
the third line MenuRepository UserRepository to
delete rows 5-11 <resultMap>
the findById resultMap now in line 13 and 5 findAll row = "menuMap" to resultType = "User"
of the next row and all subsequent t_menu to T_USER
. 17 and 21 modify the save and update the line, corresponding to the user attributes

<?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.southwind.repository.UserRepository">
	
	<select id="findAll" resultType="User">
		select * from t_user limit #{param1},#{param2}
	</select>

	<select id="count" resultType="int">
		select count(id) from t_user
	</select>

	<select id="findById" parameterType="long" resultType="User">
		select * from t_user where id = #{id}
	</select>

	<insert id="save" parameterType="User">
		insert into t_user(username,password,nickname,gender,telephone,registerdate,address) values(#{username},#{password},#{nickname},#{gender},#{telephone},#{registerdate},#{address})
	</insert>

	<update id="update" parameterType="User">
		update t_user set username = #{username},password = #{password},nickname = #{nickname}, gender=#{gender},telephone = #{telephone},registerdate = #{registerdate},address=#{address} where id = #{id}
	</update>

	<delete id="deleteById" parameterType="long">
		delete from t_user where id = #{id}
	</delete>
</mapper>

10. Now to user / UserHandler modify the code:
delete port code
then added Code:

package com.southwind.controller;

import com.southwind.entity.User;
import com.southwind.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserHandler {

    @Autowired
    private UserRepository userRepository;

    /*增*/
    @PostMapping("/save")
    public void save(@RequestBody User  user){
        userRepository.save(user);
    }

    @PutMapping("/update")
    public void update(@RequestBody User user){
        userRepository.update(user);
    }
    /*删*/
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id")long id){
        userRepository.deleteById(id);
    }

    /*查*/
    @GetMapping("/findAll/{index}/{limit}")
    public List<User>findAll(@PathVariable("index") int index ,@PathVariable("limit") int limit){
        return userRepository.findAll(index, limit);
    }

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id")long id){
        return userRepository.findById(id);
    }

    @GetMapping("/count")
    public int count(){
        return userRepository.count();
    }
}

11. Check: Start the User
. A Open the postman, enter the HTTP GET: // localhost: 8040 / the User / findAll / 0/10
Here Insert Picture Description
findAll call success!
. b enter the HTTP GET: // localhost: 8040 / the User / findById / 2
Here Insert Picture Description
findById call success!
c into the HTTP GET:. // localhost: 8040 / the User / COUNT
Here Insert Picture Description
COUNT success!
d into the post http:. // localhost: 8040 / user / save
Here Insert Picture Description
enter get http: // localhost: 8040 / user / findAll / 0/10
Here Insert Picture Description
successfully added!
e enters put http:. // localhost: 8040 / user / update
Here Insert Picture Description
into the get http: // localhost: 8040 / user / findAll / 0/10
Here Insert Picture Description
updated successfully!
f enter delete http:. // localhost: 8040 / user / deleteById / 6
re-entering the get http: // localhost: 8040 / user / findAll / 0/10
Here Insert Picture Description
deleted successfully!
Such user function is realized!

On a link:
https://blog.csdn.net/qq_40893824/article/details/103609138
next section link:
https://blog.csdn.net/qq_40893824/article/details/103618769

Published 42 original articles · won praise 2 · Views 1182

Guess you like

Origin blog.csdn.net/qq_40893824/article/details/103615379