环境 :
IntelliJ IDEA 2016.2(推荐使用)
mysql
jdk 1.7.0_17
maven 3.3.9
(这个是我本地用的版本)
环境和工具安装好后,开始创建项目。
第一步:打开 IDEA 新建项目,创建一个新项目
然后:
点击 “Next” 按钮继续,会出来一个选项卡,如下图:
选择完成后点击“Next”继续,然后又出来一个面板:
选择完成后点击“Next”继续,选择你的存放路径:
选择完成,项目这是正在打开并加载maven配置,等加载完再继续下一步;
第二步:
项目打开,删除没用文件夹以及文件:
新建一个类HelloController:
然后启动项目,在启动类SpringbootDemoApplication中点击右键,选择Run:
页面访问查看工程:
http://127.0.0.1:8080/hellow/say
第三步,添加配置,连接数据库添加功能:
删除 多余的包:
配置文件 推荐使用application.yml文件,先看一下 application.properties 和application.yml文件的差别:
application.yml 相对简便一点;必须注意application.yml 中,变量和值之间必须有空格,否则变量变灰,配置会报错如图:
认识spring-boot就到这里;
下边是我做的一个例子,仅供参考:
pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
application.yml (总配置文件):
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/demo username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true
application-dev.yml (测试环境配置文件)
server: port: 8080 girl: cupSize: B age: 20
application-prod.yml(生产环境配置文件)
server: port: 8085 girl: cupSize: G age: 20
实体类Girl.class:
package com.imooc; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Created by user on 2017/4/11. * 加注解Entity , 就表明 Girl类是和数据库对应的 , Girl类中的字段会生成数据库Girl表中的字段 */ @Entity public class Girl { @Id @GeneratedValue //表示id为自增长 private Integer id; private String cupSize; private Integer age; //Constructor 构建无参的构造方法,必须要选无参的,否则连接数据库就报错 public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
新建GirlController.class:
package com.imooc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by user on 2017/4/11. */ @RestController public class GirlController { @Autowired private GirlRepository girlRepository; @Autowired private GirlService girlService; /** * 查询列表 * @return */ @GetMapping(value="/girls") public List<Girl> girlList(){ return girlRepository.findAll(); }; /** * 添加 * @param cupSize * @param age * @return * 访问链接:http://127.0.0.1:8085/girlAdd?cupSize=F&age=19 */ @PostMapping(value="/girlAdd") public Girl gilrAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); }; /** * 根据ID查找 * @param girlid * @return */ // @GetMapping(value="/findGirlById") 传参字段@RequestParam("id") @GetMapping(value="/findGirlById/{id}") public Girl findGirlById(@PathVariable("id") Integer girlid){ return girlRepository.findOne(girlid); }; /** * 修改 * @param girlid * @param cupSize * @param age * @return * 访问路径:http://127.0.0.1:8085/updateGirlById?id=2&cupSize=F&age=30 */ @PutMapping(value="/updateGirlById/{id}") public Girl updateGirlById(@PathVariable("id") Integer girlid, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setId(girlid); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); }; /** * 删除 * @param id */ @DeleteMapping(value="/deleteGirlById/{id}") public void deleteGirlById(@PathVariable("id") Integer id){ girlRepository.delete(id); }; /** * 根据年龄查询 查出多个对象 * @param age * @return */ @GetMapping(value="/findByAge/{age}") public List<Girl> findByAge(@PathVariable("age") Integer age){ return girlRepository.findByAge(age); }; @PostMapping(value="/insertTwo") public void insertTwo(){ girlService.insertTwo(); }; }
新建GirlProperties.class:
package com.imooc; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by user on 2017/4/10. */ @Component //注入配置时必加注解 @ConfigurationProperties(prefix = "girl") //获取前缀是 girl 的配置 public class GirlProperties { private String cupSize; private Integer age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
新建GirlRepository.class:
package com.imooc; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; /** * Created by user on 2017/4/11. */ public interface GirlRepository extends JpaRepository<Girl,Integer>{ /** * 通过年龄查询 * @return */ public List<Girl> findByAge(Integer age); }
新建GirlService.class:
package com.imooc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; /** * Created by user on 2017/4/12. */ @Service public class GirlService { @Autowired private GirlRepository girlRepository; @Transactional //添加一条数据的时候也要加事务 @Transactional,只有查询的时候不加事务。 public void insertTwo(){ Girl girlA = new Girl(); girlA.setCupSize("B"); girlA.setAge(18); girlRepository.save(girlA); Girl girlB = new Girl(); girlB.setCupSize("DDDDD"); girlB.setAge(20); girlRepository.save(girlB); }; }
相关推荐
spring-boot-web代码示例,是IDEA的项目工程,使用restful风格实现增删查改,静态数据
包括'chapter-2-spring-boot-quick-start','chapter-4-spring-boot-validating-form-input','chapter-5-spring-boot-paging-sorting','doc','springboot-configuration','springboot-dubbo-client','springboot-...
spring-boot2.0全新教程实例20例.zip - [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-...
本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目...
spring-boot-demo实例
spring-boot-dubbo整合入门实例,基于注解的入门实例a
初学spring-boot webScoket 入门实例,idea项目,spring-boot版本2.6.9.
Spring-boot web项目实例,前后端数据传递,页面搭建。
现在,Spring可以在任何需要的地方为您注入web3j实例: @Autowired private Web3j web3j; 如果要通过HTTP连接到默认URL 则不需要其他配置。 否则,只需在应用程序属性中添加端点的地址: # An infura endpoint ...
spring-boot的starter是一个通过maven完成自包含并通过annotation配置使得可被spring上下文发现并实例化的一个可插拔的组件或服务。这篇文章主要介绍了Spring-boot原理及spring-boot-starter实例和代码 ,需要的朋友...
最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内置了tomcat容器,启动生成的jar包即可启动项目,也是目前流行的微服务常用框架。本文主要用到了spring-boot,以及mybatis,...
Spring-mvc Srping-boot spring-jdbc manven 整合实例
spring boot starter for RocketMQ 项目介绍 是由阿里巴巴团队开发并捐赠给apache团队的优秀消息中间件,目前已经是apache的顶级项目之一,承受过历年双十一大促的考验。 你可以通过本项目轻松的集成Rocketmq到你的...
spring-boot入门实例(jpa+postgresql+aop实现valid切面式校验)
移动支付 Payment Spring Boot 示例项目,基于最新版本,向下兼容。 请给 Star以鼓励,谢谢。 说明 Maven 最新中央仓库坐标: <groupId>cn.felord</groupId> <artifactId>payment-spring-boot-starter <version>...
使用 Spring Starter Project快速创建基于spring-boot的web项目
RocketMQ的Spring启动启动器 项目介绍 ... < artifactId> spring - boot - starter - rocketmq < / artifactId > < version> 0.1 . 0 < / version > < / dependency > 2.添加配置: spring :
Redis拾遗部分博客相关的实例代码,包括其中的抢红包案例,相关SQL也一并附上,导入之后,修改Redis的配置文件之后,可以直接运行,但是如果想要配置集群,也只需要修改连接配置即可
春天的引导实例 :grinning_face_with_smiling_eyes:该仓库为Spring / SpringBoot / SpringCloud系列技术栈,涵盖实战教程+原始码深度学习,一网打尽! 目前还在持续更新中哦!我的公众号:猿生物语,ID:JavaApes,...
本人精心整理spring boot 2.x多个实例,全部调试通过,希望对大家学习spring boot 2有所帮助,很多实例可以在项目中直接使用各位在使用过程中遇到问题,欢迎留言交流. 01-spring-boot-helloWorld //调度器 02-spring-...