springboot-MongoDB

pom.xml

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

top.lrshuai.mongodb
blog
0.0.1-SNAPSHOT
jar

springboot-mongodb
Demo project for Spring Boot

org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE org.springframework.boot spring-boot-starter
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
  </dependency>
  
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
org.springframework.boot spring-boot-maven-plugin poperitis # this is need password auth #spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test

not need auth

spring.data.mongodb.uri=mongodb://192.168.12.133:22222/test
启动
@SpringBootApplication()
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
dao
public interface UserDao {
public void saveUser(User user);
public void saveBathUser(List users);
public void delUserById(Long id);
public int upadteUserById(User user);
public User findUserByName(String name);
public List findAll();
public List findUserByLikeName(String name);

}
package top.lrshuai.mongodb.dao.impl;

import java.util.List;

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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.mongodb.WriteResult;

import top.lrshuai.mongodb.dao.UserDao;
import top.lrshuai.mongodb.entity.User;

@Component
public class UserDaoImpl implements UserDao{

@Autowired
private MongoTemplate mongoTemplate;

/**
* 新增一个,存在则覆盖
*/
@Override
public void saveUser(User user) {
mongoTemplate.save(user);
}

/**
* 批量新增
*/
@Override
public void saveBathUser(List users) {
mongoTemplate.insert(users, User.class);
}

/**
* 删除
*/
@Override
public void delUserById(Long id) {
Query query = new Query(Criteria.where(“id”).is(id));
mongoTemplate.remove(query, User.class);
}

/**
* 更新 通过id
*/
@Override
public int upadteUserById(User user) {
Query query = new Query(Criteria.where(“id”).is(user.getId()));
Update update = new Update();
update.set(“name”, user.getName()).set(“age”, user.getAge());
WriteResult result = mongoTemplate.updateFirst(query, update, User.class);
return result.getN();
}

/**
* 通过名称查找
*/
@Override
public User findUserByName(String name) {
Query query = new Query(Criteria.where(“name”).is(name));
return mongoTemplate.findOne(query, User.class);
}

/**
* 查询所有
*/
@Override
public List findAll() {
return mongoTemplate.findAll(User.class);
}

/**
* name模糊查找
/
@Override
public List findUserByLikeName(String name) {
Query query = new Query();
query.addCriteria(Criteria.where(“name”).regex(".
" +name+ “.*”));
return mongoTemplate.find(query, User.class);
}

}

实体类
package top.lrshuai.mongodb.entity;

import org.springframework.data.annotation.Id;

public class User {
@Id
private Long id;

private String name;
private Integer age;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Long id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return “User [id=” + id + “, name=” + name + “, age=” + age + “]”;
}

}

package top.lrshuai.mongodb.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Component;

import top.lrshuai.mongodb.entity.User;

@Component
public interface UserRepository extends MongoRepository<User, Long>{
public User findUserByName(String username);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值