Gorm for mongo在Spring boot中的使用

Spring boot 中使用Gorm for Mongodb

在之前的Grails项目中使用Grails的ORM发现很好用,目前可以在spring boot中使用这个插件了.
在build.gradle中

	compile "org.mongodb:mongodb-driver:3.4.2"
	compile("org.grails:gorm-mongodb-spring-boot:6.1.2.RELEASE")
	compile "org.grails:grails-datastore-gorm-mongodb:6.1.2.RELEASE"
	compile 'org.codehaus.groovy:groovy-all:2.4.10'

然后在spring boot 的启动类中

@SpringBootApplication
@ComponentScan		//这里要加才能将Entity全部找出来
class MqApplication {

	static void main(String[] args) {
		SpringApplication.run MqApplication, args
	}
}

对于我们需要进行操作的Domain,可以进行如下的配置


import grails.gorm.annotation.Entity

@Entity
class GeoPoint {

    Double lat
    Double lon

    static constraints = {
        lat nullable: true
        lon nullable: true
    }

    static mapping = {
        // Used to disable optimistic locking
        version false
    }
}

这样就能在其他的地方自如的使用GORM了.

在实际使用中发现了一个bug(自己的问题)
由于在类中引入上个GeoPoint,但是GeoPoint自身没有填写@Entity,导致在更新的时候,不能更新这个字段,也就是说如果要保持一些embed的类也能保持更新,还是要在被包含的类中添加@Entity标识.

谨以此博客,记录犯下的错误,避免在以后的工作中再次遇到.

猜你喜欢

转载自blog.csdn.net/shsongtao/article/details/83546374