zoukankan      html  css  js  c++  java
  • Spring boot中使用Mongodb

    安装

    使用Idea新建Spring boot工程时需要选择Mongodb

    或者在工程中加入依赖

    Maven:

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

    Gradle:

    dependencies {
      compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    }

    目录结构

    配置

    在application.yml中配置

    spring:
      data:
          mongodb:
            uri: mongodb://test:test@127.0.0.1:27017/test

    或者在application.properties配置

    spring.data.mongodb.uri=mongodb://test:test@127.0.0.1:27017/test

    其中URL的格式是:mongodb://用户名:用户名@ip地址:端口/数据库

    使用

    在实体类中加入注解

    package com.example.demo.domain;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.index.IndexDirection;
    import org.springframework.data.mongodb.core.index.Indexed;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    import java.io.Serializable;
    
    /**
     * 用户模型
     *
     * @author hackyo
     * Created on 2017/12/3 11:53.
     */
    @Document
    public class User implements Serializable {
    
        @Id
        private String id;
    
        @Indexed(unique = true, direction = IndexDirection.DESCENDING, dropDups = true)
        private String username;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
    }

    @Document表示该类为实体类

    @Id表示该字段为自动生成的ID

    @Indexed表示自动为该字段建立索引

    编写查询接口

    package com.example.demo.dao;
    
    import com.example.demo.domain.User;
    
    /**
     * 用户表操作接口
     *
     * @author hackyo
     * Created on 2017/12/3 11:53.
     */
    public interface IUserRepository {
    
        /**
         * 通过用户名查找用户
         *
         * @param username 用户名
         * @return 用户信息
         */
        User findByUsername(String username);
    
    }

    查询接口实现

    package com.example.demo.dao.impl;
    
    import com.example.demo.dao.IUserRepository;
    import com.example.demo.domain.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.stereotype.Repository;
    
    /**
     * 用户表操作接口实现
     *
     * @author hackyo
     * Created on 2017/12/3 11:53.
     */
    @Repository
    public class UserRepositoryImpl implements IUserRepository {
    
        private MongoTemplate mongoTemplate;
    
        @Autowired
        public UserRepositoryImpl(MongoTemplate mongoTemplate) {
            this.mongoTemplate = mongoTemplate;
        }
    
        @Override
        public User findByUsername(String username) {
            Query query = new Query(Criteria.where("username").is(username));
            return mongoTemplate.findOne(query, User.class);
        }
    
    }
    

    测试

    测试类

    package com.example.demo;
    
    import com.example.demo.dao.IUserRepository;
    import com.example.demo.domain.User;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
    
        @Autowired
        private IUserRepository userRepository;
    
        @Test
        public void contextLoads() {
        }
    
        @Test
        public void test() {
            User user = userRepository.findByUsername("test");
            System.out.println(user.getId());
        }
    
    }
    

    查询出结果即完成

  • 相关阅读:
    Jquery字符串,数组(拷贝、删选、合并等),each循环,阻止冒泡,ajax出错,$.grep筛选,$.param序列化,$.when
    Jquery cookie操作示例,写入cookie,读取cookie,删除cookie
    执行Sqlserver中waitfor delay延时操作或waitfor time定时操作
    JS里try...catch...finally详解,以及console日志调试(console.log、console.info等)
    19.Remove Nth Node From End of List---双指针
    18.4Sum
    16.3Sum Closest
    45.Jump Game II---贪心---2018大疆笔试题
    55.Jump Game---dp
    SQL相关
  • 原文地址:https://www.cnblogs.com/hackyo/p/8243669.html
Copyright © 2011-2022 走看看