常用注解
- @PathVariable:获取Url中的数据
- @RequestParam: 获取请求参数的值
- @GetMapping : 组合注解,相当于@RequestMapping(method = RequestMethod.GET)
- @PostMapping: 组合注解,相当于@RequestMapping(method = RequestMethod.POST)
@RestController:组合注解,相当于@Controller 加 @ResponseBody
## Jpa ##
Spring-Data-Jpa定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate,TopLink.
pom依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web项目必须引入的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql附件-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--spring官方模板,跳转指定页面-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
数据库配置:
增删改查代码:
实体类 :
package com.cym;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by hasee
* on 2017/4/15.
*/
@Entity //表示当前类代表数据库中的表
public class Girl {
@Id
@GeneratedValue //自增
private Integer id;
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;
}
}
接口:
package com.cym;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by hasee
* on 2017/4/15.
*/
public interface GirlPreposity extends JpaRepository<Girl,Integer>{
//通过年龄age查询
public List<Girl> findByAge(Integer age);
}
Controller层(逻辑实际是写在servicec层,这里为了简化):
/**
* Created by hasee
* on 2017/4/15.
*/
@RestController
public class GirlController {
@Autowired
private GirlPreposity girlPreposity;
/**
*查询所有列表
*/
@GetMapping(value = "/girls")
public List<Girl> findlist(){
return girlPreposity.findAll();
}
/**
*id查询一条数据
*/
@GetMapping(value = "/select")
public Girl findid(@RequestParam(value = "id",defaultValue = "1") Integer id){
return girlPreposity.findOne(id);
}
/**
*age 查询
*/
@GetMapping(value = "/girls/age/{age}")
public List<Girl> findlist1(@PathVariable("age") Integer age){
return girlPreposity.findByAge(age);
}
/**
*添加一条数据
*/
@PostMapping (value= "/girls")
public Girl add(@RequestParam(value = "age") Integer age){
Girl girl = new Girl();
girl.setAge(age);
return girlPreposity.save(girl);
}
/**
*更新
*/
@PutMapping(value= "/girls/{id}")
public Girl update(@PathVariable("id") Integer id,
@RequestParam(value = "age") Integer age){
Girl girl = new Girl();
girl.setId(id);
girl.setAge(age);
System.out.println(id+age);
return girlPreposity.save(girl);
}
/**
*更新
*/
@DeleteMapping(value= "/girls/{id}")
public void delet(@PathVariable("id") Integer id){
girlPreposity.delete(id);
}
}