eclipse下第一个spring-boot

eclipse下创建spring-boot参考文章http://blog.csdn.net/clementad/article/details/51334064
  1. 按着以上文章创建好后, 目录结构如下:

    eclipse下第一个spring-boot
    文章图片
    image.png
  2. 以上HelloController.java是自己创建的内容是:
package com.lxf; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping(value="https://www.it610.com/hello", method=RequestMethod.GET) public String say() { return "spring boot hello world"; } }

  1. 在MyfirstSpringBootApplication文件中main方法右键->run as -> Spring Boot App,就可以把spring-boot启动, 也就意味着spring-boot中的tomcate会自动启动, 默认端口是8080, 注意如果有占用会启动失败;
  2. 启动成功后,在控制前会看到类似以下的提示,代表8080端口已启用
    eclipse下第一个spring-boot
    文章图片
    image.png
    5.访问地址: http://localhost:8080/hello
    eclipse下第一个spring-boot
    文章图片
    image.png
spring-boot的启动方式
  1. 在MyfirstSpringBootApplication文件中main方法右键->run as -> Spring Boot App. 关闭spring-boot,在控制前右上侧有红色的关闭按钮;
  2. 进入项目目录
cd ~/workspaceEE/myfirst-spring-boot/ mvn spring-boot:run

  1. 进入项目目录
cd ~/workspaceEE/myfirst-spring-boot/ mvn install cd target

后会看到在target目录下多了一个 myfirst-spring-boot-0.0.1-SNAPSHOT.jar, 然后使用java命令启动
java -jar myfirst-spring-boot-0.0.1-SNAPSHOT.jar

属性配置
  • 目录存放src/main/resources/application.yml, 配置说明如下:
server: port: 8089 context-path: /gril cupSize: B age: 19 content: "cupSize = ${cupSize}, age = ${age}" //在配置文件中引用配置文件自身的属性girl://定义公有属性,该配置需要新建 GirlProgerties.java类配合使用 age: 199 heigh: 19.99

  • GirlProgerties.java 类配合属性girl的子属性使用
package com.lxf; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "girl") //该配置对应application.yml配置文件中的girl属性 public class GirlProgerties { private Integer age; private float height; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } }

  • 在Controller中获取application.yml配置文件的属性
package com.lxf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 获取application.yml属性内容 * @author lxf */ @RestController public class HelloController { @Value("${cupSize}") private String cupSize; @Value("${age}") private int age; @Value("${content}") private String content; @Autowired private GirlProgerties girlProgerties; @RequestMapping(value="https://www.it610.com/hello", method=RequestMethod.GET) public String say() { return cupSize + " " +age + content + girlProgerties.getAge(); } }

  • 开发环境 配置 application-develop.yml, 注意,服务器端口配置为8088
server: port: 8088 context-path: /gril cupSize: A age: 18 content: "cupSize = ${cupSize}, age = ${age}"girl: age: 188 heigh: 18.8

  • 生产环境 配置 application-develop.yml, 注意,服务器端口配置为8089
  • 公共配置 application.yml, 调用 开发环境 的配置
spring: profiles: active: develop

  • 根据以上第一种启动spring-boot方式启动, 开启8088端口开发环境
  • 感觉以上第三种启动方式, java -jar 带启动参数的方式启动,参数为生产环境配置
java -jar myfirst-spring-boot-0.0.1-SNAPSHOT.jar --spring.profiles.active=production

  • 以上两步分别启动了 开发环境8088, 和生产环境8089端口
  • 可以分别访问
http://localhost:8088/gril/hello http://localhost:8089/gril/hello

Controller的使用
  • 常用注解

    eclipse下第一个spring-boot
    文章图片
    image.png
  • 使用模板解析配合@Controller注解 使用
    控制器
package com.lxf; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @Controller public class HelloController { @RequestMapping(value="https://www.it610.com/hello", method=RequestMethod.GET) public String say() { return "index"; } }

pom.xml添加依赖
org.springframework.boot spring-boot-starter-thymeleaf

在src/main/resources/templates中新增index.html
Hello Spring Boot

