spring集成mongodb
程序员文章站
2022-04-30 20:13:12
...
public class Country implements Serializable {
//实体类参考本人博客地址:https://blog.csdn.net/yl_hahha/article/details/80208161\
@Id
private String id;
private String countryname;
private String countrycode;
public Country() {
}
//idea 生成构造器的快捷键 alt+insert
public Country(String countryname, String countrycode) {
this.countryname = countryname;
this.countrycode = countrycode;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
@Override
public String toString() {
return "Country [id=" + id + ", countryname=" + countryname + ", countrycode=" + countrycode + "]";
}
}
``
public interface CountryRepository extends MongoRepository<Country,String> {
public Country findByCountrycode(String firstName);
public List<Country> findByCountryname(String lastName);
}
@SpringBootApplication
public class DemoApplication implements CommandLineRunner{
@Autowired
private CountryRepository countryRepository;
//mongodb 下载地址 :http://dl.mongodb.org/dl/win32/x86_64
//配置 mongodb:http://www.runoob.com/mongodb/mongodb-window-install.html
/**
* com.mongodb.MongoSocketOpenException: Exception opening socket
* 确定服务是否启动成功 浏览器访问url localhost
* E11000 duplicate key error collection: springboot-db.country index: _id_ dup key: { : 0 }
* 加注解@id
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
countryRepository.deleteAll();
countryRepository.save(new Country("bj","bj"));
countryRepository.save(new Country("bj1","bj2"));
List<Country> all = countryRepository.findAll();
for (Country c:all){
System.out.print(c);
}
for (Country b : countryRepository.findByCountryname("bj")) {
System.out.println(b);
}
}
}
“`项目地址,点击
上一篇: 我在一家私人承包公司干了三年
下一篇: 文件上传和下载
推荐阅读
-
SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
-
Spring-Cloud-Filter(过滤器)
-
使用android studio打jar包并且集成第三方开源库(修改补充篇)
-
spring依赖注入
-
Nodejs使用Mongodb存储与提供后端CRD服务详解
-
Mac OS X下PhpStorm+MAMP PRO+Xdebug+FireFox集成开发和断点调试环境配置
-
bootstrap+spring mvc+ibatis 实现增删改查
-
gRPC在Spring Cloud中的应用
-
SpringBoot轻松整合MongoDB的全过程记录
-
【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)(二