spring boot+mybatis搭建一個(gè)后端restfull服務(wù)的實(shí)例詳解
1、創(chuàng)建一個(gè)maven項(xiàng)目。
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之前,那么首先需要?jiǎng)?chuàng)建數(shù)據(jù)表,如:
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、然后配置數(shù)據(jù)庫(kù)連接池,這里使用的是alibaba的druid,首先在resources中新建一個(gè)application.yml文件,內(nèi)容如下:
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、然后新建一個(gè)包c(diǎn)om.best.db,并新建一個(gè)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: 數(shù)據(jù)連接池 */@Configurationpublic 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 對(duì)象為 Spring 容器所管理; @Primary//表示這里定義的DataSource將覆蓋其他來(lái)源的DataSource。 public DataSource dataSource(){ DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.url); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); return datasource; } //配置數(shù)據(jù)庫(kù)事務(wù) @Bean(name = 'transactionManager') public DataSourceTransactionManager dbOneTransactionManager( @Qualifier('dataSource') DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } //配置數(shù)據(jù)庫(kù)工廠 @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了,也就是與表映射的實(shí)體類,這里我們使用lombok的注解自動(dòng)生成set和get方法,那么新建一個(gè)com.best.entity包,并新建一個(gè)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:用戶實(shí)體類 */@Data@AllArgsConstructor@NoArgsConstructorpublic class User { private Integer id; private String name; private Date createDate;}
7、實(shí)體類創(chuàng)建好了之后,那么就是創(chuàng)建mybatis映射文件以及類了,這里的映射接口文件和Mapper.xml文件放到一起,首先創(chuàng)建一個(gè)com.best.dbo包,映射接口文件和.xml都放到這個(gè)包中,并創(chuàng)建一個(gè)接口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:用戶表的映射接口文件 */@Repositorypublic 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 resultMap='UserMap'> SELECT * FROM user </select> <select resultMap='UserMap'> SELECT * FROM user WHERE id=#{id} </select></mapper>
9、在配置好之后就可以在application.xml來(lái)指定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: #如果有多個(gè)目錄則可以寫多個(gè),比如com/best/dao/*.xml,com/best/*/dao/*.xml,com/best/*/sss/*/dao/*.xml mapper-locations: classpath:com/best/dao/*.xml #配置包,這里同樣可以配置多個(gè), type-aliases-package: com.best.daoserver: port: 8080
10、配置好之后就開始配置service以及impl了,首先創(chuàng)建一個(gè)service和service/impl包,并在service下創(chuàng)建一個(gè)IUserService接口,并在impl下來(lái)實(shí)現(xiàn)該接口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:用戶接口實(shí)現(xiàn)類 */@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、在創(chuàng)建好service后,需要?jiǎng)?chuàng)建一個(gè)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訪問(wèn)時(shí)必須加上/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、創(chuàng)建好之后,最后創(chuàng)建一個(gè)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: */@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}
13、此時(shí)啟動(dòng)會(huì)提示無(wú)法找到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、出現(xiàn)此問(wèn)題是由于springboot在啟動(dòng)時(shí),在UserServiceImpl中又使用到了UserMapper的自動(dòng)注入,而springboot啟動(dòng)時(shí)就沒(méi)有掃描到UserMapper,更不可能將它作為bean注入到IOC容器中,因此就無(wú)法注入了,那么需要再入口的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'})//如果有多個(gè)可以使用{'com.best.dao','com.best.sss.dao'}public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}
15、此時(shí)即可正常啟動(dòng),如下圖所示:
16、此時(shí)在瀏覽器中輸入:http://localhost:8080/best/getUser即可,如下圖所示:
17、以上只是搭建一個(gè)可以正常通過(guò)url訪問(wèn),那么現(xiàn)在來(lái)訪問(wèn)數(shù)據(jù)庫(kù)中的記錄,在訪問(wèn)之前先創(chuàng)建一些封裝類,比如將實(shí)體類封裝成json、返回狀態(tài)碼等,先創(chuàng)建一個(gè)common包,在該包下創(chuàng)建entity、enums、utils等包,如下圖所示:
18、然后在enums中創(chuàng)建一個(gè)ResultCode枚舉類,此來(lái)定義了一些返回狀態(tài)碼信息,如下代碼所示:
package com.best.common.enums; /** * @author:sunxj * @date:2020-11-10 22:08:11 * @description:返回碼定義 */public enum ResultCode { //成功 SUCCESS(200,'成功'), //默認(rèn)失敗 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會(huì)默認(rèn)取出enum中的值對(duì)象列表,每個(gè)值都是一個(gè)ResultCode對(duì)象 if (ele.getCode().equals(code)) { return ele.getMessage(); } } return null; }}
19、然后在entity中新建一個(gè)JsonResult類,用來(lái)封裝統(tǒng)一返回的實(shí)體類,如:
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:統(tǒng)一返回的實(shí)體類 */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中新建一個(gè)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返回構(gòu)造工具 */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、創(chuàng)建好這些之后,那么就可以開始在controller中進(jìn)行構(gòu)造了,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訪問(wèn)時(shí)必須加上/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、重新啟動(dòng)springboot,然后輸入:http://localhost:8080/best/getUser,此時(shí)頁(yè)面會(huì)出現(xiàn)如下錯(cuò)誤:
23、后臺(tái)顯示如下錯(cuò)誤:
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訪問(wèn)時(shí)必須加上/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、這意思就是沒(méi)有找到UserMapper接口文件中的selectAll方法,也就是沒(méi)有和*Mapper.xml匹配成功,這是由于我們將.xml文件和java文件放在了java目錄,而通過(guò)maven來(lái)管理,它不會(huì)將java文件中的xml文件打包進(jìn)去的,因此需要再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 causeorg.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、然后在此重新運(yùn)行,重新輸入網(wǎng)址即可,如下圖所示:
25、到此即可搭建完畢,使用此方法可以搭建一個(gè)前后端完全分離通過(guò)json通信的springboot后臺(tái)。
到此這篇關(guān)于spring boot+mybatis搭建一個(gè)后端restfull服務(wù)的文章就介紹到這了,更多相關(guān)spring boot+mybatis搭建restfull服務(wù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP循環(huán)與分支知識(shí)點(diǎn)梳理2. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)3. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題4. 解析原生JS getComputedStyle5. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. 前端從瀏覽器的渲染到性能優(yōu)化9. 利用CSS3新特性創(chuàng)建透明邊框三角10. css代碼優(yōu)化的12個(gè)技巧
