Spring Boot JdbcTemplate: (1) 在pom.xml加入jdbcTemplate的依赖; (2) 编写DemoDao类,声明为:@Repository,引入JdbcTemplate (3) 编写DemoService类,引入DemoDao进行使用 (4) 编写Demo2Controller进行简单测试。 在pom.xml加入jdbcTemplate的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 如果在JPA已经加入的话,则可以不用引入以上的配置。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> 那么只需要在需要使用的类中加入: @Resource private JdbcTemplate jdbcTemplate;
编写DemoDao类: 声明为:@Repository,引入JdbcTemplate public Demo getById(long id){ String sql = "select *from Demo where id=?"; RowMapper<Demo> rowMapper = new BeanPropertyRowMapper<Demo>(Demo.class); return jdbcTemplate.queryForObject(sql, rowMapper,id); } 编写DemoService类,引入DemoDao进行使用: @Resource private DemoDao demoDao; public void save(Demo demo){ demoDao.save(demo); } 编写Demo2Controller进行简单测试: @Resource private DemoService demoService; @RequestMapping("/getById") public Demo getById(long id){ return demoService.getById(id); }
全局异常捕捉: 在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice, 在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下: @ControllerAdvice public class GlobalDefaultExceptionHandler{ @ExceptionHandler(value = Exception.class) public void defaultErrorHandler(HttpServletRequest req, Exception e) { }
配置server信息: 1/修改端口号 2/修改context-path 3/其它配置说明 Spring boot 默认端口是8080,如果想要进行更改的话,只需要修改applicatoin.properties文件,在配置文件中加入: server.port=8081 applicatoin.properties里面有数据库的配置信息,jpa的配置信息,server的配置信息。 配置context-path: 在application.properties进行配置: server.context-path=/spring-boot 访问地址就是http://ip:port/spring-boot/hello可以访问
其它server配置: #server.port=8080 #server.address= # bind to a specific NIC #server.session-timeout= # session timeout in seconds #the context path, defaults to ‘/‘ #server.context-path=/spring-boot #server.servlet-path= # the servlet path, defaults to ‘/‘ #server.tomcat.access-log-pattern= # log pattern of the access log #server.tomcat.access-log-enabled=false # is access logging enabled #server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers #server.tomcat.remote-ip-header=x-forwarded-for #server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp) #server.tomcat.background-processor-delay=30; # in seconds #server.tomcat.max-threads = 0 # number of threads in protocol handler #server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
spring boot使用thymeleaf: (1)在pom.xml中引入thymeleaf; (2)如何关闭thymeleaf缓存 (3)编写模板文件.html (4)编写访问模板文件controller 在pom.xml加入thymeleaf的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 如何关闭thymeleaf缓存: spring.thymeleaf.cache=false
编写模板文件.html 编写模板文件src/main/resouces/templates/hello.html: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1 th:inline="text">Hello.v.2</h1> <p th:text="${hello}"></p> </body> </html>
编写访问模板文件controller @Controller public class TemplateController { /** * 返回html模板. */ @RequestMapping("/helloHtml") public String helloHtml(Map<String,Object> map){ map.put("hello","from TemplateController.helloHtml"); return "/helloHtml"; } }
Spring Boot 使用freemarker: (1)在pom.xml中引入freemarker; (2)如何关闭freemarker缓存 (3)编写模板文件.ftl (4)编写访问文件的controller
在pom.xml中引入freemarker: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 如何关闭freemarker缓存: ######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.suffix=.ftl #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved
编写模板文件.ftl <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1>Hello.v.2</h1> <p>${hello}</p> </body> </html> 编写访问文件的controller @RequestMapping("/helloFtl") public String helloFtl(Map<String,Object> map){ map.put("hello","from TemplateController.helloFtl"); return "/helloFtl"; }