Git记录-Please commit your changes or stash them before you merge 错误重现 情况是想要将github上的远程仓库的修改更新到本地
解决办法 根据提示,可以commit再meger。这里我选择直接使用 git pull ,之前我是使用的 git fetch 是将远程主机的最新内容拉到本地检查了以后决定是否合并到工作本机分支中。使用git pull 可以同时操作了我之前的两个步骤 git fetch+git meger ,则是将远程主机的最新内容拉下来后直接合并。在这之前将 git 重置一下 , git reset --hard。
通过虚拟路径访问本地图片 情况说明: 之前存入在数据库中的图片地址是本地的相对路径,需要实现的是在html页面中img标签src中直接写上本地图片资源的相对路径,得以显示。
1. 错误重现 数据库:
没有做任何设置之前:
2. 解决步骤 第一步: 在Tomcat目录下找到conf文件夹,打开server.xml D:\apache-tomcat-9.0.22\conf\server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt"/> <!-- eclipse --> <Context docBase="shopping" path="/shopping" reloadble="true" source=" org.eclipse.jst.jee.server:o2o"> </Context> <!-- 在此处添加以下内容 我的图片位置为 : D:\projectdev\img\upload\item\headtitle\XXXXX.jpg --> <Context path="/upload" docBase="D:\projectdev\img\upload" crossContext="true" reloadable="true" debug="0"> </Context> </Host> 第二步: 开发工具为 Idea 快捷键: shift+alt+F10 , 进入Run小窗口
进入Tomcat设置界面:
选择自己的当前项目的Tomcat:
设置路径:
重启tomcat:
访问成功:
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 容器 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 错误处理 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.