日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

(二)SpringBoot功能

發(fā)布時(shí)間:2023/12/6 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (二)SpringBoot功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

web開發(fā)

spring boot web開發(fā)非常的簡(jiǎn)單,其中包括常用的json輸出、filters、property、log等

json 接口開發(fā)

在以前的spring 開發(fā)的時(shí)候需要我們提供json接口的時(shí)候需要做那些配置呢

就這樣我們會(huì)經(jīng)常由于配置錯(cuò)誤,導(dǎo)致406錯(cuò)誤等等,spring boot如何做呢,只需要類添加 @RestController 即可,默認(rèn)類中的方法都會(huì)以json的格式返回

如果我們需要使用頁面開發(fā)只要使用 @Controller ,下面會(huì)結(jié)合模板來說明

自定義Filter

我們常常在項(xiàng)目中會(huì)使用filters用于錄調(diào)用日志、排除有XSS威脅的字符、執(zhí)行權(quán)限驗(yàn)證等等。Spring Boot自動(dòng)添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,并且我們可以自定義Filter。

1 @Configuration 2 public class WebConfiguration { 3 4 @Bean 5 6 public RemoteIpFilter remoteIpFilter() { 7 8 return new RemoteIpFilter(); 9 10 } 11 12 @Bean 13 14 public FilterRegistrationBean testFilterRegistration() { 15 16 FilterRegistrationBean registration = new FilterRegistrationBean(); 17 18 registration.setFilter(new MyFilter()); 19 20 registration.addUrlPatterns("/*"); 21 22 registration.addInitParameter("paramName", "paramValue"); 23 24 registration.setName("MyFilter"); 25 26 registration.setOrder(1); 27 28 return registration; 29 30 } 31 32 public class MyFilter implements Filter { 33 34 @Override 35 36 public void destroy() { 37 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 44 public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) 45 46 throws IOException, ServletException { 47 48 // TODO Auto-generated method stub 49 50 HttpServletRequest request = (HttpServletRequest) srequest; 51 52 System.out.println("this is MyFilter,url :"+request.getRequestURI()); 53 54 filterChain.doFilter(srequest, sresponse); 55 56 } 57 58 @Override 59 60 public void init(FilterConfig arg0) throws ServletException { 61 62 // TODO Auto-generated method stub 63 64 } 65 66 } 67 68 }

自定義Property

在web開發(fā)的過程中,我經(jīng)常需要自定義一些配置文件,如何使用呢

配置在application.properties中

log配置

配置輸出的地址和輸出級(jí)別

數(shù)據(jù)庫操作

在這里我重點(diǎn)講述mysql、spring data jpa的使用,其中mysql 就不用說了大家很熟悉,jpa是利用Hibernate生成各種自動(dòng)化的sql,如果只是簡(jiǎn)單的增刪改查,基本上不用手寫了,spring內(nèi)部已經(jīng)幫大家封裝實(shí)現(xiàn)了。

下面簡(jiǎn)單介紹一下如何在spring boot中使用

1、添加相jar包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

2、添加配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= true

其實(shí)這個(gè)hibernate.hbm2ddl.auto參數(shù)的作用主要用于:自動(dòng)創(chuàng)建|更新|驗(yàn)證數(shù)據(jù)庫表結(jié)構(gòu),有四個(gè)值:

dialect 主要是指定生成表名的存儲(chǔ)引擎為InneoDB
show-sql 是否打印出自動(dòng)生產(chǎn)的SQL,方便調(diào)試的時(shí)候查看

3、添加實(shí)體類和Dao

@Entity public class User implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValueprivate Long id;@Column(nullable = false, unique = true)private String userName;@Column(nullable = false)private String passWord;@Column(nullable = false, unique = true)private String email;@Column(nullable = true, unique = true)private String nickName;@Column(nullable = false)private String regTime;//省略getter settet方法、構(gòu)造方法 }

dao只要繼承JpaRepository類就可以,幾乎可以不用寫方法,還有一個(gè)特別有尿性的功能非常贊,就是可以根據(jù)方法名來自動(dòng)的生產(chǎn)SQL,比如findByUserName 會(huì)自動(dòng)生產(chǎn)一個(gè)以 userName 為參數(shù)的查詢方法,比如 findAlll 自動(dòng)會(huì)查詢表里面的所有數(shù)據(jù),比如自動(dòng)分頁等等。。

Entity中不映射成列的字段得加@Transient 注解,不加注解也會(huì)映射成列

public interface UserRepository extends JpaRepository<User, Long> {User findByUserName(String userName);User findByUserNameOrEmail(String username, String email);

4、測(cè)試

?

@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"));}}

當(dāng)讓 spring data jpa 還有很多功能,比如封裝好的分頁,可以自己定義SQL,主從分離等等,這里就不詳細(xì)講了

thymeleaf模板

Spring boot 推薦使用來代替jsp,thymeleaf模板到底是什么來頭呢,讓spring大哥來推薦,下面我們來聊聊

Thymeleaf 介紹

Thymeleaf是一款用于渲染XML/XHTML/HTML5內(nèi)容的模板引擎。類似JSP,Velocity,FreeMaker等,它也可以輕易的與Spring MVC等Web框架進(jìn)行集成作為Web應(yīng)用的模板引擎。與其它模板引擎相比,Thymeleaf最大的特點(diǎn)是能夠直接在瀏覽器中打開并正確顯示模板頁面,而不需要啟動(dòng)整個(gè)Web應(yīng)用。

好了,你們說了我們已經(jīng)習(xí)慣使用了什么 velocity,FreMaker,beetle之類的模版,那么到底好在哪里呢? 比一比吧 Thymeleaf是與眾不同的,因?yàn)樗褂昧俗匀坏哪0寮夹g(shù)。這意味著Thymeleaf的模板語法并不會(huì)破壞文檔的結(jié)構(gòu),模板依舊是有效的XML文檔。模板還可以用作工作原型,Thymeleaf會(huì)在運(yùn)行期替換掉靜態(tài)值。Velocity與FreeMarker則是連續(xù)的文本處理器。 下面的代碼示例分別使用Velocity、FreeMarker與Thymeleaf打印出一條消息:

?

Velocity: <p>$message</p> FreeMarker: <p>${message}</p> Thymeleaf: <p th:text="${message}">Hello World!</p>

** 注意,由于Thymeleaf使用了XML DOM解析器,因此它并不適合于處理大規(guī)模的XML文件。**

URL

URL在Web應(yīng)用模板中占據(jù)著十分重要的地位,需要特別注意的是Thymeleaf對(duì)于URL的處理是通過語法@{…}來處理的。Thymeleaf支持絕對(duì)路徑URL:

?

頁面即原型

在Web開發(fā)過程中一個(gè)繞不開的話題就是前端工程師與后端工程師的寫作,在傳統(tǒng)Java Web開發(fā)過程中,前端工程師和后端工程師一樣,也需要安裝一套完整的開發(fā)環(huán)境,然后各類Java IDE中修改模板、靜態(tài)資源文件,啟動(dòng)/重啟/重新加載應(yīng)用服務(wù)器,刷新頁面查看最終效果。

但實(shí)際上前端工程師的職責(zé)更多應(yīng)該關(guān)注于頁面本身而非后端,使用JSP,Velocity等傳統(tǒng)的Java模板引擎很難做到這一點(diǎn),因?yàn)樗鼈儽仨氃趹?yīng)用服務(wù)器中渲染完成后才能在瀏覽器中看到結(jié)果,而Thymeleaf從根本上顛覆了這一過程,通過屬性進(jìn)行模板渲染不會(huì)引入任何新的瀏覽器不能識(shí)別的標(biāo)簽,例如JSP中的,不會(huì)在Tag內(nèi)部寫表達(dá)式。整個(gè)頁面直接作為HTML文件用瀏覽器打開,幾乎就可以看到最終的效果,這大大解放了前端工程師的生產(chǎn)力,它們的最終交付物就是純的HTML/CSS/JavaScript文件。

Gradle 構(gòu)建工具

spring 項(xiàng)目建議使用Gradle進(jìn)行構(gòu)建項(xiàng)目,相比maven來講 Gradle更簡(jiǎn)潔,而且gradle更時(shí)候大型復(fù)雜項(xiàng)目的構(gòu)建。gradle吸收了maven和ant的特點(diǎn)而來,不過目前maven仍然是Java界的主流,大家可以先了解了解。

一個(gè)使用gradle配置的項(xiàng)目

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.7 repositories { // Maven 倉庫mavenLocal() //使用本地倉庫mavenCentral() //使用中央倉庫maven { url "http://repo.spring.io/libs-snapshot" } //使用遠(yuǎn)程倉庫 }dependencies { // 各種 依賴的jar包compile("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 }

WebJars

WebJars是一個(gè)很神奇的東西,可以讓大家以jar包的形式來使用前端的各種框架、組件。

什么是WebJars

什么是WebJars?WebJars是將客戶端(瀏覽器)資源(JavaScript,Css等)打成jar包文件,以對(duì)資源進(jìn)行統(tǒng)一依賴管理。WebJars的jar包部署在Maven中央倉庫上。

為什么使用

我們?cè)陂_發(fā)Java web項(xiàng)目的時(shí)候會(huì)使用像Maven,Gradle等構(gòu)建工具以實(shí)現(xiàn)對(duì)jar包版本依賴管理,以及項(xiàng)目的自動(dòng)化管理,但是對(duì)于JavaScript,Css等前端資源包,我們只能采用拷貝到webapp下的方式,這樣做就無法對(duì)這些資源進(jìn)行依賴管理。那么WebJars就提供給我們這些前端資源的jar包形勢(shì),我們就可以進(jìn)行依賴管理。

如何使用

1、 WebJars主官網(wǎng) 查找對(duì)于的組件,比如Vuejs

?

轉(zhuǎn)載于:https://www.cnblogs.com/tanlei-sxs/p/9487776.html

總結(jié)

以上是生活随笔為你收集整理的(二)SpringBoot功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。