上一篇介紹了springboot簡單整合mybatis的教程。這一篇是介紹springboot簡單整合jpa的教程。java
因爲jpa的功能強大,後續會繼續寫關於jpa的介紹已經使用,本文只是簡單介紹一下它與springboot的整合。mysql
jpa不須要像mybatis同樣建立表,首先給你們看一下application.properties文件代碼,其中包含了jpa的配置和數據庫配置,尤爲注意一下spring.jpa.hibernate.ddl-auto屬性,代碼以下:web
##端口號
server.port=8888
##數據庫配置
##數據庫地址
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
複製代碼
啓動類applicationspring
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
}
複製代碼
pom文件大體和整合mybatis同樣,只是把其中的mybatis改爲了jpa,代碼以下:sql
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_jpa</name>
<description>springboot_jpa</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
實體類city,其中@Table中的name對應數據庫中表的名稱數據庫
package com.dalaoyang.entity;
import javax.persistence.*;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.Entity
* @email 397600342@qq.com
* @date 2018/4/7
*/
@Entity
@Table(name="city")
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;
}
}
複製代碼
而後就是jpa的重要地方,CityRepository,繼承了JpaRepository, 因爲本文只是簡單介紹了jpa的簡單功能,因此JpaRepository中內置的方法已經足夠使用。apache
代碼以下:springboot
package com.dalaoyang.repository;
import com.dalaoyang.entity.City;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.Repository
* @email 397600342@qq.com
* @date 2018/4/7
*/
public interface CityRepository extends JpaRepository<City,Integer> {
}
複製代碼
最後是controller,裏面和mybatis整合同樣,方法上面寫的就是對應的測試方法。bash
package com.dalaoyang.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;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email 397600342@qq.com
* @date 2018/4/7
*/
@RestController
public class CityController {
@Autowired
private CityRepository cityRepository;
//http://localhost:8888/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:8888/deleteCity?cityId=2
@GetMapping(value = "deleteCity")
public String deleteCity(int cityId){
cityRepository.delete(cityId);
return "success";
}
//http://localhost:8888/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:8888/getCityById?cityId=3
@GetMapping(value = "getCityById")
public City getCityById(int cityId){
City city = cityRepository.findOne(cityId);
return city;
}
}
複製代碼
到這裏啓動項目就能夠簡單測試一下整合的效果了。mybatis