springboot学习笔记(六)spring data mongo以及querydsl在此的应用

1.引入依赖:

引入spring boot data mongo依赖

	
			org.springframework.boot
			spring-boot-starter-data-mongodb
		
		

引入mongo querydel依赖

    
            com.querydsl
            querydsl-mongodb
            ${querydsl.version}
        

引入查询实体生成插件:

 
         

完整依赖:

	         
	           org.mongodb  
	           mongo-java-driver  
	           3.2.2
	          
		     
		        org.springframework.boot  
		        spring-boot-starter-data-mongodb  
		      
		      
		        com.querydsl  
		        querydsl-mongodb  
		        4.1.1  
		    
	          
	            com.querydsl  
	            querydsl-apt  
	            provided  
	            4.1.1
	            
		      
		           org.mongodb.morphia  
		           morphia  
		           0.109  
		      

2.右键项目debug as maven generate source生成对应实体的查询实体


3.创建实体查询的仓库类

* Test_myspringboot
 * MongoJpaTest.java
 */
package com.springboot.Test_myspringboot.repostory;

import java.io.Serializable;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.springboot.Test_myspringboot.entity.Literature;

/**
 * @author xuchuang
 * @updateTime 2017年12月27日 上午9:51:14
 */  																						
public interface MongoJpaTest extends PagingAndSortingRepository,QueryDslPredicateExecutor{

}

ok基本配置完成,开始编写测试接口!

@Override
	public void helloMongoRepostory() {

		QLiterature ql=QLiterature.literature;
		Literature  lit=mongoJpaTest.findOne("59f7e5a8f8fbac03249d977e");
		 System.out.println("书籍标题为:"+lit.getTitle());
		 
		Predicate pre=ql.author.eq("王阳明");
		List  list=(List) mongoJpaTest.findAll(pre);
		System.out.println(list.size());
		
	}


package com.springboot.Test_myspringboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.Test_myspringboot.service.IMongoService;

/**
 * @author xuchuang
 * @updateTime 2017年12月26日 下午3:16:38
 */
@RestController
public class MongoController_test {
	@Autowired
	private IMongoService mongoService;
	
	@RequestMapping("/helloMongo")	  
	public String home() {
		mongoService.MongoJdbc();
	  return "helloMongo!";
	}
	
	@RequestMapping("/helloMongoRepostory")	  
	public String helloMongoRepostory() {
		mongoService.helloMongoRepostory();
		
	  return "helloMongoRepostory!";
	}
	

}

访问

localhost:6636/helloMongoRepostory查看控制台:


2017-12-27 10:34:10.803  INFO 8464 --- [nio-6636-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-12-27 10:34:10.823  INFO 8464 --- [nio-6636-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
2017-12-27 10:34:10.900  INFO 8464 --- [nio-6636-exec-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:1331}] to 192.9.8.204:27017
书籍标题为:王阳明
11482

ok 测试完成


项目结构


springboot学习笔记(六)spring data mongo以及querydsl在此的应用_第1张图片


你可能感兴趣的:(springboot)