starter-data-mongodb

发布于:2024-12-22 ⋅ 阅读:(10) ⋅ 点赞:(0)

pom依赖

<!--mongo依赖-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-mongodb</artifactId>
 </dependency>

 <!--测试依赖-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
 </dependency>

mongodb yml连接信息

spring:
  data:
    mongodb:
      uri: mongodb://admin:admin@192.168.98.128:27017/articledb?authSource=admin

springboot主方法

不需要排除mongodb,使用spring自带的mongo连接

@SpringBootApplication
@Slf4j
public class SpringbootApplication {
    public static void main(String[] args) {  SpringApplication.run(SpringbootApplication.class, args);
    }
}

mongo实体类

对应具体mongo collection

@Document(collection  = "BalanceTest") //集合名称
@Data
public class BalanceEntity implements Serializable {
    //主键标识,该属性的值会自动对应MongoDB的主键字段_id,如果该属性名就叫"id"则该注解可以省略,否则必须写
    @Id //主键
    private String id;
    //该属性对应MongoDB字段的名字,如果一致,则无需该注解
    @Field("articleid")
    private int articleId;
    private String content;
    private Date createTime;
}

dao层

MongoRepository<T, ID>中ID为主键类型

//MongoRepository<T, ID>中ID为主键类型
public interface BalanceRepository extends MongoRepository<BalanceEntity, String> {

    //ArticleId为需要查询的字段
    Page<BalanceEntity> findByArticleId(String parentid,Pageable pageable);
}

service层

调用dao层或者使用mongoTemplate方法

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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.Service;
@Service
public class BalanceService {
    @Autowired
    private BalanceRepository balanceRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveBalance(BalanceEntity balanceEntity) {
        balanceRepository.save(balanceEntity);
    }

    public void updateBalance(BalanceEntity balanceEntity) {
        balanceRepository.save(balanceEntity);
    }

    /**
     * 根据id删除评论
     */
    public void deleteCommentById(String id){
        //调用dao
        balanceRepository.deleteById(id);
    }

    /**
     * 查询所有评论
     */
    public List<BalanceEntity> findCommentList(){
        //调用dao
        return balanceRepository.findAll();
    }

    /**
     * 根据id查询评论
     */
    public BalanceEntity findCommentById(String id){
        //调用dao
        return balanceRepository.findById(id).get();
    }

    /**
     * 分页条件查询
     */
    public Page<BalanceEntity> findByArticleId(String articleId, int page, int size) {
        return balanceRepository.findByArticleId(articleId,PageRequest.of(page-1,size));
    }

    /**
     * 自增更新
     */
    public void updateIncrementArticleId(String id){
        //  查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        //  更新条件
        Update update = new Update();
        //需要自增的字段
        update.inc("articleId");
        mongoTemplate.updateFirst(query,update,BalanceEntity.class);
    }
}

测试类,调用server层

@RunWith(SpringRunner.class)
@SpringBootTest
public class BalanceServiceTest {

    @Autowired
    private BalanceService balanceService;

    @Test
    public void testFindCommentList() {
        List<BalanceEntity> commentList = balanceService.findCommentList();
        System.out.println(commentList);
    }

    @Test
    public void testFindCommentById() {
        BalanceEntity commentById = balanceService.findCommentById("672f82baba8fda7d061e2097");
        System.out.println(commentById);
    }

    @Test
    public void testSaveComment(){
        BalanceEntity balanceEntity =new BalanceEntity();
        balanceEntity.setContent("测试添加的数据");
        balanceEntity.setCreateTime(new Date());
        balanceEntity.setArticleId(1003);

        balanceService.saveBalance(balanceEntity);

    }

    @Test
    public void findByArticleId() {
        Page<BalanceEntity> page = balanceService.findByArticleId("3", 1, 2);
        System.out.println(page.getTotalElements());
        System.out.println(page.getContent());
    }

    @Test
    public void updateIncrementArticleId() {
        balanceService.updateIncrementArticleId("67657f30c1e6c51645a0be55");
    }
}