spring data mongodb 配置遇到的幾個問題


一. mongodb 2.2版本以上的配置

spring.data.mongodb.uri = mongodb://newlook:newlook@192.168.0.109:27017/admin

  2.2及以下版本的配置

  #spring.data.mongodb.host = 192.168.0.109
  #spring.data.mongodb.username = newlook
  #spring.data.mongodb.password = newlook
  #spring.data.mongodb.defaultDB = admin
  #spring.data.mongodb.port = 27017

二. Configuration 

@Bean
PlatformTransactionManager platformTransactionManager() {
return new org.springframework.integration.transaction.PseudoTransactionManager();
}

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>

開始注入了其他的類型的TransactionManager,導致maven中自動引入了jpa相關的依賴,報錯如下:

Caused by: java.lang.IllegalStateException: Cannot load driver class: mongodb.jdbc.MongoDriver

原因:

You seem to be trying to mix JPA, which is primarily intended for relational datastores, with MongoDB, which is an "unrelated" document store. Drop the dependency on spring-boot-starter-data-jpa (you simply don't need it) and the spring.datasource.driver-class-name (you should use MongoDB natively, not via a JDBC bridge).

三.org.springframework.dao.OptimisticLockingFailureException: Optimistic lock exception on saving entity

原因:版本控制的類型須為包裝類型, 

@Version
private int version;

改為:

@Version
private Integer version;

四.數據保存時候id總是empty

原因:ui中id提交時為“”,

<input th:field="*{id}" type="hidden"/>

改為:

<input th:field="*{id}" type="hidden" th:disabled="*{id==null}"/>

這樣id提交后為null,可自動生成id

 

 

相關文檔

http://docs.spring.io/spring-data/data-mongo/docs/1.9.2.RELEASE/reference/html/#query-by-example

https://docs.mongodb.com/manual/reference/command/

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#a-menu-for-our-home-page


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM