关于JPA和Spring Data JPA详解:Spring Data JPA数据持久化
1、引入依赖
<!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- springdata jpa依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2、配置application.properties
##端口号 server.port=8080 ##数据库配置 ##数据库地址 spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false ##数据库用户名 spring.datasource.username=root ##数据库密码 spring.datasource.password=root ##数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ##validate 加载hibernate时,验证创建数据库表结构 ##create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 ##create-drop 加载hibernate时创建,退出是删除表结构 ##update 加载hibernate自动更新数据库结构 ##validate 启动时验证表的结构,不会创建表 ##none 启动时不做任何操作 spring.jpa.hibernate.ddl-auto=create ##控制台打印sql spring.jpa.show-sql=true
3、实体类
其中@Table中的name对应数据库中表的名称
package com.echola.entity; import javax.persistence.*; @Entity //标明这是一个实体类 @Table(name="city") //Table配置的属性name指出它银蛇数据库的表,这样实体就映射到对应的表上 public class City { @Id //标注那个属性为表单的主键 @GeneratedValue(strategy=GenerationType.AUTO) //配置采用何种策略生成主键 private int cityId; private String cityName; private String cityIntroduce; public City(int cityId, String cityName, String cityIntroduce) { this.cityId = cityId; this.cityName = cityName; this.cityIntroduce = cityIntroduce; } public City(String cityName, String cityIntroduce) { this.cityName = cityName; this.cityIntroduce = cityIntroduce; } public City() { } public int getCityId() { return cityId; } public void setCityId(int cityId) { this.cityId = cityId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getCityIntroduce() { return cityIntroduce; } public void setCityIntroduce(String cityIntroduce) { this.cityIntroduce = cityIntroduce; } }
4、持久层
CityRepository,继承了JpaRepository,本文只是简单介绍了jpa的简单功能,所以JpaRepository中内置的方法已经足够使用。
package com.echola.repository; import com.dalaoyang.entity.City; import org.springframework.data.jpa.repository.JpaRepository; public interface CityRepository extends JpaRepository<City,Integer> { }
5、控制层
package com.echola.controller; import com.dalaoyang.entity.City; import com.dalaoyang.repository.CityRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class CityController { @Autowired private CityRepository cityRepository; //http://localhost:8080/saveCity?cityName=北京&cityIntroduce=中国首都 @GetMapping(value = "saveCity") public String saveCity(String cityName,String cityIntroduce){ City city = new City(cityName,cityIntroduce); cityRepository.save(city); return "success"; } //http://localhost:8080/deleteCity?cityId=2 @GetMapping(value = "deleteCity") public String deleteCity(int cityId){ cityRepository.delete(cityId); return "success"; } //http://localhost:8080/updateCity?cityId=3&cityName=沈阳&cityIntroduce=辽宁省省会 @GetMapping(value = "updateCity") public String updateCity(int cityId,String cityName,String cityIntroduce){ City city = new City(cityId,cityName,cityIntroduce); cityRepository.save(city); return "success"; } //http://localhost:8080/getCityById?cityId=3 @GetMapping(value = "getCityById") public City getCityById(int cityId){ City city = cityRepository.findOne(cityId); return city; } }