Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建
一、 建立service、serviceImpl、controller包,建立完之後的專案結構如下:
二、編寫UserService和UserServiceImpl
UserService.java
package com.kai.demo.service; import com.kai.demo.model.User; public interface UserService { public String show(); User selectByPrimaryKey(Integer id); }
UserServiceImpl.java
package com.kai.demo.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.kai.demo.dao.UserMapper; import com.kai.demo.model.User; import com.kai.demo.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public String show() { return "test service"; } @Override public User selectByPrimaryKey(Integer id) { // TODO Auto-generated method stub return userMapper.selectByPrimaryKey(id); } }
三、UserController.java
package com.kai.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.kai.demo.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/show") public String show(){ return userService.show(); } @RequestMapping("/getUser") public String getUser() { return userService.selectByPrimaryKey(1).toString(); } }
四、對UserController進行單元測試
@RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTests { @Autowired private WebApplicationContext context; private MockMvc mvc; @Before public void setUp() throws Exception { mvc=MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void show() throws Exception{ mvc.perform(MockMvcRequestBuilders.get("/show").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } @Test public void getUser() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/getUser") .accept(MediaType.APPLICATION_JSON_UTF8)).andDo(MockMvcResultHandlers.print()); } }
結果:
完整環境下載地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10314474/viewspace-2200333/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(三):DataSource配置Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(五):templates模板使用Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(一) :Spring Boot初始化,依賴新增Spring BootMyBatisMVC
- 搭建MSSM框架(Maven+Spring+Spring MVC+MyBatis)SSM框架MavenSpringMVCMyBatis
- spring、spring MVC、spring BootMVCSpring Boot
- IDEA配置SSM(Spring-Spring MVC-MyBatisIdeaSSMSpringMVCMyBatis
- 使用IntelliJ IDEA 搭建 spring mvc開發環境IntelliJIdeaSpringMVC開發環境
- spring mvc 框架搭建及詳解SpringMVC框架
- 精盡Spring MVC原始碼分析 - 調式環境搭建SpringMVC原始碼
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring MVC和Spring Boot的區別 - hackernoonMVCSpring Boot
- Spring MVC 到 Spring Boot 的簡化之路MVCSpring Boot
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- Spring Boot - Profile不同環境配置Spring Boot
- 基於spring boot 及mybatis的web開發環境搭建Spring BootMyBatisWeb開發環境
- Spring MVC整合redis的配置SpringMVCRedis
- spring - mvcSpringMVC
- 手寫Spring MVC框架(一) 實現簡易版mvc框架SpringMVC框架
- IDEA社群版搭建Spring工程(03-Spring MVC搭建)IdeaSpringMVC
- spring boot與spring mvc的區別是什麼?Spring BootMVC
- Spring MVC框架有哪些優點SpringMVC框架
- Spring(環境搭建&配置詳解)Spring
- spring mvc專案配置一覽SpringMVC
- 程式設計體系結構(08):Spring.Mvc.Boot框架程式設計SpringMVCboot框架
- spring - mvc - @ScheduledSpringMVC
- spring - mvc - @ValidSpringMVC
- spring mvc interceptorsSpringMVC
- spring boot學習4 多環境配置Spring Boot
- spring boot環境抽象Spring Boot抽象
- Spring MVC1SpringMVC
- Spring MVC基礎SpringMVC
- Spring MVC學習SpringMVC
- Spring MVC應用SpringMVC
- Spring5 MVCSpringMVC
- Spring MVC 簡述SpringMVC
- 使用 Spring Boot 和 @WebMvcTest 測試 MVC Web ControllerSpring BootWebMVCController
- tomcat + spring mvc原理外傳:spring mvc與前端的糾葛TomcatSpringMVC前端