java
版本:1.8.0
maven
版本:3.3.9
GirlApplication.java
每次可以通过运行该方法,启动服务。
package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GirlApplication {
public static void main(String[] args) {
SpringApplication.run(GirlApplication.class, args);
}
}
配置文件application.properties和application.yml任意一个就行
application.properties
server.port=8081
server.context-path=/girl
server:
port: 8081
context-path: /girl
项目访问路径是:http://localhost:8080/girl/hello
application.xml
server:
port: 8080
cupSize: B
age: 18
content: "cupSize:${cupSize},age:${age}"
package com.imooc;
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;
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private int age;
@Value("${content}")
@RequestMapping(value="/hello")
public String say(){
return cupSize + age;
}
}
application.yml
server:
port: 8080
girl:
cupSize: B
age: 18
增加java方法定义girl开头的变量
GirlProperties
package com.imooc;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2017/7/12.
*/
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
/**下面省略get/set方法**/
/**... ...**/
}
package com.imooc;
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;
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value="/hello")
public String say(){
return girlProperties.getCupSize();
}
}
application复制出来两个结构相同的文件
application-dev.yml
application-prod.yml
然后在application.yml指定用哪个配置文件启动
用命令方式启动
1.进入项目目录下,执行
mvn install
2.根据一个项目文件启动项目
java -jar target/girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
@Controller:处理http请求
@RestController:Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
@RequestMapping:配置url映射
pom.xml增加
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency>
Controller里
@RequestMapping(value={"/hello","/hi"},method=RequestMethod.GET)
映射了两个地址
@PathVariable:获取url中的数据
@RequestParam:获取请求参数的值
@GetMapping:组合注解
Controller的写法
@RequestMapping(value="/hello/{id}",method=RequestMethod.GET) public String say(@PathVariable("id") Integer id){ return "id:"+id; }
访问路径:http://localhost:8081/hello/44
Controller的写法
@RequestMapping(value="/hello",method=RequestMethod.GET) public String say(@RequestParam("id") Integer id){ return "id:"+id; }访问路径:http://localhost:8081/hello?id=2
id为空会报错。那么下面设置可以避免
/*@RequestMapping(value="/hello",method=RequestMethod.GET)*/
/*简化RequestMethod.GET方式。同理还有@PostMapping*/ @GetMapping(value="/hello")public String say( @RequestParam(value= "id",required= false,defaultValue = "0") Integer id){ return "id:"+id;}
Spring-Data-Jpa
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate,TopLink等。
pom.xml
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-jpaartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency>
application.yml
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mysql username: root password: root jpa: hibernate: ddl-auto: create show-sql: true
girl.java
package com.imooc;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
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;
}
}
package com.imooc;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository {
//通过年龄来查询,扩展接口
public List findByAge(Integer age);
}
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
/**
* 查询所有女生列表
* @return
*/
@GetMapping(value="/girls")
public List girlList(){
return girlRepository.findAll();
}
/**
* 添加一个女生
* @param cupSize
* @param age
* @return
*/
@PostMapping(value="/girls")
public Girl girlAdd(@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
return girlRepository.save(girl);
}
/**
* 查询一个女生
* @return
*/
@GetMapping(value="/girls/{id}")
public Girl girlFindOne(@PathVariable("id") Integer id){
return girlRepository.findOne(id);
}
/**
* 更新
*/
@PutMapping(value="/girls/{id}")
public Girl girlUpdate(@PathVariable("id") Integer id,
@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setId(id);
girl.setCupSize(cupSize);
girl.setAge(age);
return girlRepository.save(girl);
}
/**
* 删除
*/
@DeleteMapping(value="/girls/{id}")
public void girlDelete(@PathVariable("id") Integer id){
girlRepository.delete(id);
}
//通过年龄查询女生列表
@GetMapping(value="/girls/age/{age}")
public List girlListByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
}
@Transactional