Spring Boot整合Spring Data JPA過程解析
Spring Boot整合Spring Data JPA
1)加入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2)增加配置(application.properties)
server.port=8080 server.servlet.context-path=/ # database configuration spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/blog?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123 # jpa configuration # 更新或者創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu) spring.jpa.hibernate.ddl-auto=update # 控制臺打印sql語句 spring.jpa.show-sql=true spring.jpa.open-in-view=false # log configuration logging.level.root=info
3)編寫一個實(shí)體類(bean)和數(shù)據(jù)表進(jìn)行映射,并且配置好映射關(guān)系
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
/**
* 使用JPA注解配置映射關(guān)系
* Created by zxf on 2019年9月30日
*/
@Entity // 告訴JPA這是一個實(shí)體類(和數(shù)據(jù)庫映射的類)
@Table(name = "t_type") // @Table來指定和哪個數(shù)據(jù)表對應(yīng),如果省略默認(rèn)表名就是類名首字母小寫
public class Type {
@Id // 表明這是一個主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主鍵
private Long id;
@Column(name = "last_name", length = 50) // 這是和數(shù)據(jù)表對應(yīng)的一個列,省略默認(rèn)列名就是屬性名
private String name;
}
4)編寫一個Dao接口來操作實(shí)體類對應(yīng)的數(shù)據(jù)表
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by zxf on 2019年10月1日
*/
// 第一個泛型表示操作的類是Type,第二個泛型Long表示Type的主鍵id為Long類型
public interface TypeRepository extends JpaRepository<Type, Long> {
// 定義自己的方法
Type findTypeByName(String name);
}
5)service層調(diào)用測試
import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.fei.NotFoundException;
import com.fei.po.Type;
import com.fei.repository.TypeRepository;
import com.fei.service.TypeService;
/**
* Created by zxf on 2019年10月1日
*/
@Service
@Transactional
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeRepository typeRepository;
/**
* 保存一個分類
*
* @param type
* @return
*/
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
/**
* 根據(jù)id獲得一個分類對象
*
* @param id
* @return
*/
@Override
public Type getType(Long id) {
return typeRepository.findById(id).get();
}
/**
* 根據(jù)分頁參數(shù)查詢一個分類列表
*
* @param pageable
* @return
*/
@Override
public Page<Type> listType(Pageable pageable) {
return typeRepository.findAll(pageable);
}
/**
* 更新分類
*
* @param id
* @param type
* @return
*/
@Override
public Type updateType(Long id, Type type) {
Type t = typeRepository.findById(id).get();
if (t == null) {
throw new NotFoundException("類型不存在");
}
BeanUtils.copyProperties(type, t);
return typeRepository.save(t);
}
/**
* 根據(jù)id刪除一個分類
*
* @param id
*/
@Override
public void deleteType(Long id) {
typeRepository.deleteById(id);
}
/**
* 根據(jù)名字查詢一個分類對象
*
* @param name
* @return
*/
@Override
public Type getTypeByName(String name) {
return typeRepository.findTypeByName(name);
}
/**
* 不帶參數(shù)的查詢所有分類
*
* @return
*/
@Override
public List<Type> listType() {
return typeRepository.findAll();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說明
這篇文章主要介紹了mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java集合框架之List ArrayList LinkedList使用詳解刨析
早在 Java 2 中之前,Java 就提供了特設(shè)類。比如:Dictionary, Vector, Stack, 和 Properties 這些類用來存儲和操作對象組。雖然這些類都非常有用,但是它們?nèi)鄙僖粋€核心的,統(tǒng)一的主題。由于這個原因,使用 Vector 類的方式和使用 Properties 類的方式有著很大不同2021-10-10
Java Web使用POI導(dǎo)出Excel的方法詳解
這篇文章主要介紹了Java Web使用POI導(dǎo)出Excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java Web使用POI導(dǎo)出Excel的具體操作步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
java地理坐標(biāo)系及投影間轉(zhuǎn)換代碼示例
在地圖投影中,經(jīng)常需要將坐標(biāo)從不同的坐標(biāo)系之間進(jìn)行轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于java地理坐標(biāo)系及投影間轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2024-08-08
淺談Spring與SpringMVC父子容器的關(guān)系與初始化
這篇文章主要介紹了淺談Spring與SpringMVC父子容器的關(guān)系與初始化,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
SpringMVC實(shí)現(xiàn)文件上傳下載的全過程
對于上傳功能,我們在項(xiàng)目中是經(jīng)常會用到的,比如用戶注冊的時候,上傳用戶頭像,這個時候就會使用到上傳的功能,而對于下載使用場景也很常見,下面這篇文章主要給大家介紹了關(guān)于SpringMVC實(shí)現(xiàn)文件上傳下載的相關(guān)資料,需要的朋友可以參考下2022-01-01

