当前位置:网站首页>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
边栏推荐
- 学习光线跟踪一样的自3D表征Ego3RT
- 【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
- Stm32f407 ------- DAC digital to analog conversion
- 详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
- Part IV: STM32 interrupt control programming
- 通过串口实现printf函数,中断实现串口数据接收
- ZYNQ移植uCOSIII
- 深度学习简史(一)
- Attention SLAM:一种从人类注意中学习的视觉单目SLAM
- Advantages and disadvantages of code cloning
猜你喜欢
Mujoco Jacobi - inverse motion - sensor
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
Learn self 3D representation like ray tracing ego3rt
[software reverse automation] complete collection of reverse tools
JS+SVG爱心扩散动画js特效
Dr selection of OSPF configuration for Huawei devices
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
筑梦数字时代,城链科技战略峰会西安站顺利落幕
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
随机推荐
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
第六篇,STM32脉冲宽度调制(PWM)编程
基于SSM框架的文章管理系统
【vulnhub】presidential1
Zynq transplant ucosiii
Stm32f407 ------- DAC digital to analog conversion
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
Interface master v3.9, API low code development tool, build your interface service platform immediately
How engineers treat open source -- the heartfelt words of an old engineer
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
Use mujoco to simulate Cassie robot
Idea automatically imports and deletes package settings
View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
[software reverse automation] complete collection of reverse tools
Set (generic & list & Set & custom sort)
用tkinter做一个简单图形界面
Are you ready to automate continuous deployment in ci/cd?
Distributed cache
stm32F407-------DAC数模转换