Spring Boot MongoDB
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
1. Linux下启动MongoDB并使用mongosh连接
启动方式有两种:
- systemctl start mongod
- mongod <选项参数>
启动的时候有可能会报类似如下的错误:
ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
mongod.service: Control process exited, code=exited status=14
当看到这种“code=exited, status=14”的时候,一般是文件或目录权限问题,即执行mongodb启动命令的用户对某些文件或目录没有访问权限,此时应检查一些几个文件或目录:
- /var/run/mongodb/
- /var/log/mongodb/
- /var/lib/mongo/
- /tmp
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "film")
public class Film implements Serializable {
@Id
private String id;
private String name;
private List
category; } 然后,利用MongoTemplate进行操作
package com.cjs.example; import com.cjs.example.domain.Film; import com.cjs.example.repository.FilmRepository; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; @SpringBootTest class DemoMongodbApplicationTests { @Resource private MongoTemplate mongoTemplate; @Test void contextLoads() { Film film = new Film(); film.setName("我和我的父辈"); film.setCategory(Arrays.asList("剧情")); mongoTemplate.insert(film); } @Test void testQuery() { List
filmList = mongoTemplate.findAll(Film.class); System.out.println(filmList); Query query = new Query(); query.addCriteria(Criteria.where("name").is("我和我的父辈").and("category").in("爱情","战争","剧情")); Film film = mongoTemplate.findOne(query, Film.class); System.out.println(film); } @Test void testSave() { Film film = new Film(); film.setId("asdfgh"); film.setName("长津湖"); film.setCategory(Arrays.asList("战争","历史")); mongoTemplate.save(film); } } 方式二:Spring Data MongoDB Repositories
定义一个Repository
package com.cjs.example.repository; import com.cjs.example.domain.Film; import org.springframework.data.repository.CrudRepository; import java.util.List; public interface FilmRepository extends CrudRepository
{ List findByName(String name); } 完整测试代码如下:
package com.cjs.example; import com.cjs.example.domain.Film; import com.cjs.example.repository.FilmRepository; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; @SpringBootTest class DemoMongodbApplicationTests { @Resource private MongoTemplate mongoTemplate; @Resource private FilmRepository filmRepository; @Test void contextLoads() { Film film = new Film(); film.setName("我和我的父辈"); film.setCategory(Arrays.asList("剧情")); mongoTemplate.insert(film); } @Test void testQuery() { List
filmList = mongoTemplate.findAll(Film.class); System.out.println(filmList); Query query = new Query(); query.addCriteria(Criteria.where("name").is("我和我的父辈").and("category").in("爱情","战争","剧情")); Film film = mongoTemplate.findOne(query, Film.class); System.out.println(film); } @Test void testSave() { Film film = new Film(); film.setId("asdfgh"); film.setName("长津湖"); film.setCategory(Arrays.asList("战争","历史")); mongoTemplate.save(film); } @Test void testSaveByRepository() { filmRepository.save(new Film("1234", "火影忍者", Arrays.asList("动画","喜剧","动作","冒险"))); } @Test void testQueryByRepository() { Film film = filmRepository.findById("1234").orElse(null); System.out.println(film); film = filmRepository.findByName("火影忍者").get(0); System.out.println(film); filmRepository.findAll().forEach(System.out::println); } } 工程结构如下:
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341