javascript
Spring boot web(2):web综合开发
1 web開發(fā)
Spring boot web 開發(fā)非常簡單,其中包括常用的 json輸出、filters、property、log等
1.1 json接口開發(fā)
在以前的Spring 開發(fā)我么提供json 的做法:
而在Spring boot中,只需要添加 @RestController 即可。默認(rèn)類中的方法都會以json格式返回
@RestController public class HelloWorldController {@RequestMapping("/getUser")public User getUser() {User user=new User();user.setUserName("小明");user.setPassWord("xxxx");return user;} }在使用頁面開發(fā)使用 @Controller 即可
1.2 自定義Filter
我們常常在項(xiàng)目中會使用filters用于調(diào)用日志、排除有xss威脅的字符、執(zhí)行權(quán)限驗(yàn)證等等。Spring Boot自動添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter ,并且我們可以自定義Filter。
兩個(gè)步驟:
1.3 log配置
配置輸出的地址和輸出級別
logging.path=/user/local/log logging.level.com.favorites=DEBUG logging.level.org.springframework.web=INFO logging.level.org.hibernate=ERROR1.4 數(shù)據(jù)庫操作
重點(diǎn)講mysql、Spring data jpa:jpa是利用Hibernate生成各種自動化的 sql,如果只是簡單的增刪改查,基本上不用手寫,Spring 內(nèi)部已經(jīng)封裝實(shí)現(xiàn)了。
hibernate.hbm2ddl.auto 參數(shù)的作用是:自動創(chuàng)建|更新|驗(yàn)證數(shù)據(jù)庫表結(jié)構(gòu),有四個(gè)值:
- create:每次加載hibernate時(shí)都會刪除上一次的生成的表,然后根據(jù)你的model類再重新來生成新表,哪怕兩次沒有任何改變也要這樣執(zhí)行,這就是導(dǎo)致數(shù)據(jù)庫表數(shù)據(jù)丟失的一個(gè)重要原因。
- create-drop: 每次加載hibernate 時(shí)根據(jù)model 類生成表,但是sessionFactory 一關(guān)閉,表就刪除。
- update:最常用的屬性,第一次加載hibernate 時(shí)根據(jù)model類會自動建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫),以后加載hibernate時(shí)會根據(jù)model類自動更新表結(jié)構(gòu),即使表結(jié)構(gòu)該表了但表中的行仍然存在不會刪除以前的行。 要注意的是當(dāng)?shù)谝淮尾渴鸬椒?wù)器后,表結(jié)構(gòu)不會立馬被建立起來,要等到應(yīng)用第一次運(yùn)行起來后才會。
- validate:每次加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),只會和數(shù)據(jù)庫中的表進(jìn)行比較,不會創(chuàng)建新表,但會插入新值。
dialect 主要是指定生成表名的存儲引擎為InneoDB。
1.5 添加實(shí)體類和Dao
這里后續(xù)插入常用實(shí)體類的注解dao只要繼承jpaRepository 類就可以,幾乎可以不用寫方法
1.6 測試
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class UserRepositoryTests { @Autowiredprivate UserRepository userRepository; @Testpublic void test() throws Exception {Date date = new Date();DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String formattedDate = dateFormat.format(date);userRepository.save(new User("aa1", "aa@126.com", "aa", "aa123456",formattedDate));userRepository.save(new User("bb2", "bb@126.com", "bb", "bb123456",formattedDate));userRepository.save(new User("cc3", "cc@126.com", "cc", "cc123456",formattedDate));Assert.assertEquals(9, userRepository.findAll().size());Assert.assertEquals("bb", userRepository.findByUserNameOrEmail("bb", "cc@126.com").getNickName());userRepository.delete(userRepository.findByUserName("aa1"));} }1.7 thymeleaf介紹
thymeleaf是一款用于渲染XML/XHTML/HTML5內(nèi)容的模板引擎。 它使用了自然的模板技術(shù)。這意味著Thymeleaf的模板語言不會破壞文檔結(jié)構(gòu),模板依舊是有效的XML文檔。 在運(yùn)行期替換掉靜態(tài)值。
velocity,FreMaker,beetle也是模板引擎。
下面的代碼示例分別使用Velocity、FreeMarker、Thymeleaf打印一條消息:
注意:由于Thymeleaf使用了XML DOM解析器,因此它并不適合處理大規(guī)模的XML文件。
這里是Thymeleaf使用教程:
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
- 頁面即原型(Thymeleaf的好處)
在傳統(tǒng)java web開發(fā)過程中,前端工程師和后端工程師一樣,需要一套完整的開發(fā)環(huán)境,然后各類java IDE中修改模板、靜態(tài)資源。
但實(shí)際上前端工程師的職責(zé)更多關(guān)注頁面本身而非后端。使用jsp,Velocity等傳統(tǒng)的java模板引擎很難做到這一點(diǎn),而Thymeleaf通過屬性進(jìn)行模板渲染不會引起新的瀏覽器不能識別的標(biāo)簽,可以直接作為HTML文件在瀏覽器中打開。
1.8 WebJars
WebJars是一個(gè)很神奇的東西,可以讓大家以jar包的形式來使用前端的各種框架、組件。
什么是WebJars
什么是WebJars?WebJars是將客戶端(瀏覽器)資源(JavaScript,Css等)打成jar包文件,以對資源進(jìn)行統(tǒng)一依賴管理。WebJars的jar包部署在Maven中央倉庫上。
為什么使用
我們在開發(fā)Java web項(xiàng)目的時(shí)候會使用像Maven,Gradle等構(gòu)建工具以實(shí)現(xiàn)對jar包版本依賴管理,以及項(xiàng)目的自動化管理,但是對于JavaScript,Css等前端資源包,我們只能采用拷貝到webapp下的方式,這樣做就無法對這些資源進(jìn)行依賴管理。那么WebJars就提供給我們這些前端資源的jar包形勢,我們就可以進(jìn)行依賴管理。
如何使用
1、 WebJars主官網(wǎng) 查找對于的組件,比如Vuejs
2、頁面引入
<link th:href="@{/webjars/bootstrap/3.3.6/dist/css/bootstrap.css}" rel="stylesheet"></link>這樣就可以正常使用了。
1.9 Gradle 構(gòu)建工具
類似maven,Spring項(xiàng)目建議使用Gradle進(jìn)行構(gòu)建項(xiàng)目,相比maven來講 Gradle更簡潔,而且Gradle更適合大型項(xiàng)目的構(gòu)建。
buildscript {repositories {maven { url "http://repo.spring.io/libs-snapshot" }mavenLocal()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")} }apply plugin: 'java' //添加 Java 插件, 表明這是一個(gè) Java 項(xiàng)目 apply plugin: 'spring-boot' //添加 Spring-boot支持 apply plugin: 'war' //添加 War 插件, 可以導(dǎo)出 War 包 apply plugin: 'eclipse' //添加 Eclipse 插件, 添加 Eclipse IDE 支持, Intellij Idea 為 "idea"war {baseName = 'favorites'version = '0.1.0' }sourceCompatibility = 1.7 //最低兼容版本 JDK1.7 targetCompatibility = 1.7 //目標(biāo)兼容版本 JDK1.7repositories { // Maven 倉庫mavenLocal() //使用本地倉庫mavenCentral() //使用中央倉庫maven { url "http://repo.spring.io/libs-snapshot" } //使用遠(yuǎn)程倉庫 }dependencies { // 各種 依賴的jar包c(diǎn)ompile("org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE")compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'compile("org.springframework.boot:spring-boot-devtools:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-test:1.3.6.RELEASE")compile 'org.webjars.bower:bootstrap:3.3.6'compile 'org.webjars.bower:jquery:2.2.4'compile("org.webjars:vue:1.0.24")compile 'org.webjars.bower:vue-resource:0.7.0'}bootRun {addResources = true }總結(jié)
以上是生活随笔為你收集整理的Spring boot web(2):web综合开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑板cf怎么刷枪(cf怎么弄枪)
- 下一篇: Spring boot(3):Sprin