JeecgBoot与MongoDB

1. 坐标引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2. 增加配置类

package org.jeecg.config;

import com.mongodb.client.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class MongoConfig {

    @Bean
    public MongoClientFactoryBean mongoClientFactoryBean() {
        MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
        factoryBean.setHost("127.0.0.1");
        factoryBean.setPort(27017);
        return factoryBean;
    }
    @Bean
    public MongoTemplate mongoTemplate(MongoClient mongoClient) {
        return new MongoTemplate(mongoClient, "springboot-db");
    }
}

说明:新建springboot工程,引入坐标,然后在application.properties文件中加入

spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db

springboot就能支持MongoRepository和MongoTemplate读写MongoDb,测试很顺利。

但是将springboot工程中的代码加入到jeecgboot中,运行后一直提示错误

用这个配置类,代替yml中的mongodb配置可解决该错误。

3,升级积木报表

针对上面提到的mongoTemplate错误,官方已经提供了最新的解决方案,将积木报表升级。这样就可以不用配置类。

<dependency>
    <groupId>org.jeecgframework.jimureport</groupId>
    <artifactId>jimureport-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>

之后在application-dev.yml文件中,加入mongoDb的配置项

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/springboot-db

4. 创建实体类

package org.jeecg.modules.mongodb.entity;

import org.springframework.data.annotation.Id;


public class Customer {

    @Id
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

5. 创建Repository

package org.jeecg.modules.mongodb.dao;

import org.jeecg.modules.mongodb.entity.Customer;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface CustomerRepository extends MongoRepository<Customer, String> {

     Customer findByFirstName(String firstName);
     List<Customer> findByLastName(String lastName);

}

6. 测试用例

用两种方式测试mongoDB,分别为MongoRepository和MongoTemplate

package org.jeecg.modules.mongodb;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.mongodb.dao.CustomerRepository;
import org.jeecg.modules.mongodb.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 测试mongodb
 */
@RestController
@RequestMapping("/mongo")
public class MongoController {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private CustomerRepository repository;

    @GetMapping("/test1")
    public Result<?> TestMongoDb(){
        Map<String,String> map = new HashMap<>();
        map.put("jeecg","mongodb-jeecg");
        mongoTemplate.insert(map, "testMongoDb");

        return Result.OK("存入成功");
    }

    @GetMapping("/test2")
    public Result<?> TestMongoDb2(){
        repository.deleteAll();

        // save a couple of customers
        repository.save(new Customer("Alice", "Smith"));
        repository.save(new Customer("Bob", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repository.findByLastName("Smith")) {
            System.out.println(customer);
        }

        return Result.OK("存入成功");
    }

}

7. 测试结果 

测试后的数据库截图

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值