Controller层实现

This commit is contained in:
myh 2023-10-06 23:32:28 +08:00
parent 669c470916
commit 37988c4ff0
2 changed files with 49 additions and 42 deletions

View File

@ -0,0 +1,49 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.service.MerchantsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/merchants")
public class MerchantsController {
@Resource
private MerchantsService merchantsService;
@GetMapping("/findAll")
public List<Merchants> getMerchants() {
return merchantsService.findAll();
}
@GetMapping("/findByName")
public List<Merchants> getMerchantsByName(@RequestParam("name") String name) {
return merchantsService.findByName(name);
}
@GetMapping("/save")
public String saveMerchants(@RequestParam("name") String name,
@RequestParam("address") String address,
@RequestParam("description") String description,
@RequestParam("phoneNumber") String phoneNumber) {
Merchants merchants = new Merchants();
merchants.setName(name);
merchants.setAddress(address);
merchants.setDescription(description);
merchants.setPhoneNumber(phoneNumber);
merchantsService.save(merchants);
return "success";
}
@GetMapping("/delete")
public String deleteMerchants(@RequestParam("id") Long id) {
merchantsService.deleteById(id);
return "success";
}
}

View File

@ -1,42 +0,0 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.Business;
import com.example.springdemo.config.DataBaseProperties;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("/student")
public class StudentController {
@Resource
DataBaseProperties dbProps;
@GetMapping("/get")
public List<String> getStudent() {
// DataBaseProperties dbProps = new DataBaseProperties();
return List.of(dbProps.getUsername(), dbProps.getPassword(), dbProps.getDbUrl());
}
@GetMapping("/getBy")
public Business getStudentById(@RequestParam String id) {
var bs = Business.builder()
.name(id)
.tel(dbProps.getPassword())
.build();
if (!Objects.equals(bs.getTel(), "")) {
bs.setTel("");
}
return bs;
}
}