SpringBoot

SpringBoot数据访问

SpringBoot 数据库访问 JDBC访问数据库 整合Druid数据源 整合MyBatis 整合JPA

SpringBoot访问数据库 springboot 默认支持很多数据源 有:Hikari,Tomcat, Dbcp2,Generic。 springboot2.X默认数据源为:Hikari。下面还会讲到切换数据源到阿里的Druid, 同时和springboot桥接进行数据库访问的方式也有很多:JDBC,Mybatis,JPA 源码:DataSourceAutoConfiguration –>PooledDataSourceConfiguration可以看到数据源的配置 @Configuration @Conditional({DataSourceAutoConfiguration.PooledDataSourceCondition.class}) @ConditionalOnMissingBean({DataSource.class, XADataSource.class}) @Import({Hikari.class, Tomcat.class, Dbcp2.class, Generic.class, DataSourceJmxConfiguration.class}) protected static class PooledDataSourceConfiguration { protected PooledDataSourceConfiguration() { } } JDBC访问数据库 1. 准备工作 pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> application.yml spring: datasource: username: root password: url: jdbc:mysql://localhost:3306/jdbc driver-class-name: com.mysql.jdbc.Driver 2. 创建表 springboot创建表相关源码, 通过DataSourceInitializer拿到数据源,可以创建表结构或者表数据 //创建表结构 public boolean createSchema() { List<Resource> scripts = this.

SpringBoot与web开发(六)----SpringBoot容器

SpringBoot容器

SpringBoot 容器 SpringBoot的容器默认使用的Tomcat, 1.如何修改Servlet容器的相关配置? 修改Servlet的配置 (org.springframework.boot.autoconfigure.web.ServerProperties) server.servlet.context-path=/crud server.port=8081 2.如何注册自己的Servlet,Filter,Listener SpringBoot默认的是以jar包的方式启动嵌入Serlvet容器,没有Web.xml。 注册三大组件的方式 ServletRegistrationBean FilterRegistrationBean ServletListenerRegistrationBean /** * @program: spring-boot-restful-crud * @description: servlet配置类 * @author: YuanChangYue * @create: 2019-08-19 19:20 */ @Configuration public class MyServletConfig { @Bean public ServletRegistrationBean myServlet() { return new ServletRegistrationBean<>(new MyServlet(), "/myServlet"); } @Bean public FilterRegistrationBean myFilter() { FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new MyFilter()); registrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet")); return registrationBean; } @Bean public ServletListenerRegistrationBean myListen() { return new ServletListenerRegistrationBean(new MyListen()); } } /** * @program: spring-boot-restful-crud * @description: 自定义Listen * @author: YuanChangYue * @create: 2019-08-19 19:42 */ public class MyListen implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.

SpringBoot与web开发(五) ---- SpringBoot 错误处理机制

SpringBoot 错误处理机制

SpringBoot 错误处理 SpringBoot 默认错误机制,返回一个错误页面和 json数据 浏览器 其它客户端 1. 原理 源码 ErrorMvcAutoConfiguration 其中有几个重要的组件: DefaultErrorAttributes : 共享信息 timestamp 时间 status 状态码 error 错误提示 exception 异常对象 message 异常信息 errors JSR303 数据校验的信息 DefaultErrorViewResolver @Override public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) { ModelAndView modelAndView = resolve(String.valueOf(status.value()), model); if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { modelAndView = resolve(SERIES_VIEWS.get(status.series()), model); } return modelAndView; } private ModelAndView resolve(String viewName, Map<String, Object> model) { String errorViewName = "error/" + viewName; //使用模板引擎 TemplateAvailabilityProvider provider = this.

SpringBoot与web开发(四)-----登录拦截

拦截器登录

准备工作 页面downloadboostrap的官方实例页面,方便练习 资源来自:https://getbootstrap.com/docs/4.3/examples/ 资源的目录如下: 1.controller层 package com.changyue.springbootresetfucrud.controller; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession; import java.util.Map; /** * @program: spring-boot-restful-crud * @description: 用户控制器 * @author: YuanChangYue * @create: 2019-08-13 17:59 */ @Controller public class LoginController { /** * 用户登录 * * @param username 用户名 默认测试 123 * @param password 密码 默认测试 123 * @param map map * @param httpSession session * @return 页面名称 */ @PostMapping("/user/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String, Object> map, HttpSession httpSession) { if ("123".

SpringBoot与Web开发(三)-----Spring MVC

Spring Boot 中 Spring MVC 自动配置 ,Spring MVC的拓展 ,全面接管SpringMVC

Spring Boot 与 Spring MVC 根据SpringBoot 官方文档中的资料 ,可以看出SpringBoot和SpringMVC的关系。 本章节提到的官文文档地址如下: <https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration 1. Spring Boot 中Spring MVC 自动配置 官方文档中的原文如下: Spring Boot provides auto-configuration for Spring MVC that works well with most applications. The auto-configuration adds the following features on top of Spring’s defaults: Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans. 自动配置viewResolver (视图解析器:根据方法返回的值获得视图对象 ) ContentNegotiatingViewResolver : 组合所有的试图解析器 自定义:==可以自己给容器中添加视图解析器 ,自动为我们组合起来== Support for serving static resources, including support for WebJars (covered later in this document)).