编写mvc基础代码添加ResponseMessage编写Message关键字

This commit is contained in:
qingfeng1121
2025-10-09 12:12:42 +08:00
parent 1d4dee573e
commit 6841ba67f1
13 changed files with 282 additions and 157 deletions

View File

@@ -1,9 +1,11 @@
package com.qf.myafterprojecy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.qf.myafterprojecy.controller")
public class MyAfterProjecyApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,30 @@
package com.qf.myafterprojecy.controller;
import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
import com.qf.myafterprojecy.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/article")
public class ArticleController {
/**
*
*/
@Autowired
IArticleService ArticleService;
@PostMapping
public ResponseMessage<Iterable<Article>> getArticle(){
return ArticleService.getArticleAll();
};
@GetMapping
public ResponseMessage<Article> geybyID(@RequestBody ArticleDto articleDto){
System.out.println("id:"+ articleDto.getArticleid());
return ArticleService.getArticleByid(Integer.valueOf(articleDto.getArticleid()));
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qf.myafterprojecy.demos.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Controller
public class BasicController {
// http://127.0.0.1:8080/hello?name=lisi
@RequestMapping("/hello")
@ResponseBody
public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
return "Hello " + name;
}
// http://127.0.0.1:8080/user
@RequestMapping("/user")
@ResponseBody
public User user() {
User user = new User();
user.setName("theonefx");
user.setAge(666);
return user;
}
// http://127.0.0.1:8080/save_user?name=newName&age=11
@RequestMapping("/save_user")
@ResponseBody
public String saveUser(User u) {
return "user will save: name=" + u.getName() + ", age=" + u.getAge();
}
// http://127.0.0.1:8080/html
@RequestMapping("/html")
public String html(){
return "index.html";
}
@ModelAttribute
public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
, @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
user.setName("zhangsan");
user.setAge(18);
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qf.myafterprojecy.demos.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Controller
public class PathVariableController {
// http://127.0.0.1:8080/user/123/roles/222
@RequestMapping(value = "/user/{userId}/roles/{roleId}", method = RequestMethod.GET)
@ResponseBody
public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {
return "User Id : " + userId + " Role Id : " + roleId;
}
// http://127.0.0.1:8080/javabeat/somewords
@RequestMapping(value = "/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET)
@ResponseBody
public String getRegExp(@PathVariable("regexp1") String regexp1) {
return "URI Part : " + regexp1;
}
}

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qf.myafterprojecy.demos.web;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

View File

@@ -0,0 +1,95 @@
package com.qf.myafterprojecy.pojo;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "articleid")
private Integer articleid;
@Column(name = "title")
private String title;
@Column(name = "content")
private String content;
@Column(name = "img")
private String img;
@Column(name = "typeid")
private int typeid;
@Column(name = "published_at")
private String publisher_at;
@Column(name = "created_at")
private String created_at;
public Integer getArticleid() {
return articleid;
}
public void setArticleid(Integer articleid) {
this.articleid = articleid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
public String getPublisher_at() {
return publisher_at;
}
public void setPublisher_at(String publisher_at) {
this.publisher_at = publisher_at;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
@Override
public String toString() {
return "Article{" +
"articleid=" + articleid +
", title='" + title + '\'' +
", content='" + content + '\'' +
", img='" + img + '\'' +
", typeid=" + typeid +
", publisher_at='" + publisher_at + '\'' +
", created_at='" + created_at + '\'' +
'}';
}
}

View File

@@ -0,0 +1,19 @@
package com.qf.myafterprojecy.pojo;
import lombok.Data;
import org.springframework.http.HttpStatus;
public class ResponseMessage<T> {
private Integer code;
private String message;
private T data;
public ResponseMessage(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
// 接口请求成功
public static <T> ResponseMessage<T> success(T data) {
return new ResponseMessage(HttpStatus.OK.value(), "success", data);
}
}

View File

@@ -0,0 +1,82 @@
package com.qf.myafterprojecy.pojo.dto;
import javax.persistence.Column;
public class ArticleDto {
private Integer articleid;
private String title;
private String content;
private String img;
private Integer typeid;
private String publisher_at;
private String created_at;
public Integer getArticleid() {
return articleid;
}
public void setArticleid(Integer articleid) {
this.articleid = articleid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getPublisher_at() {
return publisher_at;
}
public void setPublisher_at(String publisher_at) {
this.publisher_at = publisher_at;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
@Override
public String toString() {
return "ArticleDto{" +
"articleid=" + articleid +
", title='" + title + '\'' +
", content='" + content + '\'' +
", img='" + img + '\'' +
", typeid=" + typeid +
", publisher_at='" + publisher_at + '\'' +
", created_at='" + created_at + '\'' +
'}';
}
}

View File

@@ -0,0 +1,9 @@
package com.qf.myafterprojecy.repository;
import com.qf.myafterprojecy.pojo.Article;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ArticleRepository extends CrudRepository<Article,Integer> {
}

View File

@@ -0,0 +1,24 @@
package com.qf.myafterprojecy.service;
import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ArticleService implements IArticleService{
@Autowired
ArticleRepository articleRepository;
@Override
public ResponseMessage<Article> getArticleByid(Integer articleid){
Article articlenew = articleRepository.findById(articleid).orElseThrow(RuntimeException::new);
return ResponseMessage.success(articlenew) ;
}
@Override
public ResponseMessage<Iterable<Article>> getArticleAll(){
Iterable<Article> Articles = articleRepository.findAll();
return ResponseMessage.success(Articles);
}
}

View File

@@ -0,0 +1,11 @@
package com.qf.myafterprojecy.service;
import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.ResponseMessage;
public interface IArticleService {
ResponseMessage<Article> getArticleByid(Integer articleid);
ResponseMessage<Iterable<Article>> getArticleAll();
}