1.添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<jwt.version>0.7.0</jwt.version>
<fastjson.version>1.2.60</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
2.添加配置
spring:
data:
mongodb:
username: user01
password: '123456'
port: 27017
host: 192.168.1.101
database: dz14b
mongodb配置有两种方式,如果第一种不生效,可以用第二种尝试一下
spring:
data:
mongodb:
uri: mongodb://user01:123456@192.168.1.101:27017/dz12b
3.编写Entry
import lombok.Data;
import org.springframework.data.annotation.Id;
@Data
public class User {
@Id
private String id;
private String name;
private String password;
}
4.编写DAO
import org.jsoft.demo.vo.User;
import java.util.List;
public interface IUserDao {
/**
* 新增
*
* @param user 目标对象
*/
void insert(User user);
/**
* 删除
*
* @param id 对象主键
*/
void delete(String id);
/**
* 修改
*
* @param user 目标对象
*/
void update(User user);
/**
* 通过主键查询
*
* @param id 主键
* @return user对象
*/
User findById(String id);
/**
* 查询
*
* @param user 条件对象
* @return user集合
*/
List<User> query(User user);
}
5.编写DAOImpl
MongoTemplate常用方法:
mongoTemplate.insert(user); //添加user对象
mongoTemplate.findById(id, User.class); //根据主键id,查询单个user对象。
mongoTemplate.remove(user); //删除指定user对象
mongoTemplate.remove(条件对象Query, User.class); // 删除符合条件的user对象
mongoTemplate.updateMulti(条件对象query, 修改对象update, User.class); //修改符合条件的user对象
mongoTemplate.find(条件对象query, User.class); //查询符合条件的user对象
条件对象Query常用方法:
#将构建好的查询条件添加到查询对象中
query.addCriteria(Criteria.where("_id").is(id));
Criteria常用方法:
#用于定义查询条件的接口,它提供了一系列的方法来构建复杂的查询条件。
Criteria.where("_id").is(id);
修改对象Update常用方法:
update.set("password", user.getPassword());
测试代码:
import org.jsoft.demo.dao.IUserDao;
import org.jsoft.demo.vo.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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDaoImpl implements IUserDao {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void insert(User user) {
mongoTemplate.insert(user);
}
@Override
public User findById(String id) {
return mongoTemplate.findById(id, User.class);
}
// @Override
public void delete1(String id) {
User user = findById(id);
if (user != null) {
mongoTemplate.remove(user);
} else {
System.out.println("没有找到要删除的数据!");
}
}
@Override
public void delete(String id) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
mongoTemplate.remove(query, User.class);
}
@Override
public void update(User user) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(user.getId()));
Update update = new Update();
update.set("name", user.getName());
update.set("password", user.getPassword());
mongoTemplate.updateMulti(query, update, User.class);
}
@Override
public List<User> query(User user) {
Query query = new Query();
if(user.getId() != null){
query.addCriteria(Criteria.where("_id").is(user.getId()));
}
if(user.getName() != null){
query.addCriteria(Criteria.where("name").is(user.getName()));
}
if(user.getPassword() != null){
query.addCriteria(Criteria.where("password").is(user.getPassword()));
}
return mongoTemplate.find(query, User.class);
}
}
6.编写测试类Test
import lombok.extern.slf4j.Slf4j;
import org.jsoft.demo.dao.IUserDao;
import org.jsoft.demo.vo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@Slf4j
@SpringBootTest(classes = DemoHelloworldApplication.class)
@SuppressWarnings("all")
public class MongoDbTest {
@Autowired
private IUserDao userDao;
@Test
public void testInsert() {
User user = new User();
user.setPassword("123");
user.setName("zhangsan");
userDao.insert(user);
}
@Test
public void testFindById() {
User byId = userDao.findById("661794efd775ee4bd7a6d069");
System.out.println(byId);
}
@Test
public void testDelete() {
userDao.delete("6617970547fbc57ef66a962f");
}
@Test
public void testUpdate() {
User user = new User();
user.setId("661797ee479a0c02f0dd9212");
user.setPassword("123456");
user.setName("lisi");
userDao.update(user);
}
@Test
public void testQuery() {
User user = new User();
// user.setId("661797ee479a0c02f0dd9212");
user.setPassword("123");
// user.setName("lisi");
System.out.println(userDao.query(user));
}
}
7.编写启动类
package com.jr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class);
}
}
user.setName("lisi");
userDao.update(user);
}
@Test
public void testQuery() {
User user = new User();
// user.setId(“661797ee479a0c02f0dd9212”);
user.setPassword(“123”);
// user.setName(“lisi”);
System.out.println(userDao.query(user));
}
}
## 7.编写启动类
package com.jr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class);
}
}