当前位置:网站首页>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
边栏推荐
- How engineers treat open source -- the heartfelt words of an old engineer
- Use mujoco to simulate Cassie robot
- Five different code similarity detection and the development trend of code similarity detection
- [software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving
- Basic information of mujoco
- Explain in detail the implementation of call, apply and bind in JS (source code implementation)
- Attention SLAM:一种从人类注意中学习的视觉单目SLAM
- 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
- How to get started and improve test development?
- C9 colleges and universities, doctoral students make a statement of nature!
猜你喜欢
Js+svg love diffusion animation JS special effects
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
VTK volume rendering program design of 3D scanned volume data
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Summary of being a microservice R & D Engineer in the past year
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
用tkinter做一个简单图形界面
Configuring OSPF basic functions for Huawei devices
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
随机推荐
Attention slam: a visual monocular slam that learns from human attention
OSPF configuration command of Huawei equipment
Interface (interface related meaning, different abstract classes, interface callback)
Cross-entrpy Method
Matlab learning notes
Learning notes 5: ram and ROM
批量获取中国所有行政区域经边界纬度坐标(到县区级别)
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Three methods to realize JS asynchronous loading
STM32开发资料链接分享
Stm32f407 ------- SPI communication
第六篇,STM32脉冲宽度调制(PWM)编程
Advanced learning of MySQL -- basics -- transactions
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Chapter II proxy and cookies of urllib Library
Mujoco Jacobi - inverse motion - sensor
Advantages and disadvantages of code cloning
筑梦数字时代,城链科技战略峰会西安站顺利落幕
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS