SpringBoot整合JPA資料來源方法及配置解析

2022-09-29 05:15:07 字數 1487 閱讀 6201

一、建立專案並匯入依賴

org.springframework.boot

spring-boot-starter-data-jpa

&lhwptxxvcht;dependency>

com.alibaba

druid-spring-boot-starter

1.1.10

mysql

mysql-connector-j**a

runtime

5.1.27

二、相關配置

application.proteries

spring.datasource.one.type=com.alibaba.druid.pool.druiddatasource

spring.datasource.one.username=root

spring.datasource.one.password=123

spring.datasource.one.url=jdbc:mysql:

spring.datasource.two.type=com.alibaba.druid.pool.druiddhwptxxvchatasource

spring.datasource.two.username=root

spring.datasource.two.password=123

spring.datasource.two.url=jdbc:mysql:

spring.jpa.properties.show-sql=true

spring.jpa.properties.database=mysql

spring.jpa.properties.database-platform=mysql

spring.jpa.properties.hibernawww.cppcns.comte.ddl-auto=upda程式設計客棧te

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.mysql57dialect

datasourceconfig.class  

注:必須指定乙個@primary

jpaconfigone.class

@primary

表示當某乙個類存在多個例項時,優先使用哪個例項。

properties()

jpaproperties是系統提供的乙個例項,裡邊的資料就是我們在application.properties中配置的jpa相關的配置

packages()

這裡的packages指定的包就是這個資料來源對應的實體類所在的位置

persistenceunit()

相當於為這個配置取乙個別名

jpaconfigtwo.class

注:這個沒有@pri

pojo層

dao1和dao2層

controller層

本文標題: springboot整合jpa資料來源方法及配置解析

本文位址:

學習日誌 springboot整合JPA

1.編寫實體類 entity 告訴spring這是乙個實體類 table 之地你和哪個表對映。不寫則預設是user,即實體類名的小寫。指定 name public class user 3.配置檔案編寫 spring datasource driver class name com.mysql.jd...

springboot整合jpa的分頁操作

分頁的工具類 package com.pig.pigbase.tools import org.springframework.data.domain.pagerequest import org.springframework.data.domain.pageable import org.spr...

springboot 整合JPA 主鍵生成方式常用

採用mysql主鍵自增屬性生成 generatedvalue strategy generationtype.identity 指明生成主鍵的方式為mysql的自增主鍵 id column name id generatedvalue strategy generationtype.identity...