当前位置:网站首页>mongodb客户端操作(MongoRepository)
mongodb客户端操作(MongoRepository)
2022-07-06 17:01:00 【white_while】
orm
jpa
MongoRepository
springboot 整合
- 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
- springboot扫描注解
@EnableMongoRepositories(
basePackages = {"com.jl.ability.*"},
repositoryImplementationPostfix = "Impl")
repositoryImplementationPostfix 默认就是 Impl 后缀,可指定自定义仓储层接口实现类后缀
如mongodb其CustomizedUserRepository指定实现类customizedUserRepositoryImpl
- 定义接口和自定义接口及实现类
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基本操作
实体类
@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;
}
接口
@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);
}
注意:
MongoRepository<BalloonMsg, Long>
BalloonMsg为集合映射实体类,Long为主键id类型
基本操作
支持 save,insert,find,delete
查询扩展支持原生查询语句
类似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);
支持jpa规范方法
类似sql:select * from balloon_msg where `from` = from order by id desc limit x, x
List<BalloonMsg> findByFromOrderByIdDesc(Long from, Pageable pageable);
自定义扩展
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();
}
}
此时的 BalloonMsgMapper 接口包含基本curd和自定义扩展的方法集
@Repository
public interface BalloonMsgMapper extends MongoRepository<BalloonMsg, Long>, CustomizedBalloonMsgMapper{
...
}
注意:
- 通用操作使用 Spring Data Repositories 其 MongoRepository
- 自定义扩展使用 MongoTemplate,通过自定义扩展接口接入
边栏推荐
- 【JokerのZYNQ7020】AXI_ EMC。
- 5种不同的代码相似性检测,以及代码相似性检测的发展趋势
- Attention slam: a visual monocular slam that learns from human attention
- Use mujoco to simulate Cassie robot
- 【vulnhub】presidential1
- Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
- Leecode brushes questions to record interview questions 17.16 massagist
- Attention SLAM:一種從人類注意中學習的視覺單目SLAM
- Explain in detail the implementation of call, apply and bind in JS (source code implementation)
- 【JokerのZYNQ7020】AXI_EMC。
猜你喜欢

准备好在CI/CD中自动化持续部署了吗?

JS+SVG爱心扩散动画js特效

. Bytecode structure of class file

Equals() and hashcode()

Mujoco finite state machine and trajectory tracking

Interface master v3.9, API low code development tool, build your interface service platform immediately

Threejs image deformation enlarge full screen animation JS special effect

Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database

509 certificat basé sur Go

英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
随机推荐
Advanced learning of MySQL -- basics -- transactions
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
threejs图片变形放大全屏动画js特效
2021 SASE integration strategic roadmap (I)
Mujoco Jacobi - inverse motion - sensor
以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
How engineers treat open source -- the heartfelt words of an old engineer
Sword finger offer 26 Substructure of tree
Rails 4 asset pipeline vendor asset images are not precompiled
After leaving a foreign company, I know what respect and compliance are
【软件逆向-自动化】逆向工具大全
Encryption algorithm - password security
深度学习之环境配置 jupyter notebook
What can the interactive slide screen demonstration bring to the enterprise exhibition hall
Article management system based on SSM framework
Leecode brushes questions to record interview questions 17.16 massagist
Quaternion attitude calculation of madgwick
Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
Stm32f407 ------- DAC digital to analog conversion
Advanced learning of MySQL -- basics -- multi table query -- inner join