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

转载于:https://www.cnblogs.com/ly-radiata/p/5881472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值