如下是本身學習spring-boot的過程當中整理的筆記。html
1、spring-boot介紹java
一、Spring-boot跟springframe的關係mysql
spring framework就比如一個大型的電子元件生產公司,它生產的元件性能都很優秀,可是坊間使用它的元件的做坊,拿到手以後還得搞一些電焊,銜接,可能要花個10天半個月最後作成一個家電或者機器人(無論啥了,只是例子)。web
有一天這個公司就宣佈,咱們如今提供了一些功能模塊,好比攝像頭傳感器,擴音器傳感器,壓力傳感器,它們都是統一的usb接口的,只須要插線鏈接就能使用了。這樣是否是大大下降了坊間小做坊的人力物力各類力,5分鐘就拼湊出一個機器人了有木有。redis
看出啥來了嗎?各類電子元件就是springframe上面關於其餘諸如mq,websocket,zookeeper,redis的整合代碼包,沒有springbootstarter咱們仍是得本身去調用各類不一樣的接線頭,芯片端口去接線調試,還可能把芯片弄壞,弄燒了。。。spring
[摘]sql
二、SpringBoot誕生數據庫
spring4--->發佈spring-boot1.3.* --->各個子項目,好比spring-boot-starter-webspringboot
三、spring-boot介紹websocket
Spring Boot提供了一個強大的一鍵式Spring的集成開發環境,可以單獨進行一個Spring應用的開發,其中:
(1)集中式配置(application.properties)+註解,大大簡化了開發流程
(2)內嵌的Tomcat和Jetty容器,可直接打成jar包啓動,無需提供Javawar包以及繁瑣的Web配置
(3)提供了Spring各個插件的基於Maven的pom模板配置,開箱即用,便利無比
(4)能夠在任何你想自動化配置的地方,實現可能
(5)提供更多的企業級開發特性,如何系統監控,健康診斷,權限控制
(6)無冗餘代碼生成和XML強制配置
(7)提供支持強大的Restfult風格的編碼,很是簡潔
總結:
springboot提供了基於spring的各類starter(傳感器)的快速啓動,搭建框架的便利;spring-boot-starter-xxx就是上面說的各類傳感器,對各類芯片的封裝,提供簡單統一的調用,配置方式。你去學會發現只須要加入starter依賴,就能使用默認配置了快速把新功能加入到項目中了。
With Spring Bootyou can focus moreon business features and less on infrastructure.
2、spring-boot經常使用註解
一、@SpringBootApplication
該註解等價於以默認屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。
二、Configuration
至關於傳統的xml配置文件,若是有些第三方庫須要用到xml文件,建議仍然經過@Configuration類做爲項目的配置主類——可使用@ImportResource註解加載xml配置文件。
三、EnableAutoConfiguration
Spring Boot自動配置(auto-configuration):嘗試根據你添加的jar依賴自動配置你的Spring應用。若是發現了你不想要的特定自動配置類,你可使用排除屬性來禁用它們。
四、ComponentScan
表示將該類自動發現(掃描)並註冊爲Bean,能夠自動收集全部的Spring組件,包括@Configuration類。咱們常用@ComponentScan註解搜索beans,並結合@Autowired註解導入。若是沒有配置的話,SpringBoot會掃描啓動類所在包下以及子包下的使用了@Service,@Repository等註解的類。
五、@RestController
@ResponseBody和@Controller的合集
六、@Import
用來導入其餘配置類。
七、@ImportResource
用來加載xml配置文件。
八、@Bean
用@Bean標註方法等價於XML中配置的bean。
九、@Value
注入Springbootapplication.properties配置的屬性的值。
十、@Inject
等價於默認的@Autowired,只是沒有required屬性;
十一、其餘
@ResponseBody、@Controller、@RequestMapping、@Autowired、@Service、@Repository
3、建立一個maven項目
一、建立一個maven項目
new maven project 下一步,下一步,選擇過濾條件:maven-archetype-webapp
填寫groupid(組織名),和 artifaceid(項目名)
點擊 finish,完成項目建立。
二、編寫pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent>
三、編寫啓動類Application.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 啓動類 * @author zhongshibo */ @SpringBootApplication public classApplication { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
四、建立controllerHelloWorldController.java
importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; /** * 啓動類 * @author zhongshibo */ @SpringBootApplication public classApplication { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
五、鏈接mysql數據庫
5.1 在pom.xml中添加依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
5.2 在src/main/java/resources下建立application.properties文件,在文件中添加數據庫鏈接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sys spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true
六、編寫實體類User、dao(UserRepository)、service(UserService)省略具體代碼
七、編寫相應的控制層代碼(UserController)
@RestController @RequestMapping("/user") publicclassUserController { @Resource private UserService userService; @RequestMapping("/getUser") publicString getUser() { Useruser = userService.getUser(1); returnuser.getUserName()+","+user.getPasswd(); } }
八、訪問localhost:8080/user/getUser
九、使用模版thyme leaf
9.1 在pom.xml添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
9.2application.properties添加配置
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html
9.3 建立模版controller
importjava.util.Map; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; @Controller public classTemplateController { /** * 返回html模板. */ @RequestMapping("/helloHtml") public StringhelloHtml(Map<String,Object> map){ map.put("hello","fromTemplateController.helloHtml"); return "/helloHtml"; } }
10 給項目添加junit測試
10.1 在pom.xml添加依賴
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
參考:
[1] http://www.cnblogs.com/ityouknow/p/5662753.html [2] http://www.cnblogs.com/larryzeal/p/5799195.html [3] http://blog.csdn.net/xiaoyu411502/article/details/47864969