springclouddemo5.1服务提供者-user

从b站学习springcloud项目,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒

b站链接:https://www.bilibili.com/video/av55629580?p=1
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru

上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103609138
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103618769

下面的内容总结:
module(user)→configserver/shared新建user-dev.yml→bootstrap.yml→
com.southwind.UserApplication.java→controler.UserHandler.java→检查

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

对用户的增、删、查操作
实现细节:
1.新建module命名为user,其pom文件添加的代码和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.配置:在confiserver/resources/shared中再复制一份menu-dev.yml但改名为user-dev.yml,并修改port为8040,name为user,其他不变:

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读取configserver中的配置将menu/resources中的bootstrap.yml复制至user/resources,修改name为user:

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

4.在user/java中创包com.southwind,在southwind中新建启动类UserApplication.java加入代码:

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.在user/southwind中创包controller,在里面新建控制器UserHandler.java,加入代码:

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.重启 configserver 启动user 检查端口是否可以调通:进入 http://localhost:8761
在这里插入图片描述
再进入 http://localhost:8040/user/index
在这里插入图片描述
说明可以调通!

实现数据接口:
7.在user/southwind中创包entity,创建实体类User.java,加入代码:

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是注册日期,其类型可以这样查看:
对着t_user右键单击
在这里插入图片描述
在这里插入图片描述
可以看到registerdate的类型是Date
8.在southwind中创包repository,在里面新建接口UserRepository.java,加入代码:

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.在user/resources中创包mapping,将menu/mapping中的MenuRepository.xml复制至User/mapping中,并改名为UserRepository.xml,修改代码:
第三行MenuRepository改为UserRepository
删除5-11行的< resultMap>
现在的第5行findAll 和第13行的findById 中 resultMap=“menuMap” 改为 resultType=“User”
下一行的 及后面所有的t_menu 改为 t_user
17和21行的save和update修改,对应user中的属性

<?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.现在去user/UserHandler中修改代码:
删除port的代码
然后加入代码:

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.检查:启动user
a. 打开postman,进入 get http://localhost:8040/user/findAll/0/10
在这里插入图片描述
findAll调用成功!
b. 进入 get http://localhost:8040/user/findById/2
在这里插入图片描述
findById调用成功!
c. 进入 get http://localhost:8040/user/count
在这里插入图片描述
count成功!
d. 进入 post http://localhost:8040/user/save
在这里插入图片描述
进入 get http://localhost:8040/user/findAll/0/10
在这里插入图片描述
成功添加!
e. 进入 put http://localhost:8040/user/update
在这里插入图片描述
进入 get http://localhost:8040/user/findAll/0/10
在这里插入图片描述
更新成功!
f. 进入 delete http://localhost:8040/user/deleteById/6
再进入 get http://localhost:8040/user/findAll/0/10
在这里插入图片描述
成功删除!
这样user的功能就实现了!

上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103609138
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103618769

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值