server:
port: 8082 #端口号
servlet:
context-path: /girl #项目名
复制代码
yml配置文件java
server:
port: 8082 #端口号
servlet:
context-path: /girl #项目名
cupSize: B
复制代码
Controller代码mysql
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return "cupSize:"+cupSize;
}
}
复制代码
页面输出效果web
yml 配置文件spring
server:
port: 8082 #端口号
servlet:
context-path: /girl #项目名
cupSize: B
age: 18
content : "cupSize: ${cupSize},age: ${age}"
复制代码
controller调用代码sql
@RestController
public class HelloController {
@Value("${content}")
private String content;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return content;
}
}
复制代码
yml文件数据库
server:
port: 8082 #端口号
servlet:
context-path: /girl #项目名
girl:
cupSize: B
age: 18
复制代码
引用的bean类app
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
复制代码
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
复制代码
controller 注入调用代码ide
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
复制代码
spring:
profiles:
active: prod
复制代码
映射两个或以上的url到同一个方法,可使用集合。函数
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
复制代码
请求地址为:http://localhost:8082/girl/hello/sayspring-boot
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.POST)
public String say(){
return girlProperties.getCupSize();
}
}
复制代码
获取请求地址上定义的数值
@RequestMapping(value = "/say/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id ){
return "id:" +id;
}
复制代码
请求地址:http://localhost:8082/girl/hello/say/100
输出效果:
获取请求地址上?key=value 的参数值
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myId ){
return "id:" +myId;
}
复制代码
请求地址:http://localhost:8082/girl/hello/say?id=666
输出效果:
其他参数
value 为默认参数
required 表明参数是否必须
defaultValue 参数默认值
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam(value = "id", required = false,defaultValue = "0") Integer myId ){
return "id:" +myId;
}
复制代码
简化的一种注解写法,代替了
@RequestMapping(value = "/say",method = RequestMethod.GET)
@GetMapping("/say")
public String say(@RequestParam("id") Integer myId ){
return "id:" +myId;
}
复制代码
pom.xml引入JPA和mysql驱动包
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
复制代码
application.yml配置内容
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
#JPA建立表默认引擎为 MyISAM,不支持事务回滚
# 加上如下设置JPA建立表时引擎为支持事务的InnoDB,
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
复制代码
实体类
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
private Integer height;
public Girl() {}
复制代码
编写接口类继承 JpaRepository接口
public interface GirlRepository extends JpaRepository<Girl,Integer> {
}
复制代码
controller调用
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository ;
/** * 查询全部女生列表 * @return */
@GetMapping("/girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
}
复制代码
输出效果:
controller调用
使用post方式添加一条数据,返回的是添加的这个对象
/** * 添加一个女生 * @param cupSize * @param age * @param height * @return */
@PostMapping("/girls")
public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age, @RequestParam("height") Integer height){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
girl.setHeight(height);
return girlRepository.save(girl);
}
复制代码
输出效果:
经过id查找单一对象findById(id).orElse(null),没有找到数据不会报错,并返回空,
//查询一个女生
@GetMapping("/girls/{id}")
public Girl girlFindOne(@PathVariable("id") Integer id){
return girlRepository.findById(id).orElse(null);
}
复制代码
//更新一个女生
@PutMapping("/girls/{id}")
public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age, @RequestParam("height") Integer height){
Girl girl = new Girl();
girl.setId(id);
girl.setCupSize(cupSize);
girl.setAge(age);
girl.setHeight(height);
return girlRepository.save(girl);
}
复制代码
postman 使用put方法传递参数须要使用 x-www-form-urlencoded
controller 调用
// 删除一个女孩
@DeleteMapping("/girls/{id}")
public void girlDel(@PathVariable("id") Integer id){
girlRepository.deleteById(id);
}
复制代码
GirlRepository 扩展查询接口
public interface GirlRepository extends JpaRepository<Girl,Integer> {
public List<Girl> findByAge(Integer age);
}
复制代码
controller 调用
//经过年龄去查女生
@GetMapping("/girls/age/{age}")
public List<Girl> girlListByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
复制代码
在类上面打上@Service 和 @Component 都支持事务回滚
注意事项【不然回滚不了】
@Transactional回滚的方法必须为public
mysql数据库引擎必须为InnoDB,特别检查对应的表是否InnoDB
Spring boot 2.0 的JPA 自动生成的表默认引擎为MyISAM,不支持事务回滚,在application.yml配置文件加上database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
#JPA建立表默认引擎为 MyISAM,不支持事务回滚
# 加上如下设置JPA建立表时引擎为支持事务的InnoDB,
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
复制代码
Service 类代码
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo(){
Girl girlA = new Girl();
girlA.setCupSize("A");
girlA.setAge(18);
girlA.setHeight(155);
girlRepository.save(girlA);
Girl girlB = new Girl();
girlB.setCupSize("BBBBB");
girlB.setAge(16);
girlB.setHeight(170);
girlRepository.save(girlB);
}
}
复制代码