diff --git a/pom.xml b/pom.xml index 3255b12..2968e72 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/src/main/java/com/qf/myafterprojecy/MyAfterProjecyApplication.java b/src/main/java/com/qf/myafterprojecy/MyAfterProjecyApplication.java index 08b17ef..a9ed576 100644 --- a/src/main/java/com/qf/myafterprojecy/MyAfterProjecyApplication.java +++ b/src/main/java/com/qf/myafterprojecy/MyAfterProjecyApplication.java @@ -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) { diff --git a/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java new file mode 100644 index 0000000..2cf5fe3 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java @@ -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> getArticle(){ + return ArticleService.getArticleAll(); + }; + @GetMapping + public ResponseMessage
geybyID(@RequestBody ArticleDto articleDto){ + System.out.println("id:"+ articleDto.getArticleid()); + return ArticleService.getArticleByid(Integer.valueOf(articleDto.getArticleid())); + } + +} diff --git a/src/main/java/com/qf/myafterprojecy/demos/web/BasicController.java b/src/main/java/com/qf/myafterprojecy/demos/web/BasicController.java deleted file mode 100644 index 8554f3d..0000000 --- a/src/main/java/com/qf/myafterprojecy/demos/web/BasicController.java +++ /dev/null @@ -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 theonefx - */ -@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); - } -} diff --git a/src/main/java/com/qf/myafterprojecy/demos/web/PathVariableController.java b/src/main/java/com/qf/myafterprojecy/demos/web/PathVariableController.java deleted file mode 100644 index b37bd66..0000000 --- a/src/main/java/com/qf/myafterprojecy/demos/web/PathVariableController.java +++ /dev/null @@ -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 theonefx - */ -@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; - } -} diff --git a/src/main/java/com/qf/myafterprojecy/demos/web/User.java b/src/main/java/com/qf/myafterprojecy/demos/web/User.java deleted file mode 100644 index cda6dec..0000000 --- a/src/main/java/com/qf/myafterprojecy/demos/web/User.java +++ /dev/null @@ -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 theonefx - */ -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; - } -} diff --git a/src/main/java/com/qf/myafterprojecy/pojo/Article.java b/src/main/java/com/qf/myafterprojecy/pojo/Article.java new file mode 100644 index 0000000..07fa9e2 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/pojo/Article.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java b/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java new file mode 100644 index 0000000..1b826b0 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/pojo/ResponseMessage.java @@ -0,0 +1,19 @@ +package com.qf.myafterprojecy.pojo; + +import lombok.Data; +import org.springframework.http.HttpStatus; + +public class ResponseMessage { + 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 ResponseMessage success(T data) { + return new ResponseMessage(HttpStatus.OK.value(), "success", data); + } +} diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java new file mode 100644 index 0000000..9d7c104 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArticleDto.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java b/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java new file mode 100644 index 0000000..6f03b76 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java @@ -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 { +} diff --git a/src/main/java/com/qf/myafterprojecy/service/ArticleService.java b/src/main/java/com/qf/myafterprojecy/service/ArticleService.java new file mode 100644 index 0000000..c98b6e3 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/service/ArticleService.java @@ -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
getArticleByid(Integer articleid){ + Article articlenew = articleRepository.findById(articleid).orElseThrow(RuntimeException::new); + return ResponseMessage.success(articlenew) ; + } + @Override + public ResponseMessage> getArticleAll(){ + Iterable
Articles = articleRepository.findAll(); + return ResponseMessage.success(Articles); + } +} diff --git a/src/main/java/com/qf/myafterprojecy/service/IArticleService.java b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java new file mode 100644 index 0000000..b9df6c8 --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java @@ -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
getArticleByid(Integer articleid); + + ResponseMessage> getArticleAll(); +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 09ee3d4..43f261a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,13 @@ # 应用服务 WEB 访问端口 +server.port=8080 spring.application.name=web_project -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:jdbc:mysql://localhost:3306//webproject +spring.datasource.url=jdbc:mysql://localhost:3306/webporject spring.datasource.username=root spring.datasource.password=123456 -server.port=8080 +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true #下面这些内容是为了让MyBatis映射 #指定Mybatis的Mapper文件 mybatis.mapper-locations=classpath:mappers/*xml