spring boot 整合mongodb

一:介绍

Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程,简化配置

mongodb 是nosql 文档型数据库 可用于日志存储以及实时数据存储方案


二: 新建项目


1: 新建项目为eglsc-mongo 如图:



spring boot 整合mongodb_第1张图片

三:配置

  maven依赖配置

  

="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.0modelVersion>

    <groupId>com.eglscgroupId>
    <artifactId>eglsc-mongoartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
         <version>1.4.0.RELEASEversion>
    parent>
    <properties>
        <java.version>1.7java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>

        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-mongodbartifactId>

        dependency>

        
        <dependency>
        <groupId>com.google.guavagroupId>
        <artifactId>guavaartifactId>
        <version>19.0version>
    dependency>
      
        <dependency>
            <groupId>joda-timegroupId>
            <artifactId>joda-timeartifactId>
            <version>2.9.4version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>

        dependency>

        <dependency>
            <groupId>commons-langgroupId>
            <artifactId>commons-langartifactId>
            <version>2.4version>
        dependency>
        <dependency>
            <groupId>commons-beanutilsgroupId>
            <artifactId>commons-beanutilsartifactId>
            <version>1.8.0version>
        dependency>


        <dependency>
            <groupId>com.google.code.gsongroupId>
            <artifactId>gsonartifactId>
            <version>2.7version>
        dependency>
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-core-aslartifactId>
            <version>1.9.13version>
        dependency>
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-mapper-aslartifactId>
            <version>1.9.13version>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>

        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.4version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.10version>
        dependency>

    dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>1.2.3.RELEASEversion>
                <configuration>
                    <mainClass>com.eglsc.spiss.ApplicationmainClass>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jarbootclasspath>
                    compilerArguments>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <version>2.6version>
                <configuration>
                    <encoding>UTF-8encoding>
                    <delimiters>
                        <delimiter>@delimiter>
                    delimiters>
                    <useDefaultDelimiters>falseuseDefaultDelimiters>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <configuration>
                    <skip>trueskip>
                configuration>
            plugin>
        plugins>
    build>
project>
 
  
 
  
2:javaconfig 配置
 
  
 
  
 
  
 
  spring boot 整合mongodb_第2张图片 
  
四 配置文件
  
 
  
application.properties
 
  
spring.data.mongo.host=192.168.0.23
spring.data.mongo.port=27017
spring.data.mongo.database=test
application.yml
 
  
server:
  port: 8181

 
  
五: 代码具体实现
 
  
 1:新建集合类比如
 
  
/**
 * Created by ${huipei.x} on 2017/5/18
 */

@Getter
@Setter
@Document(collection="mongo_user")
public class MongoUser {

    @Id
    private String id;
    private Integer userId;
 
  
    private String userName;
}
 
  
 2:新建操作集合
  (1).新建操作集合公共类可扩展(BaseMongoRepository
 
  
实现类
 
 
  
 (2).新建操作集合类(dao 使用的是spring data mongodb)
 
  
 
  
package com.eglsc.mongo.repository;

import com.eglsc.base.BaseMongoRepository;
import com.eglsc.mongo.domain.MongoUser;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.io.Serializable;

/**
 * Created by ${huipei.x} on 2017/5/18
 */
public interface MongoUserRepository extends BaseMongoRepository {
}

六:spring boot启动类
 
  
@SpringBootApplication
public class Application {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

七: 测试
 
  
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MongoUserRepositoryTest {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MongoUserRepository mongoUserRepository;

    @Test
    public void add(){
        MongoUser mongoUser=new MongoUser();
        mongoUser.setUserId(112222);
 
  
	mongoUser.setUserName("江海通");
mongoUser.setId(UUID. randomUUID().toString()); mongoUserRepository.save(mongoUser); }
}
 
  
 
  


你可能感兴趣的:(spring,boot系列)