作者:本多天舞 | 来源:互联网 | 2023-10-10 19:17
安装使用Idea新建Springboot工程时需要选择Mongodb或者在工程中加入依赖Maven:<dependency&g
安装
使用Idea新建Spring boot工程时需要选择Mongodb
或者在工程中加入依赖
Maven:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
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());
}
}
查询出结果即完成