Spring boot整合mongodb

Spring boot整合mongodb

Github源码:https://github.com/superRabbitMan/spring-boot-mongodb

本文基于spring-data-mongodb进行spring boot和mongodb的整合

引入依赖

<dependencies>
   <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-mongodb</artifactId>
   </dependency>
   <dependency>
      <groupId>org.pegdown</groupId>
      <artifactId>pegdown</artifactId>
      <version>1.4.1</version>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
</dependencies>

配置mongodb信息和创建bean

只需要基础的配置就可以了,其它详细配置信息可以参考官网的properties说明

# MongoDB\u914D\u7F6E
mongo.host=172.16.94.138
mongo.name=test
mongo.port=27017
@Configuration
@EnableMongoRepositories(basePackages = "com.example.dao")
@PropertySource("classpath:application.properties")
public class MongoConfig extends AbstractMongoConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @Override
    public MongoClient mongoClient() {
        ServerAddress serverAddress = new ServerAddress(env.getRequiredProperty("mongo.host"));
        List<MongoCredential> credentials = new ArrayList<>();
        return new MongoClient(serverAddress, credentials);
    }

    @Override
    protected String getDatabaseName() {
        return env.getRequiredProperty("mongo.name");
    }
}

创建实体类

@Document(collection = "user")
public class User {

    @Id
    private String userId;

    @NotNull
    @Indexed(unique = true)
    private String username;

    @NotNull
    private String password;

    @NotNull
    private String name;

    @NotNull
    private String email;

    @NotNull
    private Date registrationDate = new Date();

    public User(String userId, @NotNull String username, @NotNull String password, @NotNull String name, @NotNull String email, @NotNull Date registrationDate) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.name = name;
        this.email = email;
        this.registrationDate = registrationDate;
    }

    //get set 省略
}

创建dao层处理类

MongoRepository类封装了很多基础的方法

@Repository
public interface UserRepository extends MongoRepository<User, String> {

    User findByUsername(String username);

}

Spring boot启动类添加方法

@RestController
@SpringBootApplication
public class SpringBootMongodbApplication {

   @Autowired
   private UserRepository userRepository;

   @RequestMapping(value = "/s")
   public String save() {
      User user = new User("1", "user", "123456789", "name", "email@126.com", new Date());
      userRepository.save(user);
      return "SUCCESS";
   }

   @RequestMapping(value = "/g")
   public List<User> get(String name) {
      return userRepository.findAll();
   }

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

关注微信公众号(程序员小兔)不定期分享技术

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读