Spring和MongoDB(全注解)整合过程

最近在准备毕业论文,论文中使用到了Spring框架,考虑到数据的格式并非有固定的字段格式,因此考虑到使用MongoDB非关系型数据库存储数据。今天将Spring和MongoDB的整合过程总结如下(有坑,注意选择Spring 框架的版本)。本文章将从三个方面进行总结,分别是:

  • 在ubuntu上安装MongoDB
  • 使用Java程序测试MongoDB是否安装成功
  • MongoDB和Spring框架进行整合(MongoTemplate和MongoRepository)

Ubuntu上安装MongoDB

在下载MongoDB时,我选择的是从官网上下载,结果发现下载速度实在是太慢了,下载完毕之后,我分享到了我的云盘上,MongoDB的版本是Ubuntu X64版本,望读者在下载时,请仔细确认实验机的系统版本。
MongoDB的下载链接:MongoDB Ubuntu X64 ,下载密码为:M6aJqZ
安装的过程:
1、下载好的安装包为:mongodb-linux-x86_64-3.2.10.tgz,以我的pc为例,将软件解压在/home/wangbinghua/software路径下,将解压之后的文件夹名称更改为mongodb。

2、MongoDB分为服务器端和客户端,因此如果使得MongoDB能够正常被访问,首先要启动服务器端,接下来启动客户端,在使用完毕之后,关闭服务器端。

  • 启动服务器端准备工作
    MongoDB服务器端启动开始之后,默认要读取存储数据的文件路径以及记录日志的文件路径,因此在启动之后,要只能数据文件路径以及日志文件路径。
    以我的pc为例,在MongDB的安装路径下,建立data文件夹,绝对路径为:
    /home/wangbinghua/software/mongodb/data。在MongDB的安装路径下,建立log文件夹以及mongdb.log文件,绝对路径为:/home/wangbinghua/software/mongodb/log/mongodb.log。

编写一个启动服务器的脚本 start.sh

/home/wangbinghua/software/mongodb/bin/mongod --dbpath /home/wangbinghua/software/mongodb/data --fork --auth --logpath /home/wangbinghua/software/mongod
b/log/mongodb.log```
将脚本存放在MongoDB的安装路径的bin目录下。**--fork** 表明服务器端可以在作为守护线程运行,即可以后台运行。**--auth** 表明客户端访问MongoDB的数据必须要进行权限验证,即如同mysql一样进行密码和用户名进行登陆每一个数据库。
* 启动MongoDB客户端
编写一个启动客户端的脚本 client.sh
``` shell
  /home/wangbinghua/software/mongodb/bin/mongo 127.0.0.1:27017

将脚本存放在MongoDB的安装路径的bin目录下。127.0.0.1 表明MongoDB的服务器端地址为127.0.0.1,27017 表明MongoDB服务器端的端口号为 27017。

  • 关闭MongDB服务器端
    编写一个启动客户端的脚本 shutdown.sh
  killall mongod

Java程序测试MongoDB

建立一个Maven工程,具体的依赖如下:

<dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.1.0</version>
        </dependency>
</dependencies>

编写Java测试程序:

//连接服务器端的地址和端口号
ServerAddress serverAddress = new ServerAddress("localhost",27017);
//project数据库的用户名为:proj 密码:proj
char[] password = "proj".toCharArray();
//创建 用户名和密码认证
MongoCredential mongoCredential = MongoCredential.createMongoCRCredential("proj","project",password);
List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();
mongoCredentialList.add(mongoCredential);

//连接MongoDB的一个数据库,需要用户名、密码、数据库、服务器端的IP和端口号
MongoClient mongoClient = new MongoClient(serverAddress,mongoCredentialList);
MongoDatabase mongoDatabase = mongoClient.getDatabase("project");

 System.out.println("connect successfully!");

运行结果:

connect successfully!

MongoDB和Spring框架进行整合

  • 项目的主体依赖
  • Spring主体框架,spring-webmvc,spring-tx,spring-jdbc
  • JPA的主体依赖,spring-data-jpa
  • MongoDB的驱动依赖:mongo-java-driver
  • Spring和MongoDB的主体依赖:spring-data-mongodb
  • jsp的主体依赖:jsp-api
  • servlet的主体依赖:javax.servlet-api
  • 依赖版本
  • Spring主体框架,4.2.6.RELEASE
  • JPA的主体依赖,1.10.1.RELEASE
  • MongoDB的驱动依赖:3.1.0
  • Spring和MongoDB的主体依赖:1.8.4.RELEASE
  • jsp的主体依赖:2.1
  • servlet的主体依赖:3.1.0
  • 整个工程使用annotation 注解的方式来进行编写(包括业务逻辑、配置文件)
  • 配置文件 web.xml 配置文件,使用Java类的形式进行编写:
