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

优质内容筛选与推荐>>
1、转一篇关于Unity的PlayMaker
2、hadoop shell 详解
3、新十大开发小工具
4、关于mysql事务的几件小事
5、Notes of the scrum meeting(12.5)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号