SpringBoot學習筆記09——spring-data-jpa初探
阿新 • • 發佈:2018-11-12
大家對JPA的褒貶不一,如果想弄清楚JPA是什麼、值不值得用大家可以自行百度,博主這裡就不再多說,因為說可能也說不明白,所以話不多說直接講乾貨!
1.引入依賴
下邊是整個dao曾模組的pom檔案,世界上JPA的以來只有一個,已經添加了註釋
<?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.example</groupId> <artifactId>world-address-dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>world-address-dao</name> <description>Demo project for Spring Boot</description> <parent> <artifactId>world</artifactId> <groupId>com.youyou</groupId> <version>0.0.1-SNAPSHOT</version> </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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--這裡是JPA的依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${spring.boot.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.配置檔案
#配置資料來源 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #配置資料來源 spring.datasource.url=jdbc:mysql://192.168.31.199:3306/world?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=root #配置自動建表:updata:沒有表新建,有表更新操作,控制檯顯示建表語句 spring.jpa.hibernate.ddl-auto=update #顯示執行的sql spring.jpa.show-sql=true #顯示引數 logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
3.定義資料表實體
這裡需要注意的是@Table註解中的引數是資料表的名字。
package com.youyou.address.entity; import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; /** * 資料庫實體,聯絡人 */ @Data @Entity @Table(name = "ts_contacter") public class ContacterEO { /** * 主鍵 */ @Id private String id; /** * 姓名 */ private String name; /** * 性別(0,女;1,男) */ private String sex; /** * 年齡 */ private Integer age ; /** * 電話 */ private String phone; /** * 地址 */ private String location; /** * 刪除(0,未刪除;1,刪除) */ private String dflag ; }
4.繼承JpaRepository介面
繼承JpaRepository介面之後會繼承過來一系列的增刪改查的方法
package com.youyou.address.dao;
import com.youyou.address.entity.ContacterEO;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ContacterEORepo extends JpaRepository<ContacterEO , String> {
}
5.編寫service類
這裡只寫了一個新增的方法用來測試。
package com.youyou.address.service;
import com.youyou.address.dao.ContacterDao;
import com.youyou.address.dao.ContacterEORepo;
import com.youyou.address.entity.ContacterEO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ContacterService {
@Autowired
private ContacterEORepo contacterEORepo;
/**
* 儲存聯絡人
* @param contacterEO
*/
public void saveOne(ContacterEO contacterEO){
contacterEORepo.save(contacterEO);
}
}
6.controller層
package com.youyou.address.controller;
import com.youyou.address.entity.ContacterEO;
import com.youyou.address.service.ContacterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(description = "聯絡人介面")
@RestController
@RequestMapping(value = "/contacter")
public class ContacterController {
@Autowired
private ContacterService service;
@ApiOperation(value = "新增")
@GetMapping("/add")
public ContacterEO add(ContacterEO contacterEO){
System.out.println("新增一個聯絡人");
service.saveOne(contacterEO);
return contacterEO;
}
}
觸探spring-data-jpa到此結束。