text: (window.webpackJsonp=window.webpackJsonp||[]).push([[106],{678:function(t,a,e){"use strict";e.r(a);var s=e(9),n=Object(s.a)({},(function(){var t=this,a=t.$createElement,e=t._self._c||a;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("p",[t._v("一些 Spring Boot 小技巧、小知识点")]),t._v(" "),e("h2",{attrs:{id:"初始化数据"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#初始化数据"}},[t._v("#")]),t._v(" 初始化数据")]),t._v(" "),e("p",[t._v("我们在做测试的时候经常需要初始化导入一些数据，如何来处理呢？会有两种选择，一种是使用 Jpa，另外一种是 Spring JDBC 。两种方式各有区别下面来详细介绍。")]),t._v(" "),e("p",[e("strong",[t._v("使用 Jpa")])]),t._v(" "),e("p",[t._v("在使用"),e("code",[t._v("spring boot jpa")]),t._v("的情况下设置"),e("code",[t._v("spring.jpa.hibernate.ddl-auto")]),t._v("的属性设置为 "),e("code",[t._v("create")]),t._v(" or "),e("code",[t._v("create-drop")]),t._v("的时候，Spring Boot 启动时默认会扫描 classpath 下面（项目中一般是 resources 目录）是否有"),e("code",[t._v("import.sql")]),t._v("，如果有机会执行"),e("code",[t._v("import.sql")]),t._v("脚本。")]),t._v(" "),e("p",[t._v("**使用 Spring JDBC **")]),t._v(" "),e("p",[t._v("使用 Spring JDBC 需要在配置文件中添加以下配置")]),t._v(" "),e("div",{staticClass:"language-xml extra-class"},[e("pre",{pre:!0,attrs:{class:"language-xml"}},[e("code",[t._v("spring:\n    datasource:\n      schema: database/data.sql\n      sql-script-encoding: utf-8\n    jpa:\n      hibernate:\n        ddl-auto: none\n")])])]),e("ul",[e("li",[t._v("schema ：设置脚本的路径")]),t._v(" "),e("li",[t._v("sql-script-encoding：设置脚本的编码")])]),t._v(" "),e("p",[t._v("Spring Boot 项目启动的时候会自动执行脚本。")]),t._v(" "),e("p",[e("strong",[t._v("ddl-auto 四个值的解释")])]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("create： 每次加载hibernate时都会删除上一次的生成的表，然后根据你的model类再重新来生成新表，哪怕两次没有任何改变也要这样执行，这就是导致数据库表数据丢失的一个重要原因。")]),t._v(" "),e("li",[t._v("create-drop ：每次加载hibernate时根据model类生成表，但是sessionFactory一关闭,表就自动删除。")]),t._v(" "),e("li",[t._v("update：最常用的属性，第一次加载hibernate时根据model类会自动建立起表的结构（前提是先建立好数据库），以后加载hibernate时根据 model类自动更新表结构，即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后，表结构是不会被马上建立起来的，是要等 应用第一次运行起来后才会。")]),t._v(" "),e("li",[t._v("validate ：每次加载hibernate时，验证创建数据库表结构，只会和数据库中的表进行比较，不会创建新表，但是会插入新值。\n5、 none  : 什么都不做。")])])]),t._v(" "),e("p",[e("strong",[t._v("不同点")])]),t._v(" "),e("p",[t._v("第一种方式启动的时候 Jpa 会自动创建表，import.sql 只负责创建表单后的初始化数据。第二种方式启动的时候不会创建表，需要在初始化脚本中判断表是否存在，再初始化脚本的步骤。")]),t._v(" "),e("blockquote",[e("p",[t._v("在生产中，这两种模式都建议慎用！")])]),t._v(" "),e("h2",{attrs:{id:"thymeleaf-设置不校验-html-标签"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#thymeleaf-设置不校验-html-标签"}},[t._v("#")]),t._v(" Thymeleaf 设置不校验 html 标签")]),t._v(" "),e("p",[t._v("默认配置下，Thymeleaf 对 .html 的内容要求很严格，比如"),e("code",[t._v('<meta charset="UTF-8" />')]),t._v("，如果少封闭符号"),e("code",[t._v("/")]),t._v("，就会报错而转到错误页。也比如你在使用 Vue.js 这样的库，然后有"),e("code",[t._v("<div v-cloak></div>")]),t._v("这样的 html 代码，也会被 Thymeleaf 认为不符合要求而抛出错误。")]),t._v(" "),e("p",[t._v("通过设置 Thymeleaf 模板可以解决这个问题，下面是具体的配置:")]),t._v(" "),e("div",{staticClass:"language-properties extra-class"},[e("pre",{pre:!0,attrs:{class:"language-properties"}},[e("code",[e("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("spring.thymeleaf.cache")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),e("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("false")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("spring.thymeleaf.mode")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),e("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("LEGACYHTML5")]),t._v("\n")])])]),e("p",[t._v("LEGACYHTML5 需要搭配一个额外的库 NekoHTML 才可用，项目中使用的构建工具是 Maven 添加如下的依赖即可完成:")]),t._v(" "),e("div",{staticClass:"language-xml extra-class"},[e("pre",{pre:!0,attrs:{class:"language-xml"}},[e("code",[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("dependency")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("groupId")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("net.sourceforge.nekohtml"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("</")]),t._v("groupId")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("artifactId")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("nekohtml"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("</")]),t._v("artifactId")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("version")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("1.9.22"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("</")]),t._v("version")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token tag"}},[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("</")]),t._v("dependency")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n")])])]),e("blockquote",[e("p",[t._v("文章内容已经升级到 Spring Boot 2.x")])]),t._v(" "),e("p",[e("strong",[e("a",{attrs:{href:"https://github.com/ityouknow/spring-boot-examples",target:"_blank",rel:"noopener noreferrer"}},[t._v("示例代码-github"),e("OutboundLink")],1)])]),t._v(" "),e("p",[e("strong",[e("a",{attrs:{href:"https://gitee.com/ityouknow/spring-boot-examples",target:"_blank",rel:"noopener noreferrer"}},[t._v("示例代码-码云"),e("OutboundLink")],1)])]),t._v(" "),e("p",[t._v("参考：")]),t._v(" "),e("ul",[e("li",[e("a",{attrs:{href:"https://blog.csdn.net/yalishadaa/article/details/60768811",target:"_blank",rel:"noopener noreferrer"}},[t._v("thymeleaf模板对没有结束符的HTML5标签解析出错的解决办法"),e("OutboundLink")],1)]),t._v(" "),e("li",[e("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("howto-database-initialization"),e("OutboundLink")],1)])])])}),[],!1,null,null,null);a.default=n.exports}}]);