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 Boot/Spring MVCSpring BootMVC
- spring、spring MVC、spring BootMVCSpring Boot
- Spring搭建Web應用(MVC)起步(Tomcat環境)SpringWebMVCTomcat
- spring mvc 框架搭建及詳解SpringMVC框架
- IDEA配置SSM(Spring-Spring MVC-MyBatisIdeaSSMSpringMVCMyBatis
- Spring、Spring MVC、MyBatis 整合檔案配置詳解SpringMVCMyBatis
- Spring、Spring MVC、MyBatis整合檔案配置詳解SpringMVCMyBatis
- Spring MVC 零配置 / Spring MVC JavaConfigSpringMVCJava
- 使用IntelliJ IDEA 搭建 spring mvc開發環境IntelliJIdeaSpringMVC開發環境
- 精盡Spring MVC原始碼分析 - 調式環境搭建SpringMVC原始碼
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring MVC DispatcherServlet 配置SpringMVCServlet
- Java Web之基於註解的Spring MVC環境配置JavaWebSpringMVC
- 基於spring boot 及mybatis的web開發環境搭建Spring BootMyBatisWeb開發環境
- Spring Boot - Profile不同環境配置Spring Boot
- Spring MVC和Spring Boot的區別 - hackernoonMVCSpring Boot
- Spring MVC 到 Spring Boot 的簡化之路MVCSpring Boot
- 玩轉spring boot——MVC應用Spring BootMVC
- 手寫Spring MVC框架(一) 實現簡易版mvc框架SpringMVC框架
- 8、使用 Spring Boot 搭建的一個 Spring MVC 示例(持續更新中)Spring BootMVC
- Spring MVC整合redis的配置SpringMVCRedis
- Spring(環境搭建&配置詳解)Spring
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- Spring MVCSpringMVC
- spring - mvcSpringMVC
- Spring MVC框架有哪些優點SpringMVC框架
- 程式設計體系結構(08):Spring.Mvc.Boot框架程式設計SpringMVCboot框架
- spring boot與spring mvc的區別是什麼?Spring BootMVC
- spring boot應用之spring mvc應用書目錄Spring BootMVC
- spring boot學習4 多環境配置Spring Boot
- IDEA社群版搭建Spring工程(03-Spring MVC搭建)IdeaSpringMVC
- spring mvc專案配置一覽SpringMVC
- web.xml+spring mvc基本配置WebXMLSpringMVC