建设银行河北省分行官方网站,河池网络推广,网站后台管理模板免费下载,psd网站首页图片前些天发现了一个巨牛的人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;忍不住分享一下给大家。点击跳转到教程。
一.什么是spring boot Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention…前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住分享一下给大家。点击跳转到教程。
一.什么是spring boot Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.
摘自官网
翻译采纳了建立生产就绪Spring应用程序的观点。 Spring Boot优先于配置的惯例旨在让您尽快启动和运行。
spring boot 致力于简洁让开发者写更少的配置程序能够更快的运行和启动。它是下一代javaweb框架并且它是spring cloud微服务的基础。 二、搭建第一个sping boot 程序 可以在start.spring.io上建项目也可以用idea构建。本案列采用idea.
具体步骤
new prpject - spring initializr -{name :firstspringboot , type: mavenproject,packaging:jar ,..} -{spring version :1.5.2 web: web } - ... 应用创建成功后会生成相应的目录和文件。
其中有一个Application类,它是程序的入口:
SpringBootApplication
public class FirstspringbootApplication {public static void main(String[] args) {SpringApplication.run(FirstspringbootApplication.class, args);}
}
在resources文件下下又一个application.yml文件它是程序的配置文件。默认为空写点配置 ,程序的端口为8080,context-path为 /springboot
server:port: 8080context-path: /springboot 写一个HelloController
RestController //等同于同时加上了Controller和ResponseBody
public class HelloController {//访问/hello或者/hi任何一个地址都会返回一样的结果RequestMapping(value {/hello,/hi},method RequestMethod.GET)public String say(){return hi you!!!;}
}
运行 Application的main(),呈现会启动由于springboot自动内置了servlet容器所以不需要类似传统的方式先部署到容器再启动容器。只需要运行main()即可这时打开浏览器输入网址localhost:8080/springboot/hi 就可以在浏览器上看到: hi you!!!
三.属性配置 在appliction.yml文件添加属性
server:port: 8080context-path: /springbootgirl:name: Bage: 18content: content:${name},age:${age} 在java文件中获取name属性如下
Value(${name})private String name;
也可以通过ConfigurationProperties注解将属性注入到bean中通过Component注解将bean注解到spring容器中
ConfigurationProperties(prefixgirl)
Component
public class GirlProperties {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
}
另外可以通过配置文件制定不同环境的配置文具体见源码
spring:profiles:active: prod
四.通过jpa方式操作数据库 导入jar 在pom.xml中添加依赖: dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency
在appilication.yml中添加数据库配置
spring:profiles:active: proddatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/dbgirl?useUnicodetruecharacterEncodingutf8characterSetResultsutf8username: rootpassword: 123jpa:hibernate:ddl-auto: createshow-sql: true
这些都是数据库常见的一些配置没什么可说的其中ddl_auto: create 代表在数据库创建表update 代表更新首次启动需要create ,如果你想通过hibernate 注解的方式创建数据库的表的话之后需要改为 update.
创建一个实体girl这是基于hibernate的:
Entity
public class Girl {IdGeneratedValueprivate Integer id;private String cupSize;private Integer age;public Girl() {}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getCupSize() {return cupSize;}public void setCupSize(String cupSize) {this.cupSize cupSize;}
}
创建Dao接口, springboot 将接口类会自动注解到spring容器中不需要我吗做任何配置只需要继承JpaRepository 即可
//其中第二个参数为Id的类型
public interface GirlRep extends JpaRepositoryGirl,Integer{}
创建一个GirlController写一个获取所有girl的api和添加girl的api 自己跑一下就可以了:
RestController
public class GirlController {Autowiredprivate GirlRep girlRep;/*** 查询所有女生列表* return*/RequestMapping(value /girls,method RequestMethod.GET)public ListGirl getGirlList(){return girlRep.findAll();}/*** 添加一个女生* param cupSize* param age* return*/RequestMapping(value /girls,method RequestMethod.POST)public Girl addGirl(RequestParam(cupSize) String cupSize,RequestParam(age) Integer age){Girl girl new Girl();girl.setAge(age);girl.setCupSize(cupSize);return girlRep.save(girl);}}
如果需要事务的话在service层加Transaction注解即可。已经凌晨了我要睡了.
源码http://download.csdn.net/detail/forezp/9778235
参考资料 说明本文部分内容均来自慕课网。慕课网http://www.imooc.com 廖师兄
转自方志朋的博客 https://www.fangzhipeng.com/springboot/2017/05/25/sb25-2hour.html