Java工作笔记-JPA查询达梦7数据库(Spring Boot + ORM)
生活随笔
收集整理的這篇文章主要介紹了
Java工作笔记-JPA查询达梦7数据库(Spring Boot + ORM)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
基本概念
代碼與實例
存在的坑
?
?
基本概念
使用JPA可以簡化編程,并且可以不變代碼,直接換其他數據庫,比如,現在要把數據庫換成達夢的。
首先把達夢安裝好:
這里可以看到方言什么都有,現在來看看那個Hibernate....txt那個文件:
這里我選擇了Dm7JdbcDriver17.jar方言選擇DmDialect-for-hibernate5.3.jar
這里加載有2種方式,一種是配置一個本地的Maven庫,一種是直接從文件中加載。
這里為了簡單,直接從配置本地:
maven配置如下:
<dependency><groupId>com.dm</groupId><artifactId>jdbc</artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/Dm7JdbcDriver17.jar</systemPath></dependency><dependency><groupId>com.dm</groupId><artifactId>hibernate5</artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/DmDialect-for-hibernate5.3.jar</systemPath></dependency>這樣庫就搞好了!
?
代碼與實例
達夢庫截圖如下:
程序運行截圖如下:
程序結構如下:
PolicyContexts.java
package jpadmjdbc.demo.object;import lombok.Data;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;@Data @Entity @Table(name = "POLICY_GROUPS") public class PolicyContexts {@Id@Column(name = "OBJID")private Integer id;@Column(name = "NAME")private String name; }PolicyContextsRepository
package jpadmjdbc.demo.repository;import jpadmjdbc.demo.object.PolicyContexts; import org.springframework.data.jpa.repository.JpaRepository;public interface PolicyContextsRepository extends JpaRepository<PolicyContexts, Integer> {}application.properties
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver spring.datasource.url=jdbc:dm://127.0.0.1/TESTHEHE?characterEncoding=utf-8&useSSL=false spring.datasource.username=SYSDBA spring.datasource.password=SYSDBA#spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DmDialect單例調試:
package jpadmjdbc.demo.repository;import jpadmjdbc.demo.object.PolicyContexts; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;import static org.junit.Assert.*;@RunWith(SpringRunner.class) @SpringBootTest public class PolicyContextsRepositoryTest {@AutowiredPolicyContextsRepository repository;@Testpublic void all(){List<PolicyContexts> all = repository.findAll();System.out.println(all);Assert.assertNotEquals(all.size(), 0);} }?
存在的坑
這里版本一定要對應采用的是如下JPA版本:
點進去看看其Hibernate版本:
這里要選對達夢的版本,這是最重要的!
總結
以上是生活随笔為你收集整理的Java工作笔记-JPA查询达梦7数据库(Spring Boot + ORM)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-使用RabbitMQ的Ja
- 下一篇: QML笔记-QML中SpriteSequ