上一篇文章,寫了如何搭建一個簡單的Spring boot項目,本篇是接着上一篇文章寫得:Spring boot入門:快速搭建Spring boot項目(一),主要是spring boot集成mybatis和pagehelpercss
關於mybatis和pagehelper的介紹,能夠自行博客,網上不少相似的博客,這裏,我直接上代碼和項目搭建教程。html
1.配置文件:在配置文件application.yml中配置MySql數據庫鏈接池和Mybatis掃描包以及PageHelper分頁插件前端
1 mybatis: 2 mapper-locations: classpath:mapper/*.xml 3 type-aliases-package: com.tswc.edu.entity 4 5 mapper: 6 mappers: 7 - com.tswc.edu.util.MyMapper 8 not-empty: false 9 identity: MYSQL 10 #identity: SQLSERVER 11 12 pagehelper: 13 helperDialect: MYSQL 14 reasonable: true 15 supportMethodsArguments: true 16 params: count=countSql 17 18 spring: 19 datasource: 20 type: com.alibaba.druid.pool.DruidDataSource 21 driverClassName: com.mysql.jdbc.Driver 22 url: jdbc:mysql://127.0.0.1:3306/edu?useUnicode=true&characterEncoding=utf8 23 username: root 24 password: 123456 25 initialSize: 1 26 minIdle: 3 27 maxActive: 20 28 # 配置獲取鏈接等待超時的時間 29 maxWait: 60000 30 # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 31 timeBetweenEvictionRunsMillis: 60000 32 # 配置一個鏈接在池中最小生存的時間,單位是毫秒 33 minEvictableIdleTimeMillis: 30000 34 validationQuery: select 'x' 35 testWhileIdle: true 36 testOnBorrow: false 37 testOnReturn: false 38 # 打開PSCache,而且指定每一個鏈接上PSCache的大小 39 poolPreparedStatements: true 40 maxPoolPreparedStatementPerConnectionSize: 20 41 # 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆 42 filters: stat,wall,slf4j 43 # 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄 44 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 45 # 合併多個DruidDataSource的監控數據 46 #useGlobalDataSourceStat: true
同時,須要在pom.xml中添加相應的jar包依賴,若是存在jar包沒法下載,能夠手動下載,而後放到相應的jar包文件夾中java
***********************************這裏面須要注意Spring boot和mybatis的jar包之間的版本,sping boot2+要求高版本,eg:spring boot2+不能識別pagehelper1.2如下的版本mysql
1 <!--mybatis --> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <version>5.1.6</version> 6 </dependency> 7 <dependency> 8 <groupId>org.mybatis</groupId> 9 <artifactId>mybatis</artifactId> 10 <version>3.4.4</version> 11 </dependency> 12 13 <dependency> 14 <groupId>org.mybatis.spring.boot</groupId> 15 <artifactId>mybatis-spring-boot-starter</artifactId> 16 <version>1.1.1</version> 17 </dependency> 18 <!--druid --> 19 <dependency> 20 <groupId>com.alibaba</groupId> 21 <artifactId>druid</artifactId> 22 <version>1.0.28</version> 23 </dependency> 24 25 <!--mapper--> 26 <dependency> 27 <groupId>tk.mybatis</groupId> 28 <artifactId>mapper-spring-boot-starter</artifactId> 29 <version>2.1.0</version> 30 </dependency> 31 <!--pagehelper,Spring boot2.0+只能設備pagehelper1.2+--> 32 <dependency> 33 <groupId>com.github.pagehelper</groupId> 34 <artifactId>pagehelper-spring-boot-starter</artifactId> 35 <version>1.2.5</version> 36 </dependency>
2:代碼部分git
(1).更新入口主文件,主要增長了一些註解,github
此處須要注意@MapperSca的引用,這裏有2個可引用的包,分別是tk.mybatis.spring.annotation.MapperScan和org.mybatis.spring.annotation.MapperScan;web
tk.mybatis.spring.annotation.MapperScan:爲mubatis帶的mapper掃描文件,org.mybatis.spring.annotation.MapperScan爲spring自帶的掃描文件,在低版本的mybatis上,2個包能夠隨意使用,可是Spring boot2+後,增長了一些特性(建議閱讀spring boot2+的底層代碼,加深理解),致使只能使用高版本的mtbatis,同時spring自帶的MapperScan也沒法使用,因此使用sping boot2+後,須要引用高版本的mybatis,同時這裏只能使用mybatis的MapperScan的掃描包;spring在後期更新中,應該也會使其可以用於boot中spring
1 @ServletComponentScan 2 @MapperScan(basePackages = "com.tswc.edu.mapper") 3 @SpringBootApplication 4 public class EduApplication { 5 6 public static void main(String[] args) { 7 SpringApplication.run(EduApplication.class, args); 8 } 9 10 }
若是在項目正常,啓動,可是在使用mybatis時,報此類錯誤:sql
後臺錯誤:沒法獲取實體類對應的表名!
1 2018-12-14 20:09:25.967 ERROR 29024 --- [nio-8085-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is tk.mybatis.mapper.MapperException: 沒法獲取實體類com.tswc.edu.entity.Index對應的表名!] with root caus 2 3 tk.mybatis.mapper.MapperException: 沒法獲取實體類com.tswc.edu.entity.Index對應的表名! 4 at tk.mybatis.mapper.mapperhelper.EntityHelper.getEntityTable(EntityHelper.java:69) ~[mapper-core-1.1.0.jar:na] 5 at tk.mybatis.mapper.entity.Example.<init>(Example.java:103) ~[mapper-core-1.1.0.jar:na] 6 at tk.mybatis.mapper.entity.Example.<init>(Example.java:88) ~[mapper-core-1.1.0.jar:na] 7 at tk.mybatis.mapper.entity.Example.<init>(Example.java:78) ~[mapper-core-1.1.0.jar:na] 8 at com.tswc.edu.service.IndexService.getList(IndexService.java:33) ~[classes/:na] 9 at com.tswc.edu.controller.IndexController.list(IndexController.java:35) ~[classes/:na] 10 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151] 11 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151] 12 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151] 13 at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151] 14 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 15 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 16 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 17 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 18 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 19 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 20 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 21 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 22 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 23 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 24 at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 25 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 26 at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 27 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 28 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 29 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13] 30 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 31 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 32 at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 33 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 34 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 35 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 36 at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 37 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 38 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 39 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 40 at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 42 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 43 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 44 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 45 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 46 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 47 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 48 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13] 49 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar:9.0.13] 50 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar:9.0.13] 51 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar:9.0.13] 52 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar:9.0.13] 53 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar:9.0.13] 54 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar:9.0.13] 55 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar:9.0.13] 56 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar:9.0.13] 57 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13] 58 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13] 59 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13] 60 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151] 61 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151] 62 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13] 63 at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
前端錯誤是500
1 Whitelabel Error Page 2 This application has no explicit mapping for /error, so you are seeing this as a fallback. 3 4 Fri Dec 14 20:09:25 CST 2018 5 There was an unexpected error (type=Internal Server Error, status=500). 6 ???????com.tswc.edu.entity.Index?????!
那麼,就是Mapper掃描包使用有誤
(2).通用接口mapper:此接口不能被掃描到,不然會報錯,能夠在application.yml中,排除此包的掃描
1 public interface MyMapper<T> extends Mapper<T> { 2 //TODO 3 //FIXME 特別注意,該接口不能被掃描到,不然會出錯 4 }
(3)後臺代碼,控制村層>服務層>接口
1 @Controller 2 public class IndexController { 3 @Autowired 4 private IndexService indexService; 5 6 @RequestMapping(value = "/index") 7 public void list(ModelMap modelMap) { 8 List<Index> list = indexService.getList(); 9 modelMap.put("list", list); 10 } 11 }
1 @Service 2 public class IndexService { 3 @Autowired 4 private IndexMapper indexMapper; 5 6 public List<Index> getList() { 7 Example example = new Example(Index.class); 8 example.setTableName("index_test"); 9 Criteria criteria = example.createCriteria(); 10 criteria.andEqualTo("age", 20); 11 return indexMapper.selectByExample(example); 12 } 13 }
1 public interface IndexMapper extends MyMapper<Index> { 2 }
1 @Table(name = "index_test") 2 public class Index implements Serializable, IDynamicTableName { 3 private static final long serialVersionUID = -8709793124943394842L; 4 5 @Transient 6 private String dynamicTableName; 7 private String name; 8 private Integer age; 9 10 public String getName() { 11 return name; 12 } 13 14 public void setName(String name) { 15 this.name = name; 16 } 17 18 public Integer getAge() { 19 return age; 20 } 21 22 public void setAge(Integer age) { 23 this.age = age; 24 } 25 26 @Override 27 public String getDynamicTableName() { 28 return null; 29 } 30 }
最後一個類爲實體類,建議在創建實體類的時候,統一加上@Table的註解,方便後期Mybatis的使用,同時也提升了代碼的規範性。固然,若是咱們使用Mybatis的代碼生成器,那麼均會自動加上註解(代碼生成器在後期的博客中會講到)
(4).前端ftl代碼
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <title>Spring Boot Demo - FreeMarker</title> 5 <link href="/css/index.css" rel="stylesheet" /> 6 </head> 7 <body> 8 <table> 9 <#list list as user> 10 <tr> 11 <td>第${user_index+1}個用戶</td> 12 <td>用戶名:${user.name}</td> 13 <td>年 齡: ${user.age}</td> 14 </tr> 15 </#list> 16 </table> 17 </body> 18 </html>
(5)添加mybatis的空配置文件,因爲spring boot中,數據庫是經過配置文件的形式,因此此文件是空的,可是沒有此文件,mybatis就沒法經過;固然,也能夠經過在此文件做用配置數據庫鏈接池的方式鏈接數據(已過期)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL MAP Config 3.1//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <settings> 6 <setting name="callSettersOnNulls" value="true"/> 7 </settings> 8 </configuration>
(6)數據庫以下:添加了幾條測試數據:
(7).啓動項目,並訪問http://localhost:8085/index,結果以下:
最終,項目代碼的機構,以下: