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
<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);
}