自学实前后端践项目3 Spring Cloud微服务 5

五。接下来呢接着上期开始增加编辑操作

前端页面分享存放到client的static文件夹下

链接:https://pan.baidu.com/s/1WLglEFpVb4fGNq7CJCymBg 
提取码:6nii

client menuhandler添加修改查询代码

@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable("id") long id){
     ModelAndView modelAndView = new ModelAndView();
     modelAndView.setViewName("menu_update");
     modelAndView.addObject("menu",menuFeign.findById(id));
     modelAndView.addObject("list",menuFeign.findTypes());
     return modelAndView;
}
MenuRepository.xml中sql改为
<select id="findById" resultMap="menuMap">
   select * from t_menu where id = #{id}
</select>

client MenuFeign添加修改查询代码

@GetMapping("/menu/findById/{id}")
public Menu findById(@PathVariable long id);

最后测试调用页面情况(点击修改弹出下面界面进行修改分类按id查询)

主要是前端代码实现的查询id与菜品种类id相同选中

<select name="type.id">
    <option th:each="type:${list}" th:text="${type.name}" th:value="${type.id}" th:if="${menu.type.id == type.id}" selected></option>
    <option th:each="type:${list}" th:text="${type.name}" th:value="${type.id}" th:if="${menu.type.id != type.id}" ></option>
</select>

 

 上面实现了查看显示,下面实现修改保存

client menuhandler添加

@PostMapping("/update")
public String update(Menu menu){
    menuFeign.update(menu);
    return "redirect:/menu/redirect/index";
}

 client MenuFeign添加代码

@PutMapping("/menu/update")
public void update(Menu menu);

启动服务选择29修改

 查询显示

 

 编辑

 

 保存退出显示

 

 六。创建服务提供者user

新建模块user

配置文件依赖照搬menu的吧名字改为user,端口改为8040

pom

<?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>aispringclouddemo</artifactId>
        <groupId>com.redhat</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>


    <artifactId>user</artifactId>


</project>

configserber   shared里面新加配置user-dev.yml

server:
  port: 8040
spring:
  application:
    name: user
  datasource:
    name: orderingsystem
    url: jdbc:mysql://localhost:3306/orderingsystem?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
  eureka:
    client:
      service-url:
        defaultZone: http://localhost:8761/eureka/
    instance:
      prefer-ip-address: true
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: com.redhat.entity

user的资源里面新加配置bootstrap.yml

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

创建user启动项

package com.redhat;

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

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

}
创建userhandler
package com.redhat.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 "user的端口为:"+this.port;
    }
}
重启相关服务测试

user模块下创建entity创建User实体类对照数据库类型

package com.redhat.entity;

import lombok.Data;

import java.util.Date;

@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;
}

创建UserRepository
package com.redhat.repository;

import com.redhat.entity.User;

import java.util.List;

public interface UserRepository {
    public List<User> findAll(int index,int limit);
    public User findById(long id);
    public  int count();
    public void save(User user);
    public void  update(User user);
    public void  deleteById(long id);
}

创建UserRepository.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.redhat.repository.UserRepository">

    <select id="findAll" resultType="com.redhat.entity.User">
      select*from t_user limit #{param1},#{param2}
   </select>

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

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

   <select id="findById" resultType="com.redhat.entity.User">
      select * from t_user where id = #{id}
   </select>

   <update id="update" parameterType="com.redhat.entity.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>
创建UserHandler 新增如下
package com.redhat.controller;

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

import java.util.List;

@RestController
@RequestMapping("/user")

public class UserHandler {
//   测试端口
//    @Value("${server.port}")
//    private String port;
//
//    @GetMapping("/index")
//    public String index(){
//        return "user的端口为:"+this.port;
//    }

    @Autowired
    private UserRepository userRepository;

    @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();
    }
    @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);
    }
}

最后启动服务用postman测试获取

查找全部测试

 按id查找测试

 计数测试

存储测试

 

 更改

 

 删除

 

 下面编写通过8030client端访问8040user端

 

package com.redhat.feign;

import com.redhat.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@FeignClient( value ="user")
public interface UserFeign {
     @GetMapping("/user/findAll/{index}/{limit}")
     public List<User> findAll(@PathVariable("index")int index, @PathVariable("limit")int limit);

    @GetMapping("/user/findById/{id}")
    public User findById(@PathVariable("id") long id);

    @GetMapping("/user/count")
    public int count();

    @PostMapping("/user/save")
    public void save(@RequestBody User user);

    @PutMapping("/user/update")
    public void update(@RequestBody User user);

    @DeleteMapping("/user/deleteById/{id}")
    public void deleteById(@PathVariable("id")long id);
}

 

package com.redhat.controller;

import com.redhat.entity.User;
import com.redhat.feign.UserFeign;
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 UserFeign userFeign;

    @GetMapping("/findAll/{index}/{limit}")
    public List<User> findAll(@PathVariable("index")int index, @PathVariable("limit")int limit){
        return userFeign.findAll(index,limit);
    }
    @GetMapping("/findById/{id}")
    public  User findById(@PathVariable("id") long id){
        return userFeign.findById(id);
    }
    @GetMapping("/count")
    public int count(){
        return userFeign.count();
    }

    @PostMapping("/save")
    public void save(@RequestBody User user){
        userFeign.save(user);
    }
    @PutMapping ("/update")
    public void update(@RequestBody User user){
        userFeign.update(user);
    }
    @DeleteMapping ("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        userFeign.deleteById(id);
    }
}

将user下的实体类user拷贝到下面文件夹

 

package com.redhat.entity;

import lombok.Data;

import java.util.Date;

@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;
}

重启client测试

查找全部

 按id查找

 

保存

 

 修改

 

 删除

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值