当前位置:网站首页>Mongodb client operation (mongorepository)
Mongodb client operation (mongorepository)
2022-07-07 00:55:00 【white_ while】
orm
jpa
MongoRepository
springboot Integrate
- rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
- springboot Scan the annotation
@EnableMongoRepositories(
basePackages = {"com.jl.ability.*"},
repositoryImplementationPostfix = "Impl")
repositoryImplementationPostfix The default is Impl suffix , You can specify the suffix of the implementation class of the user-defined warehouse layer interface
Such as mongodb Its CustomizedUserRepository Specify the implementation class customizedUserRepositoryImpl
- Define interfaces, user-defined interfaces and implementation classes
interface CustomizedUserRepository {
void someCustomMethod(User user);
}
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
@Repository
interface UserRepository extends CrudRepository<User, Long>, CustomizedUserRepository {
// Declare query methods here
}
curd Basic operation
Entity class
@Data
@Document(collection = "balloon_msg")
public class BalloonMsg {
@Id
private Long id;
private String sid;
private String body;
private Long from;
private int sex;
private Date ctime;
private String accid;
private Integer revNum;
private Integer replyNum;
}
Interface
@Repository
public interface BalloonMsgMapper extends MongoRepository<BalloonMsg, Long>, CustomizedBalloonMsgMapper {
List<BalloonMsg> findByFromOrderByIdDesc(Long from, Pageable pageable);
@Query(value="{'$and': [{'from':?0}, {'sid':?1}]}", fields="{'_id': 1}")
List<BalloonMsg> findIdsByFromAndSid(Long from, String sid);
}
Be careful :
MongoRepository<BalloonMsg, Long>
BalloonMsg Mapping entity classes for collections ,Long Primary key id type
Basic operation
Support save,insert,find,delete
Query extensions support native query statements
similar sql:select id from xxx where `from` = from and sid = sid
@Query(value="{'$and': [{'from':?0}, {'sid':?1}]}", fields="{'_id': 1}")
List<BalloonMsg> findIdsByFromAndSid(Long from, String sid);
Support jpa Specification method
similar sql:select * from balloon_msg where `from` = from order by id desc limit x, x
List<BalloonMsg> findByFromOrderByIdDesc(Long from, Pageable pageable);
Custom extension
public interface CustomizedBalloonMsgMapper {
int incRevNumById(long id, int num);
int incReplyNumById(long id, int num);
}
public class CustomizedBalloonMsgMapperImpl implements CustomizedBalloonMsgMapper {
@Resource
private MongoTemplate mongoTemplate;
@Override
public int incRevNumById(long id, int num) {
Query query = Query.query(Criteria.where("_id").is(id));
Update update = new Update();
update.inc("revNum");
UpdateResult res = mongoTemplate.updateFirst(query, update, BalloonMsg.class);
return (int) res.getModifiedCount();
}
@Override
public int incReplyNumById(long id, int num) {
Query query = Query.query(Criteria.where("_id").is(id));
Update update = new Update();
update.inc("replyNum");
return (int) mongoTemplate.updateFirst(query, update, BalloonMsg.class).getModifiedCount();
}
}
At this time BalloonMsgMapper The interface contains basic curd And custom extended method sets
@Repository
public interface BalloonMsgMapper extends MongoRepository<BalloonMsg, Long>, CustomizedBalloonMsgMapper{
...
}
Be careful :
- General operation use Spring Data Repositories Its MongoRepository
- Custom extensions use MongoTemplate, Access through the user-defined extension interface
边栏推荐
- 如何判断一个数组中的元素包含一个对象的所有属性值
- Slow database query optimization
- X.509 certificate based on go language
- AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
- Installation and testing of pyflink
- 【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解
- 代码克隆的优缺点
- Distributed cache
- 新手如何入门学习PostgreSQL?
- JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
猜你喜欢

Dr selection of OSPF configuration for Huawei devices
![Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax](/img/87/3fee9e6f687b0c3efe7208a25f07f1.png)
Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax

随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP

Stm32f407 ------- SPI communication

详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码

ZYNQ移植uCOSIII

学习使用代码生成美观的接口文档!!!

5种不同的代码相似性检测,以及代码相似性检测的发展趋势

VTK volume rendering program design of 3D scanned volume data

【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
随机推荐
Linear algebra of deep learning
build. How to configure the dependent version number in the gradle file
[user defined type] structure, union, enumeration
. Bytecode structure of class file
Telerik UI 2022 R2 SP1 Retail-Not Crack
学习光线跟踪一样的自3D表征Ego3RT
浅谈测试开发怎么入门,如何提升?
What is time
Are you ready to automate continuous deployment in ci/cd?
代码克隆的优缺点
Deep understanding of distributed cache design
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
dynamic programming
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
Cross-entrpy Method
Common shortcuts to idea
在jupyter中实现实时协同是一种什么体验
【软件逆向-自动化】逆向工具大全
Advanced learning of MySQL -- basics -- multi table query -- inner join
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt