springboot干货——(十四【一】)整合mongodb

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gwd1154978352/article/details/79355570

mongodb这块我就不介绍了,不了解的小伙伴可以移步mongodb教程

直接上项目

1.项目结构图如下:



2.pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.gwd</groupId>
	<artifactId>springboot-mongodb</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-mongodb</name>
	<description>Demo project for springboot-mongodb</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

3.application.properties

有密码的如下:

spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test

name对应用户名,pass对应密码

没有密码的如下:

spring.data.mongodb.uri=mongodb://localhost:27017/test

4.model层

User:

public class User {
	@Id
    private Long id;

    private String username;
    private Integer age;
    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public User(Long id, String username, Integer age) {
        this.id = id;
        this.username = username;
        this.age = age;
    }
}

5.Repository

UserRepository:

public interface UserRepository extends MongoRepository<User, Long>{
	
	User findByUsername(String username);
}


6.Controller
package com.gwd.controller;
/** 
* @FileName TestController.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年2月23日 下午5:02:05 
* 修改历史:
* 时间           作者          版本        描述
*====================================================  
*
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gwd.model.User;
import com.gwd.repository.UserRepository;

@RestController
public class TestController {
	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("/testMongodb")
	public String testMongodb() {
		// 创建三个User,并验证User总数
		userRepository.save(new User(1L, "didi", 30));
		userRepository.save(new User(2L, "mama", 40));
		userRepository.save(new User(3L, "kaka", 50));
		System.out.println(userRepository.findAll().size());

		// 删除一个User,再验证User总数
		User u = userRepository.findOne(1L);
		userRepository.delete(u);
		System.out.println(userRepository.findAll().size());

		// 删除一个User,再验证User总数
		u = userRepository.findByUsername("mama");
		userRepository.delete(u);
		System.out.println(userRepository.findAll().size());
		return userRepository.findByUsername("kaka").getUsername();
	}
}

7.测试



阅读更多
换一批

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