启动后访问: http://localhost:8088/gril/hello
配置两个url指向同一个控制器方法
@RequestMapping(value=https://www.it610.com/article/{"/hello", "hi"}, method=RequestMethod.GET) public String say() { return cupSize + " " +age + content + girlProgerties.getAge(); }

以上配置http://localhost:8088/gril/hello和http://localhost:8088/gril/hi等效
同时@RequestMapping也可以作用在控制器上与springMvc用法一致
获取URL参数的注解
eclipse下第一个spring-boot
文章图片
image.png
  • 接收参数一
@RequestMapping(value="https://www.it610.com/hello/{id}", method=RequestMethod.GET) public String say(@PathVariable("id") Integer id) { return "id="+id; }

访问: http://localhost:8088/gril/hello/12, 输出结果 id=12
  • 接收参数二
@RequestMapping(value="https://www.it610.com/hello", method=RequestMethod.GET) public String say(@RequestParam(value="https://www.it610.com/article/id", required=false, defaultValue="https://www.it610.com/article/0") Integer myId) { return "id="+myId;

【eclipse下第一个spring-boot】访问: http://localhost:8088/gril/hello/hello?id=123 , 输出: id=123
@RequestMapping(value="https://www.it610.com/hello", method=RequestMethod.GET) @GetMapping(value="https://www.it610.com/hello") 和以上一行等效

使用数据库
  • pom.xml配置添加依赖
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java 5.1.27

  • application.properties
server.port=8099 #DB Configuration: spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password=123456#JPA Configuration: spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true

  • Girl实体类
package com.lxf.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.stereotype.Component; /** * gril实体类, 对应girl数据库 * @author lxf */ @Entity @Table(name="t_girl") public class Girl { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private Integer age; private String curSize; 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 getCurSize() { return curSize; }public void setCurSize(String curSize) { this.curSize = curSize; } }

  • 启动spring-boot会在数据库中自动创建 t_girl
  • 将application.properties配置
spring.jpa.hibernate.ddl-auto=update

  • 新建 GirlRepository接口操作数据库,相当与Dao
package com.lxf; import com.lxf.model.Girl; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface GirlRepository extends JpaRepository { /** * 自定根据年龄查询列表 * @param age * @return */ public List findByAge(Integer age); }

  • 新建GirlService
package com.lxf; import com.lxf.model.Girl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @Service public class GirlService { @Autowired GirlRepository girlRepository; /** * 使用@Transactional事务注解 */ @Transactional public void insertTwo() { Girl girlA = new Girl(); girlA.setAge(36); girlA.setCurSize("A"); girlRepository.save(girlA); Girl girlB = new Girl(); girlB.setAge(38); girlB.setCurSize("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBb"); girlRepository.save(girlB); } }

  • 新建GirlController控制器
package com.lxf; import com.lxf.model.Girl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 请求controller */ @RestController public class GirlController { @Autowired private GirlRepository girlRepository; @Autowired GirlService girlService; /** * 查询女孩列表 * @return */ @GetMapping(value="https://www.it610.com/girls") public List girlList() {return girlRepository.findAll(); } @GetMapping(value = "https://www.it610.com/girls/{id}") public Girl girlFindOne(@PathVariable("id") Integer id) { return girlRepository.findOne(id); } /** * 新增女生 */ @PostMapping(value = "https://www.it610.com/girls") public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) { Girl girl = new Girl(); girl.setAge(age); girl.setCurSize(cupSize); return girlRepository.save(girl); }/** * 更新女孩信息 * @param id * @param cupSize * @param age * @return */ @PutMapping(value = "https://www.it610.com/girls/{id}") public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("age") Integer age, @RequestParam("cupSize") String cupSize ){ Girl girl = new Girl(); girl.setId(id); girl.setCurSize(cupSize); girl.setAge(age); return girlRepository.save(girl); }/** * 删除女生信息 * @param id */ @DeleteMapping(value = "https://www.it610.com/girls/{id}") public void girlDel(@PathVariable("id") Integer id) { girlRepository.delete(id); }/** * 通过年龄查询列表 * @param age * @return */ @GetMapping(value = "https://www.it610.com/girls/age/{age}") public List girlListByAge(@PathVariable("age") Integer age) { return girlRepository.findByAge(age); }/** * 调用事务同时添加两条记录 */ @PostMapping(value = "https://www.it610.com/girls/two") public void girlAddTwo() { girlService.insertTwo(); } }

    推荐阅读