public class WebConfig implements WebApplicationInitializer {
   //相当于 web.xml 文件配置
   @Override
   public void onStartup(ServletContext servletContext) throws ServletException {

       //注入 Spring框架的配置文件
       //相当于 配置SpringContext.xml 容器配置文件
       AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
       applicationContext.register(SpringContextConf.class);
       //注册监听器(启动Spring Context 上下文)
       ContextLoaderListener contextLoaderListener = new ContextLoaderListener(applicationContext);
       servletContext.addListener(contextLoaderListener);
       //动态注册 filter
       //注册 HiddenHttpMethodFilter 使用Restful服务,屏蔽get post方法
       HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
       FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("HiddenHttpMethodFilter", hiddenHttpMethodFilter);
       filterRegistration.addMappingForServletNames(
               EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),
               false,
               "dispatcherServlet");
       //SpringMVC.xml
       AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
       webApplicationContext.register(SpringWebConf.class);
       //动态注册Spring mvc DispatcherServlet
       DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
       ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("dispatcherServlet",dispatcherServlet);
       servletRegistration.setLoadOnStartup(1);
       servletRegistration.addMapping("/");
   }
}
  • 编写MongoDB的配置文件,如同在使用Mysql时编写conn.properties 文件一样,具体内容如下:
mongo.database=project
mongo.host=localhost
mongo.port=27017
mongo.username=wbh
mongo.password=wbh
  • 编写SpringConfigConf.class,即Spring的配置文件,如同编写SpringContext.xml 配置文件一般。
@Configuration
@ComponentScan(value="com.mongodb",excludeFilters=@ComponentScan.Filter(type = FilterType.ANNOTATION,
        value = {Controller.class, EnableWebMvc.class, ControllerAdvice.class}))
@Import({MongoDBConf.class})
public class SpringContextConf {
}
  • 编写Spring Mvc的配置文件,SpringWebConf.class,即Spring MVC的配置文件,如同编写SpringWeb,xml配置文件一般。
@Configuration
@ComponentScan(basePackages = "com.mongo",
       useDefaultFilters = false,
       includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, ControllerAdvice.class}))
@EnableWebMvc
/**<mvc:annotation-driven>**/
public class SpringWebConf {
}
  • 编写MongoDBConf.class,如同编写MongoDBConf.xml 文件,被引入到SpringContextConf.class 配置类中。有两种方式来进行操作MongoDB,1、MongoTemplate类,进行操作。2、使用SpringData的Repository,通过编写接口,Spring Data自动实现该接口,从而依赖该类。

使用MongoTemplate类操作MongoDB

@Configuration
@PropertySource(value = "classpath:mongodb.properties")
public class MongoDBConf {
 @Autowired
  private Environment environment;
  @Bean
   public MongoClient mongoClient(){
       String username = environment.getProperty("mongo.username");
       String password = environment.getProperty("mongo.password");
       String database = environment.getProperty("mongo.database");
       String host = environment.getProperty("mongo.host");
       Integer port = Integer.valueOf(environment.getProperty("mongo.port"));
       ServerAddress serverAddress = new ServerAddress(host,port);
       MongoCredential mongoCredential = MongoCredential.createCredential(username, database, password.toCharArray());
       List<MongoCredential> mongoCredentialList = new ArrayList<>();
       mongoCredentialList.add(mongoCredential);

       return new MongoClient(serverAddress,mongoCredentialList);
   }
   @Bean
   public MongoDbFactory mongoDbFactory(){
       String database = environment.getProperty("mongo.database");
       return new SimpleMongoDbFactory(mongoClient(),database);
   }

   @Bean
   public MongoTemplate mongoTemplate(){

       MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
       return mongoTemplate;
   }
}```
使用MVC三层结构来构建工程。
数据层,数据永久层:
```java
private MongoTemplate mongoTemplate;

服务层,服务层(接口和实现类):

