如下是本身学习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