spring cloud初学者-mongodb

上一篇博客我们学习了如何使用jpa,这一片博客我们来学习如何用springboot的jpa,这篇博客我们看看如何如何使用类似于jpa的方法来操作mongoDb,尽管他是非结构化数据库,但是我们还是可以通过这种简单明了的方式来进行增删改查的方法。
我们完全可以吧上一篇博客的工程那俩使用,只需要添加如下的配置在pom文件中。

pom文件

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.10.6.RELEASE</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>spring-libs-release</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

查询方法

查询方法和上一篇博客的一样,我们还是老一套命名方法。当然接下来的例子我不会每个都示范,但是我们示范几个

关键字 示例 同功能的查询
And findByTitleAndContent where x.title = ?1 and x.content = ?2
Or findByTitleOrContent where x.title = ?1 or x.content = ?2
Is, Equals findByTitle,findByTitleIs,FindByTItleEquals where x.title = ?1
Between findByCreateTimeBetween where x.createTime between 1? and ?2
LessThanEqual findByAgeLessThanEqual where x.age <= ?1
LessThan findByAgeLessThan where x.age < ?1
GreaterThan findByAgeGreaterThan where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual where x.age >= ?1
After findByCreateTimeAfter where x.create > ?1
Before findByCreateTimeBefore where x.createTime < ?1
IsNull findByAgeIsNull where x.age is null
IsNotNull NotNull findByAgeIsNotNull, findByAgeNotNull where x.age is not null
Like findByTitleLike where x.title like ?1
NotLike findByTitleNotLike where x.title not like ?1
StartingWith findByTitleStartingWith where x.title like ?1(title前加%)
EndingWith findByTitleEndingWith where x.title like ?1(title后加%)
Containing findByTitleContaining where x.title like ?1(前后都加)
OrderBy findByTitleOrderByIdDesc where x.title order by id desc
Not findByTitleNot where title <> ?1
In findByAgeIn(Collection ages) where x.age in ?1
NotIn findByAgeNotIn(Collection ages) where x.age not in ?1
True findByActiveTrue where x.active = true
False findByActiveFalse where x.active = false
ignoreCase findByTitleIgnoreCase where UPPER(x.title) = UPPER(?1)

方法使用

springboot会自动给我们分解关键字。

public interface PersonRepository extends Repository<User, Long> {

  List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);

  // Enables the distinct flag for the query
  List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
  List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);

  // Enabling ignoring case for an individual property
  List<Person> findByLastnameIgnoreCase(String lastname);
  // Enabling ignoring case for all suitable properties
  List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);

  // Enabling static ORDER BY for a query
  List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
  List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}

mongdb配置

#ip
spring.data.MongoDB.host=localhost
#端口
spring.data.mongodb.port=27018
数据库名称
spring.data.mongodb.database=test

mongodb安装

我使用的是docker安装的Mongo,非常的简单,当然是前提你已经安装了docker,否则就按照常规方法安装就可以了。docker安装只需要执行一句话,对外暴露27018的端口就可以了。
docker run --name some-mongo -d -p 27018:27017 mongo
按照上一篇的博客,我们写一些rest接口就可以了。

发布了176 篇原创文章 · 获赞 84 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/lovemenghaibin/article/details/76566977