//接口
public interface PersonService {
      public List<Person> findAllPerson();
}```
``` java
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Person> findAllPerson() {
        return mongoTemplate.findAll(Person.class);
    }
}```
控制层,通过Json返回数据:
``` java
@Controller
public class TestController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/allPersons")
    @ResponseBody
    public List<Person> getAllPerson(){
        return personService.findAllPerson();
    }
}

执行结果:

[{"id":"587d948ebe16f9201a6f2a1a","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587d948fbe16f9201a6f2a1b","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587d9490be16f9201a6f2a1c","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587da93ebe16f92d64ee4317","username":"wangbinghua","password":"wbhdxtd","age":12}]

通过shell窗口查看数据库中的数据:

wangbinghua@wangbinghua:~/software/mongodb/bin$ sh ./client.sh 
MongoDB shell version: 3.2.10
connecting to: 127.0.0.1:27017/test
> use project;
switched to db project
> db.auth("wbh","wbh")
1
> db.tb_person.find()
{ "_id" : ObjectId("587d948ebe16f9201a6f2a1a"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587d948fbe16f9201a6f2a1b"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587d9490be16f9201a6f2a1c"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587da93ebe16f92d64ee4317"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }

使用Spring Data的MongoRepository接口操作MongoDB

MongoDBConf.class 配置类编写如下:

@Configuration
@PropertySource(value = "classpath:mongodb.properties")
@EnableMongoRepositories(basePackages = "com.mongo.dao")
public class MongoDBConf {
  @Autowired
   private Environment environment;
   @Bean
    public MongoClient mongoClient(){
        String username = environment.getProperty("mongo.username");
        String password = environment.getProperty("mongo.password");
        String database = environment.getProperty("mongo.database");
        String host = environment.getProperty("mongo.host");
        Integer port = Integer.valueOf(environment.getProperty("mongo.port"));
        ServerAddress serverAddress = new ServerAddress(host,port);
        MongoCredential mongoCredential = MongoCredential.createCredential(username, database, password.toCharArray());
        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        mongoCredentialList.add(mongoCredential);

        return new MongoClient(serverAddress,mongoCredentialList);
    }
    @Bean
    public MongoDbFactory mongoDbFactory(){
        String database = environment.getProperty("mongo.database");
        return new SimpleMongoDbFactory(mongoClient(),database);
    }

    @Bean
    public MongoTemplate mongoTemplate(){

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }
}

@EnableMongoRepositories(basePackages = “com.mongo.dao”) 这个注解的含义是:自动扫描com.mongo.dao包下的接口(继承MongoRepository<Person,String>),并且为这个类自动生成其实现类,该类提供了操作MongoDB的方法。
数据层编写:

public interface PersonRepository extends MongoRepository<Person,String>{
}

MongoDB中一个文档(一条记录)的id为字符串型,因此MongoRepository<Person,String> 的泛型为Person和String。
服务层编写(接口和实现类):

//接口
public interface PersonService {
    public List<Person> findAllPerson();
}
//实现类
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonRepository personRepository;
    @Override
    public List<Person> findAllPerson() {
        return personRepository.findAll();
    }
}

控制层编写:

@Controller
public class TestController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/allPersons")
    @ResponseBody
    public List<Person> getAllPerson(){
        return personService.findAllPerson();
    }
}

执行结果:

[{"id":"587d948ebe16f9201a6f2a1a","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587d948fbe16f9201a6f2a1b","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587d9490be16f9201a6f2a1c","username":"wangbinghua","password":"wbhdxtd","age":12},
{"id":"587da93ebe16f92d64ee4317","username":"wangbinghua","password":"wbhdxtd","age":12}]

通过shell窗口查看数据库中的数据:

wangbinghua@wangbinghua:~/software/mongodb/bin$ sh ./client.sh 
MongoDB shell version: 3.2.10
connecting to: 127.0.0.1:27017/test
> use project;
switched to db project
> db.auth("wbh","wbh")
1
> db.tb_person.find()
{ "_id" : ObjectId("587d948ebe16f9201a6f2a1a"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587d948fbe16f9201a6f2a1b"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587d9490be16f9201a6f2a1c"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }
{ "_id" : ObjectId("587da93ebe16f92d64ee4317"), "_class" : "com.mongo.dao.domain.Person", "person_username" : "wangbinghua", "person_password" : "wbhdxtd", "person_age" : 12 }

工程代码托管在github下,读者感兴趣可以自行下载spring-mongodb

    原文作者:_挑灯看剑_
    原文地址: https://www.jianshu.com/p/84176f09a4b8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