Spring boot??????????????????Spring boot??????MySql,Mybatis???PageHelper??????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot??????????????????Spring boot??????MySql,Mybatis???PageHelper??????相关的知识,希望对你有一定的参考价值。
?????????engine ?????? eva long service settings img source pid
?????????????????????????????????????????????????????????Spring boot????????????????????????????????????????????????Spring boot?????????????????????Spring boot???????????????????????????spring boot??????mybatis???pagehelper
??????mybatis???pagehelper??????????????????????????????????????????????????????????????????????????????????????????????????????????????????
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???????????????
***********************************?????????????????????Spring boot???mybatis???jar?????????????????????sping boot2+??????????????????eg???spring boot2+????????????pagehelper1.2???????????????
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???????????????
???1???.??????????????????????????????????????????????????????
????????????????????????@MapperSca?????????????????????2??????????????????????????????tk.mybatis.spring.annotation.MapperScan???org.mybatis.spring.annotation.MapperScan;
??????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???
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????????????????????????
?????????????????????????????????????????????????????????!
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??????????????????
??????????????????????????????????????????
以上是关于Spring boot??????????????????Spring boot??????MySql,Mybatis???PageHelper??????的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?
《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》