Spring boot中使用Mongodb
安装
使用Idea新建Spring boot工程时需要选择Mongodb
或者在工程中加入依赖
Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
Gradle:
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
}
目录结构
配置
在application.yml中配置
spring:
data:
mongodb:
uri: mongodb://test:test@127.0.0.1:27017/test
或者在application.properties配置
spring.data.mongodb.uri=mongodb://test:test@127.0.0.1:27017/test
其中URL的格式是:mongodb://用户名:用户名@ip地址:端口/数据库
使用
在实体类中加入注解
package com.example.demo.domain; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.IndexDirection; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.io.Serializable; /** * 用户模型 * * @author hackyo * Created on 2017/12/3 11:53. */ @Document public class User implements Serializable { @Id private String id; @Indexed(unique = true, direction = IndexDirection.DESCENDING, dropDups = true) private String username; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
@Document表示该类为实体类
@Id表示该字段为自动生成的ID
@Indexed表示自动为该字段建立索引
编写查询接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.example.demo.dao; import com.example.demo.domain.User; /** * 用户表操作接口 * * @author hackyo * Created on 2017/12/3 11:53. */ public interface IUserRepository { /** * 通过用户名查找用户 * * @param username 用户名 * @return 用户信息 */ User findByUsername(String username); } |
查询接口实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.example.demo.dao.impl; import com.example.demo.dao.IUserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Repository; /** * 用户表操作接口实现 * * @author hackyo * Created on 2017/12/3 11:53. */ @Repository public class UserRepositoryImpl implements IUserRepository { private MongoTemplate mongoTemplate; @Autowired public UserRepositoryImpl(MongoTemplate mongoTemplate) { this .mongoTemplate = mongoTemplate; } @Override public User findByUsername(String username) { Query query = new Query(Criteria.where( "username" ).is(username)); return mongoTemplate.findOne(query, User. class ); } } |
测试
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.example.demo; import com.example.demo.dao.IUserRepository; import com.example.demo.domain.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith (SpringRunner. class ) @SpringBootTest public class DemoApplicationTests { @Autowired private IUserRepository userRepository; @Test public void contextLoads() { } @Test public void test() { User user = userRepository.findByUsername( "test" ); System.out.println(user.getId()); } } |
查询出结果即完成
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!