spring boot+mybatis搭建一個後端restfull服務的例項詳解
1、建立一個maven專案。
2、在pom.xml中引入依賴包,如下所示:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.best</groupId> <artifactId>spring-boot-mybatis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <fastjson.version>1.2.74</fastjson.version> <spring.version>5.3.5.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <!-- 排除 tomcat-jdbc 以使用 HikariCP --> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-security</artifactId>--> <!-- </dependency>--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
3、在使用mybatis之前,那麼首先需要建立資料表,如:
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) COLLATE utf8_bin DEFAULT NULL,`create_date` datetime DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1','user1','2020-11-10 21:01:54'); INSERT INTO `user` VALUES ('2','user2','2020-11-01 21:02:12');
4、然後配置資料庫連線池,這裡使用的是alibaba的druid,首先在resources中新建一個application.yml檔案,內容如下:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver platform: mysql url: jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT username: root password: server: port: 8080
5、然後新建一個包com.best.db,並新建一個DruidDBConfig類,程式碼如下:
package com.best.db; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @author:sunxj * @date:2020-11-10 21:08:53 * @description: 資料連線池 */ @Configuration public class DruidDBConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Bean//DataSource 物件為 Spring 容器所管理; @Primary//表示這裡定義的DataSource將覆蓋其他來源的DataSource。 public DataSource dataSource(){ DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.url); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); return datasource; } //配置資料庫事務 @Bean(name = "transactionManager") public DataSourceTransactionManager dbOneTransactionManager( @Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } //配置資料庫工廠 @Bean(name = "sqlSessionFactory") @ConditionalOnMissingBean(name = "sqlSessionFactory") public SqlSessionFactory dbOneSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } }
6、配置好連線池,那麼就可以開始配置entity了,也就是與表對映的實體類,這裡我們使用lombok的註解自動生成set和get方法,那麼新建一個com.best.entity包,並新建一個User類,如:
package com.best.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; /** * @author:sunxj * @date:2020-11-10 21:12:58 * @description:使用者實體類 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private Integer id; private String name; private Date createDate; }
7、實體類建立好了之後,那麼就是建立mybatis對映檔案以及類了,這裡的對映介面檔案和Mapper.xml檔案放到一起,首先建立一個com.best.dbo包,對映介面檔案和.xml都放到這個包中,並建立一個介面UserMapper,如:
package com.best.dao; import com.best.entity.User; import org.springframework.stereotype.Repository; import java.util.List; /** * @author:sunxj * @date:2020-11-10 21:17:27 * @description:使用者表的對映介面檔案 */ @Repository public interface UserMapper { List<User> selectAll(); User selectById(Integer id); }
8、配置Mapper對映的xml檔案,檔名為UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.best.dao.UserMapper"> <resultMap type="com.best.entity.User" id="UserMap"> <result property="id" column="id" jdbcType="INTEGER"></result> <result property="name" column="name" jdbcType="VARCHAR"></result> <result property="createDate" column="create_date" jdbcType="TIMESTAMP"></result> </resultMap> <select id="selectAll" resultMap="UserMap"> SELECT * FROM user </select> <select id="selectById" resultMap="UserMap"> SELECT * FROM user WHERE id=#{id} </select> </mapper>
9、在配置好之後就可以在application.xml來指定mybatis掃描哪些對映檔案,配置如下:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver platform: mysql url: jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT username: root password: mybatis: #如果有多個目錄則可以寫多個,比如com/best/dao/*.xml,com/best/*/dao/*.xml,com/best/*/sss/*/dao/*.xml mapper-locations: classpath:com/best/dao/*.xml #配置包,這裡同樣可以配置多個, type-aliases-package: com.best.dao server: port: 8080
10、配置好之後就開始配置service以及impl了,首先建立一個service和service/impl包,並在service下建立一個IUserService介面,並在impl下來實現該介面UserServiceImpl,程式碼如下:
package com.best.service; import com.best.entity.User; import java.util.List; /** * @author:sunxj * @date:2020-11-10 21:33:23 * @description:使用者介面表 */ public interface IUserService { List<User> selectAll(); User selectById(Integer id); }
package com.best.service.impl; import com.best.dao.UserMapper; import com.best.entity.User; import com.best.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author:sunxj * @date:2020-11-10 21:34:34 * @description:使用者介面實現類 */ @Service("userService") public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public List<User> selectAll() { return userMapper.selectAll(); } @Override public User selectById(Integer id) { return userMapper.selectById(id); } }
11、在建立好service後,需要建立一個controller,如UserController,程式碼如下:
package com.best.controller; import com.alibaba.fastjson.JSONObject; import com.best.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author:sunxj * @date:2020-11-10 21:38:09 * @description: */ @RestController//使用此註解,那麼該類下的所有返回都是以json方式返回 @RequestMapping(value="best")//加上value後,在url訪問時必須加上/best才行 public class UserController { @Autowired private IUserService userService; @RequestMapping("/getUser") public JSONObject getUser(HttpServletRequest request,HttpServletResponse response) { JSONObject obj = new JSONObject(); obj.put("user","1111"); return obj; } }
12、建立好之後,最後建立一個spring-boot的入口檔案,程式碼如下:
package com.best; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author:sunxj * @date:2020-11-10 21:46:43 * @description: */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
13、此時啟動會提示無法找到com.best.dao.UserMapper,如:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-11-10 21:48:21.635 ERROR 10320 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field userMapper in com.best.service.impl.UserServiceImpl required a bean of type 'com.best.dao.UserMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.best.dao.UserMapper' in your configuration.
14、出現此問題是由於springboot在啟動時,在UserServiceImpl中又使用到了UserMapper的自動注入,而springboot啟動時就沒有掃描到UserMapper,更不可能將它作為bean注入到IOC容器中,因此就無法注入了,那麼需要再入口的main檔案上加上@MapperScan即可,如:
package com.best; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author:sunxj * @date:2020-11-10 21:46:43 * @description: */ @SpringBootApplication @MapperScan({"com.best.dao"})//如果有多個可以使用{"com.best.dao","com.best.sss.dao"} public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
15、此時即可正常啟動,如下圖所示:
16、此時在瀏覽器中輸入:http://localhost:8080/best/getUser即可,如下圖所示:
17、以上只是搭建一個可以正常通過url訪問,那麼現在來訪問資料庫中的記錄,在訪問之前先建立一些封裝類,比如將實體類封裝成json、返回狀態碼等,先建立一個common包,在該包下建立entity、enums、utils等包,如下圖所示:
18、然後在enums中建立一個ResultCode列舉類,此來定義了一些返回狀態碼資訊,如下程式碼所示:
package com.best.common.enums; /** * @author:sunxj * @date:2020-11-10 22:08:11 * @description:返回碼定義 */ public enum ResultCode { //成功 SUCCESS(200,"成功"),//預設失敗 COMMON_FAIL(404,"失敗"),; private Integer code; private String message; ResultCode(Integer code,String message) { this.code = code; this.message = message; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public static String getMessageByCode(Integer code) { for (ResultCode ele : values()) { //values會預設取出enum中的值物件列表,每個值都是一個ResultCode物件 if (ele.getCode().equals(code)) { return ele.getMessage(); } } return null; } }
19、然後在entity中新建一個JsonResult類,用來封裝統一返回的實體類,如:
package com.best.common.entity; import com.best.common.enums.ResultCode; import java.io.Serializable; /** * @author:sunxj * @date:2020-11-10 22:06:50 * @description:統一返回的實體類 */ public class JsonResult<T> implements Serializable { private Boolean success; private Integer errorCode; private String errorMsg; private T data; public JsonResult() { } public JsonResult(Boolean success) { this.success = success; this.errorCode = success ? ResultCode.SUCCESS.getCode():ResultCode.COMMON_FAIL.getCode(); this.errorMsg = success ? ResultCode.SUCCESS.getMessage():ResultCode.COMMON_FAIL.getMessage(); } public JsonResult(Boolean success,ResultCode resultCode) { this.success = success; this.errorCode = success ? ResultCode.SUCCESS.getCode():(resultCode == null ? ResultCode.COMMON_FAIL.getCode():resultCode.getCode()); this.errorMsg = success ? ResultCode.SUCCESS.getMessage():(resultCode == null ? ResultCode.COMMON_FAIL.getMessage():resultCode.getMessage()); } public JsonResult(Boolean success,T data) { this.success = success; this.errorCode = success ? ResultCode.SUCCESS.getCode():ResultCode.COMMON_FAIL.getCode(); this.errorMsg = success ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage(); this.data = data; } public JsonResult(Boolean success,ResultCode resultCode,T data) { this.success = success; this.errorCode = success ? ResultCode.SUCCESS.getCode():(resultCode == null ? ResultCode.COMMON_FAIL.getCode():resultCode.getCode()); this.errorMsg = success ? ResultCode.SUCCESS.getMessage():(resultCode == null ? ResultCode.COMMON_FAIL.getMessage():resultCode.getMessage()); this.data = data; } public Boolean getSuccess() { return success; } public void setSuccess(Boolean success) { this.success = success; } public Integer getErrorCode() { return errorCode; } public void setErrorCode(Integer errorCode) { this.errorCode = errorCode; } public String getErrorMsg() { return errorMsg; } public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
20、然後在utils中新建一個ResultTool,如下程式碼所示:
package com.best.common.utils; import com.best.common.entity.JsonResult; import com.best.common.enums.ResultCode; /** * @author:sunxj * @date:2020-11-10 22:11:03 * @description:json返回構造工具 */ public class ResultTool { public static JsonResult success() { return new JsonResult(true); } public static <T> JsonResult<T> success(T data) { return new JsonResult<>(true,data); } public static JsonResult fail() { return new JsonResult(false); } public static JsonResult fail(ResultCode resultCode) { return new JsonResult(false,resultCode); } }
21、建立好這些之後,那麼就可以開始在controller中進行構造了,controller如下:
package com.best.controller; import com.best.common.entity.JsonResult; import com.best.common.utils.ResultTool; import com.best.entity.User; import com.best.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * @author:sunxj * @date:2020-11-10 21:38:09 * @description: */ @RestController//使用此註解,那麼該類下的所有返回都是以json方式返回 @RequestMapping(value="best")//加上value後,在url訪問時必須加上/best才行 public class UserController { @Autowired private IUserService userService; @RequestMapping("/getUser") public JsonResult getUser(HttpServletRequest request,HttpServletResponse response) { List<User> users = userService.selectAll(); return ResultTool.success(users); } }
22、重新啟動springboot,然後輸入:http://localhost:8080/best/getUser,此時頁面會出現如下錯誤:
23、後臺顯示如下錯誤:
package com.best.controller; import com.best.common.entity.JsonResult; import com.best.common.utils.ResultTool; import com.best.entity.User; import com.best.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * @author:sunxj * @date:2020-11-10 21:38:09 * @description: */ @RestController//使用此註解,那麼該類下的所有返回都是以json方式返回 @RequestMapping(value="best")//加上value後,在url訪問時必須加上/best才行 public class UserController { @Autowired private IUserService userService; @RequestMapping("/getUser") public JsonResult getUser(HttpServletRequest request,HttpServletResponse response) { List<User> users = userService.selectAll(); return ResultTool.success(users); } }
23、這意思就是沒有找到UserMapper介面檔案中的selectAll方法,也就是沒有和*Mapper.xml匹配成功,這是由於我們將.xml檔案和java檔案放在了java目錄,而通過maven來管理,它不會將java檔案中的xml檔案打包進去的,因此需要再pom中的build中指定,如:
2020-11-10 22:15:10.487 ERROR 12992 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.best.dao.UserMapper.selectAll] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.best.dao.UserMapper.selectAll
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.5.jar:3.5.5]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.5.jar:3.5.5]
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115) ~[mybatis-3.5.5.jar:3.5.5]
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_261]
at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102) ~[mybatis-3.5.5.jar:3.5.5]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.5.jar:3.5.5]
at com.sun.proxy.$Proxy57.selectAll(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_261]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_261]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_261]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_261]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at com.sun.proxy.$Proxy58.selectAll(Unknown Source) ~[na:na]
at com.best.service.impl.UserServiceImpl.selectAll(UserServiceImpl.java:22) ~[classes/:na]
at com.best.controller.UserController.getUser(UserController.java:26) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_261]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_261]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_261]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_261]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:41002) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.39.jar:9.0.39]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_261]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_261]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.39.jar:9.0.39]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_261]
24、然後在此重新執行,重新輸入網址即可,如下圖所示:
25、到此即可搭建完畢,使用此方法可以搭建一個前後端完全分離通過json通訊的springboot後臺。
到此這篇關於spring boot+mybatis搭建一個後端restfull服務的文章就介紹到這了,更多相關spring boot+mybatis搭建restfull服務內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!