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>

查询方法

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

关键字示例同功能的查询
AndfindByTitleAndContentwhere x.title = ?1 and x.content = ?2
OrfindByTitleOrContentwhere x.title = ?1 or x.content = ?2
Is, EqualsfindByTitle,findByTitleIs,FindByTItleEqualswhere x.title = ?1
BetweenfindByCreateTimeBetweenwhere x.createTime between 1? and ?2
LessThanEqualfindByAgeLessThanEqualwhere x.age <= ?1
LessThanfindByAgeLessThanwhere x.age < ?1
GreaterThanfindByAgeGreaterThanwhere x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqualwhere x.age >= ?1
AfterfindByCreateTimeAfterwhere x.create > ?1
BeforefindByCreateTimeBeforewhere x.createTime < ?1
IsNullfindByAgeIsNullwhere x.age is null
IsNotNull NotNullfindByAgeIsNotNull, findByAgeNotNullwhere x.age is not null
LikefindByTitleLikewhere x.title like ?1
NotLikefindByTitleNotLikewhere x.title not like ?1
StartingWithfindByTitleStartingWithwhere x.title like ?1(title前加%)
EndingWithfindByTitleEndingWithwhere x.title like ?1(title后加%)
ContainingfindByTitleContainingwhere x.title like ?1(前后都加)
OrderByfindByTitleOrderByIdDescwhere x.title order by id desc
NotfindByTitleNotwhere title <> ?1
InfindByAgeIn(Collection ages)where x.age in ?1
NotInfindByAgeNotIn(Collection ages)where x.age not in ?1
TruefindByActiveTruewhere x.active = true
FalsefindByActiveFalsewhere x.active = false
ignoreCasefindByTitleIgnoreCasewhere 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接口就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值