前言
“越來越多的企業選擇使用spring boot 開發系統,spring boot牛在什麼地方?難不難學?心動不如行動,讓我們一起開始學習吧!”
Spring boot的優點
輕鬆創建獨立的Spring應用程序。內嵌Tomcat、jetty等web容器,不需要部署WAR文件。提供一系列的“starter” 來簡化的Maven配置。開箱即用,儘可能自動配置Spring。所用工具
EclipseJDK 1.8apache-tomcat-7.0創建項目
使用Eclipse創建一個Maven項目,如何創建?參考網上文章..
目錄結構如圖。
Src/main/java。編寫代碼存放的目錄。Src/main/resources。資源文件存放目錄。Src/test/java。測試代碼存放目錄。Maven Pom.xml說明:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.winter
HtmlCssJs
0.0.1-SNAPSHOT
war
HtmlCssJs
This is Html Css Js Demo Project
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
UTF-8
UTF-8
1.8
net.sourceforge.nekohtml
nekohtml
net.sf.json-lib
json-lib
2.4
jdk15
org.apache.shiro
shiro-spring
1.4.0
org.quartz-scheduler
quartz
2.2.1
org.springframework
spring-context-support
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat
tomcat-servlet-api
8.0.36
provided
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-mail
mysql
mysql-connector-java
javax.servlet
javax.servlet-api
javax.servlet
jstl
org.apache.tomcat
tomcat-jsp-api
org.apache.tomcat.embed
tomcat-embed-core
org.apache.tomcat.embed
tomcat-embed-jasper
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.datatype
jackson-datatype-joda
com.fasterxml.jackson.module
jackson-module-parameter-names
com.github.pagehelper
pagehelper-spring-boot-starter
1.1.2
com.alibaba
druid-spring-boot-starter
1.1.0
redis.clients
jedis
aspectj
aspectjweaver
1.5.3
org.weixin4j.spring.boot
weixin4j-spring-boot-starter
1.0.0
com.soecode.wx-tools
wx-tools
2.1.4-RELEASE
org.springframework.boot
spring-boot-maven-plugin
其中
設置spring-boot-starter-parent為父親項目
這種方式可以比較容易的使用父項目中的starters的依賴。
裡面也下載了開發微信公眾號的相關jar包,感興趣的朋友可以查看我的其他文章....
創建SpringbootApplication.java啟動類和配置文件(數據庫信息和thymeleaf信息)
@SpringBootApplication
@EnableScheduling
@EnableCaching
public class SpringbootApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(SpringbootApplication.class);
}
public static void main(String[] args) {
System.out.println("The service to start.");
SpringApplication.run(SpringbootApplication.class, args);
System.out.println("The service has started.");
}
// 如果需要改動端口,複寫父類方法
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// TODO Auto-generated method stub
container.setPort(8080);
}
}
spring.datasource.url=jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# \\u521D\\u59CB\\u5316\\u5927\\u5C0F\\uFF0C\\u6700\\u5C0F\\uFF0C\\u6700\\u5927
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# \\u914D\\u7F6E\\u83B7\\u53D6\\u8FDE\\u63A5\\u7B49\\u5F85\\u8D85\\u65F6\\u7684\\u65F6\\u95F4
spring.datasource.maxWait=60000
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.cwh.springbootMybatis.entity
#thymeleaf模板使用
#關閉thymeleaf緩存
spring.thymeleaf.cache=false
#去掉thymeleaf的嚴格的模板校驗
#spring.thymeleaf.mode=LEGACYHTML5
#Spring Jpa
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
到此為止,我們已經滿足能測試Springboot的例子,創建包名和類名
因為Springboot內置Tomcat,所以只需要在SpringbootApplication啟動類右鍵然後Run as->Java Application就行了 控制檯會打印出Springboot加載信息,如果不出意外 控制檯最後會提示The service has started.則啟動成功..看到沒還有端口信息
然後就去瀏覽器請求這個URL,Ip+端口+請求映射路由 不知道IP 通過cmd後 ipconfig查
也可通過http://127.0.0.1:8080/hello訪問
使用Jpa
Spring JPA這東西簡直就是神,乎不需要寫什麼關於數據庫訪問的代碼一個基本的 CURD 的功能就出來了
建立第一個數據實體類
然後啟動Springboot類
在數據庫發現已經按要求生成了表
是不是感覺很牛逼???還有更牛逼的...
自定義一個接口然後繼承JpaRepository
然後它就具備了很多很多功能,看下源碼
ctrl+o 按一次是當前類的字段與方法 再按一次顯示繼承自父類的字段與方法
OK,到了這步 不得不提一下,其實我們在控制器層可以直接注入持久層,到這樣有個問題就是擴展性不強。所以我們還是寫入業務層,然後讓控制層注入業務層。
OK 這邊寫完了Service層,然後到控制層,在控制層注入Service層
使用thymeleaf
在src/main/resources新增文件
然後在Html文件中。取得Model域中的值
大功告成,然後啟動服務訪問就可以看到效果了
要記得將Java實體類複寫ToString方法哦