diff --git a/pom.xml b/pom.xml index dcd9ffc..73de8d7 100644 --- a/pom.xml +++ b/pom.xml @@ -64,11 +64,18 @@ commons-lang 2.6 + - org.apache.commons - commons-lang3 - 3.4 + com.alibaba.fastjson2 + fastjson2-extension-spring6 + 2.0.43 + + junit + junit + test + + diff --git a/src/main/java/com/lovenav/controller/CommentComtroller.java b/src/main/java/com/lovenav/controller/CommentComtroller.java deleted file mode 100644 index 5adf5d3..0000000 --- a/src/main/java/com/lovenav/controller/CommentComtroller.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.lovenav.controller; - -import com.lovenav.entity.Comment; -import com.lovenav.service.CommentService; -import com.lovenav.service.serviceImpl.CommentServiceImpl; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CommentComtroller { - private CommentServiceImpl commentServiceImpl; - @PostMapping(value= "/AddLikeCount",produces="application/json;charset=UTF-8") - public int AddLikeCount(@RequestBody Comment comment) { - return commentServiceImpl.AddLikeCount(comment); - } - - -} diff --git a/src/main/java/com/lovenav/controller/CommentController.java b/src/main/java/com/lovenav/controller/CommentController.java new file mode 100644 index 0000000..261363b --- /dev/null +++ b/src/main/java/com/lovenav/controller/CommentController.java @@ -0,0 +1,23 @@ +package com.lovenav.controller; + +import com.lovenav.entity.Comment; +import com.lovenav.service.serviceImpl.CommentServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/comment") +public class CommentController { + @Autowired + private CommentServiceImpl commentServiceImpl; + @PostMapping(value= "/AddLikeCount",produces="application/json;charset=UTF-8") + public String AddLikeCount(@RequestBody Comment comment) { + return commentServiceImpl.AddLikeCount(comment); + } + + @GetMapping("/hello") + public String he(){ + System.out.println("hello world"); + return "Hello world!"; + } +} diff --git a/src/main/java/com/lovenav/dao/CommentDao.java b/src/main/java/com/lovenav/dao/CommentDao.java index 1394297..3fa5b00 100644 --- a/src/main/java/com/lovenav/dao/CommentDao.java +++ b/src/main/java/com/lovenav/dao/CommentDao.java @@ -1,9 +1,11 @@ package com.lovenav.dao; import com.lovenav.entity.Comment; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; @Repository + public interface CommentDao { int deleteByPrimaryKey(Integer id); diff --git a/src/main/java/com/lovenav/service/CommentService.java b/src/main/java/com/lovenav/service/CommentService.java index c1838ac..22f9c12 100644 --- a/src/main/java/com/lovenav/service/CommentService.java +++ b/src/main/java/com/lovenav/service/CommentService.java @@ -4,5 +4,5 @@ import com.lovenav.entity.Comment; public interface CommentService { // 点赞 - public int AddLikeCount(Comment comment); + public String AddLikeCount(Comment comment); } diff --git a/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java index 00292e2..794ceb6 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java @@ -1,17 +1,21 @@ package com.lovenav.service.serviceImpl; +import com.alibaba.fastjson2.JSONObject; import com.lovenav.dao.CommentDao; import com.lovenav.entity.Comment; import com.lovenav.service.CommentService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +@Service public class CommentServiceImpl implements CommentService { private CommentDao commentDao; // 点赞++ - public int AddLikeCount(Comment comment){ + public String AddLikeCount(Comment comment){ comment.setLikeCount(comment.getLikeCount()+1); - return commentDao.updateByPrimaryKeySelective(comment); + commentDao.updateByPrimaryKeySelective(comment); + return JSONObject.toJSONString(comment); } } diff --git a/src/test/java/com/lovenav/CommentTest.java b/src/test/java/com/lovenav/CommentTest.java new file mode 100644 index 0000000..f9b41cb --- /dev/null +++ b/src/test/java/com/lovenav/CommentTest.java @@ -0,0 +1,49 @@ + +package com.lovenav; +import com.lovenav.controller.CommentController; +import org.junit.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.mybatis.spring.annotation.MapperScan; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +/** + * + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@WebAppConfiguration +@MapperScan("com/lovenav/dao") +public class CommentTest { + private MockMvc mockMvc; + @Autowired + private WebApplicationContext webApplicationContext; + @Before + public void setUp() { + //此种方式可通过spring上下文来自动配置一个或多个controller + //mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + + //此种方式,手工指定想要的controller + mockMvc = MockMvcBuilders.standaloneSetup(new CommentController()).build(); + } + + @Test + public void Test() throws Exception{ + //构造请求 + MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/comment/hello"); +// MockMvcResultHandlers.print(); + mockMvc.perform(request); + } +} \ No newline at end of file