Springboot整合jpa实现mysql&oracle
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application
#spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
#spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = url地址
spring.datasource.username = 用户名
spring.datasource.password = 密码
entity
@Entity
@Table(name = "SECURITY_ULTRA_USER")
public class UltraUser {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private Long id;
@Column(name = "USER_NAME")
private String name;
@Column(name = "ACCOUNT")
private String userAccount;
@Column(name = "PASS")
private String pass;
}
JpaRepository
public interface UltraUserRepository extends JpaRepository<UltraUser, Long> {
}
测试
List<UltraUser> all = ultraUserRepository.findAll();
多表关联查询
interface
直接写成接口的形式,而不像上面写成entity
public interface UltraOrgan {
String getDEPT_ID();
String getDEPT_NAME();
String getSUPER_ID();
String getNOTE();
Integer getSTATE();
String getGROUP_DNID();
String getORG_DNNAME();
Integer getTYPE();
String getAPP_NAME();
String getORG_PHONE();
String getORG_FAX();
Integer getORG_ORDERBY();
Integer getSYS_GROUP();
Integer getORG_STATUS();
String getDEPT_TYPE();
String getEMAIL();
String getCOUNTRY_ID();
String getPROVINCE_ID();
String getREGION_ID();
}
JpaRepository
public interface UltraOrganizationRepository extends JpaRepository<UltraOrganization, String> {
@Query(value="select o.DEPT_ID,o.DEPT_NAME,o.SUPER_ID,o.NOTE,o.STATE,o.GROUP_DNID,o.ORG_DNNAME,o.TYPE,o.APP_NAME,o.ORG_PHONE,o.ORG_FAX,o.ORG_ORDERBY,o.SYS_GROUP,o.ORG_STATUS,d.DEPT_TYPE,d.EMAIL,d.COUNTRY_ID,d.PROVINCE_ID,d.REGION_ID from SECURITY_ORGANIZATION o left join SECURITY_DEPARTMENT d on o.DEPT_ID=d.ID",nativeQuery = true)
List<UltraOrgan> findOrganDept();
}
把查出来的数据直接封装进UltraOrgan里面
测试
List<UltraOrgan> organDept = ultraOrganizationRepository.findOrganDept();