8.2.1. Example Spring Data MongoDB
8.2.1.1. pom.xml
注意Spring4 与 1.9.1.RELEASE有兼容性问题,日志提示 Error creating bean with name 'mongoTemplate' defined in ServletContext resource
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.8.1.RELEASE</version> </dependency>
8.2.1.2. springframework-servlet.xml
<mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.database}" /> <!-- username="${mongo.username}" password="${mongo.password}" --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean> <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/> <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="mongoDbFactory"/> <constructor-arg ref="converter"/> </bean>
例 8.2. Spring Data MongoDB - springframework-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd "> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/zt/" mapping="/zt/**" /> <mvc:resources location="/sm/" mapping="/sm/**" /> <mvc:resources location="/module/" mapping="/module/**" /> <context:component-scan base-package="cn.netkiller.controller" /> <!-- <context:property-placeholder location="classpath:resources/development.properties" /> --> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="configuracion" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:resources/development.properties" /> </bean> <!-- MongoDB Connection Factory --> <mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.database}" /> <!-- username="${mongo.username}" password="${mongo.password}" --> <!-- MongoDB template definition --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean> <!-- MongoDB GridFS template definition --> <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/> <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="mongoDbFactory"/> <constructor-arg ref="converter"/> </bean> <!-- Redis Connection Factory --> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="192.168.2.1" p:port="6379" p:use-pool="true" /> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory" /> </beans>
development.properties 配置内容
mongo.host=192.168.4.1 mongo.port=27017 mongo.username=test mongo.password=passw0rd mongo.database=website
8.2.1.3. POJO
package cn.netkiller.pojo; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "tracker") public class Tracker { @Id private String id; private String name; private String unique; private String hostname; private String referrer; private String href; public Tracker() { // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUnique() { return unique; } public void setUnique(String unique) { this.unique = unique; } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getReferrer() { return referrer; } public void setReferrer(String referrer) { this.referrer = referrer; } public String getHref() { return href; } public void setHref(String href) { this.href = href; } @Override public String toString() { return "Tracker [id=" + id + ", name=" + name + ", unique=" + unique + ", hostname=" + hostname + ", referrer=" + referrer + ", href=" + href + "]"; } }
8.2.1.4. Controller
package cn.netkiller.controller; import cn.netkiller.pojo.Tracker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller public class TrackerController { @Autowired private MongoTemplate mongoTemplate; public TrackerController() { } @RequestMapping("/tracker/test") @ResponseBody String hello() { return "Hello World!"; } @RequestMapping("/tracker") @ResponseBody String execute() { Tracker tracker = new Tracker(); tracker.setName("test"); tracker.setUnique("111223456"); tracker.setHostname("www.example.com"); tracker.setHref("http://example.com/test.html"); tracker.setReferrer("http://example.com/"); this.mongoTemplate.insert(tracker); return tracker.toString(); } }
8.2.1.5. 查看测试结果
> db.tracker.find(); { "_id" : ObjectId("5757c0b92c526a6bda5eea3a"), "_class" : "cn.netkiller.repositories.Tracker", "name" : "test", "unique" : "111223456", "hostname" : "www.example.com", "referrer" : "http://example.com/", "href" : "http://example.com/test.html" }
8.2.1.6. 条件查询
@RequestMapping("/read/name/{name}") public ArrayList<Tracker> sort(@PathVariable String name) { Query query = new Query(Criteria.where("name").is(name)); ArrayList<Tracker> trackers = (ArrayList<Tracker>) mongoTemplate.find(query, Tracker.class); return trackers; }
8.2.2. @Document
复杂的 @Document 数据类型定义
package cn.netkiller.domain; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class MultilevelDirectSellingTradingRebate { public enum Type { POINT, CASH, GIFT } public enum Rebate { DIRECT, INDIRECT } public enum Status { New, Rejected, Approved } @Id private String id; public String name; public Date beginDate; public Date endDate; public double lowAmount; public double highAmount; public Type type; public Status status = Status.New; public List<Map<String, Map<?, ?>>> product; @Override public String toString() { return "MultilevelDirectSellingTradingRebate [id=" + id + ", name=" + name + ", beginDate=" + beginDate + ", endDate=" + endDate + ", lowAmount=" + lowAmount + ", highAmount=" + highAmount + ", type=" + type + ", status=" + status + ", product=" + product + "]"; } }
8.2.2.1. @Indexed
索引
8.2.2.1.1. 普通索引
@Indexed
8.2.2.1.2. 唯一索引
@Indexed(unique=true)
8.2.2.2. @DateTimeFormat
@DateTimeFormat( pattern = "yyyy-MM-dd" ) private Date birthday @DateTimeFormat(iso = DateTimeFormat.ISO.NONE) private final Calendar datetime; @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date date;
8.2.2.3. @NumberFormat
@NumberFormat(style=Style.CURRENCY) private double money;
8.2.2.4. 在 @Document 中使用 Enum 类型
public enum Type { POINT, CASH, GIFT } public enum Rebate { DIRECT, INDIRECT } public enum Status { New, Rejected, Approved }
枚举类型的赋值方法
MultilevelDirectSellingTradingRebate multilevelDirectSellingTradingRebate = new MultilevelDirectSellingTradingRebate(); multilevelDirectSellingTradingRebate.name = "TEST"; multilevelDirectSellingTradingRebate.beginDate = new Date(); multilevelDirectSellingTradingRebate.endDate = new Date(); multilevelDirectSellingTradingRebate.lowAmount = 1.5d; multilevelDirectSellingTradingRebate.highAmount = 100d; multilevelDirectSellingTradingRebate.type = Type.CASH;
8.2.2.5. 在 @Document 中定义数据结构 List/Map
public List<Map<String, Map<?, ?>>> product;
下面是数据集结构的赋值例子
Map<Enum<Rebate>, Double> rebate = new HashMap<Enum<Rebate>, Double>(); rebate.put(Rebate.DIRECT, 10.05d); rebate.put(Rebate.INDIRECT, 6.05d); Map<String, Map<?, ?>> prod1 = new HashMap<String, Map<?, ?>>(); prod1.put("USDRMB", rebate); List<Map<String, Map<?, ?>>> products = new ArrayList<Map<String, Map<?, ?>>>(); products.add(prod1); multilevelDirectSellingTradingRebate.product = products;
8.2.3. MongoRepository
8.2.3.1. findAll()
@RequestMapping(value = "read", method = RequestMethod.GET, produces = { "application/xml", "application/json" }) @ResponseStatus(HttpStatus.OK) public List<Withdraw> read() { return repository.findAll(); }
8.2.3.2. deleteAll()
repository.deleteAll();
8.2.3.3. save()
repository.save(new City("Shenzhen", "China"));
8.2.3.4. count()
@RequestMapping("count") public long count() { return repository.count(); }
8.2.3.5. findByXXXX
List<User> findByName(String name); List<User> users = userRepository.findByName("Eric");
8.2.3.6. StartingWith 和 EndingWith
List<User> findByNameStartingWith(String regexp); List<User> findByNameEndingWith(String regexp); List<User> users = userRepository.findByNameStartingWith("N"); List<User> users = userRepository.findByNameEndingWith("o");
8.2.3.7. Between
List<User> findByAgeBetween(int ageGT, int ageLT); List<User> users = userRepository.findByAgeBetween(20, 50);
8.2.3.8. PageRequest
Page<User> findByLastname(String lastname, Pageable pageable);
@RequestMapping(value = "read/{size}/{page}", method = RequestMethod.GET, produces = { "application/xml", "application/json" }) @ResponseStatus(HttpStatus.OK) public List<Withdraw> readPage(@PathVariable int size, @PathVariable int page){ PageRequest pageRequest = new PageRequest(page-1,size); return repository.findAll(pageRequest).getContent(); }
URL翻页参数,每次返回10条记录
第一页 http://localhost:8080/v1/withdraw/read/10/1.json 第二页 http://localhost:8080/v1/withdraw/read/10/2.json ... 第五页 http://localhost:8080/v1/withdraw/read/10/5.json
8.2.3.9. @Query
8.2.4. mongoTemplate
8.2.4.1. is
Query query = new Query(); query.addCriteria(Criteria.where("name").is("Neo")); List<User> users = mongoTemplate.find(query, User.class);
8.2.4.2. Regex 正则表达式搜索
查询以N开头的名字
Query query = new Query(); query.addCriteria(Criteria.where("name").regex("^N")); List<User> users = mongoTemplate.find(query,User.class);
查询以o结尾的名字
Query query = new Query(); query.addCriteria(Criteria.where("name").regex("o$")); List<User> users = mongoTemplate.find(query, User.class);
8.2.4.3. lt 和 gt
查询年龄小于 < 30 并 > 20 的用户
Query query = new Query(); query.addCriteria(Criteria.where("age").lt(30).gt(20)); List<User> users = mongoTemplate.find(query,User.class);
8.2.4.4. between
实现一个区间条件 new Criteria("createdDate").gte(beginDate).lte(endDate)
public boolean AccountDeposit(Date beginDate, Date endDate) { MatchOperation matchOperation = match(new Criteria("createdDate").gte(beginDate).lte(endDate)); GroupOperation groupOperation = group("loginname").sum("amount").as("amount"); SortOperation sortOperation = sort(new Sort(Direction.ASC, "loginname")); Aggregation aggregation = newAggregation(matchOperation, groupOperation, sortOperation); AggregationResults<AccountSettlementDetails> results = mongoTemplate.aggregate(aggregation, AccountSettlementDetails.class, AccountSettlementDetails.class); if (results.getMappedResults() != null) { log.info(results.getRawResults().get("result").toString()); for (AccountSettlementDetails settlementDetails : results.getMappedResults()) { log.info("{}", settlementDetails.toString()); } } return true; }
8.2.4.5. Criteria
Query query = new Query(); query.addCriteria( new Criteria().andOperator( Criteria.where("field1").exists(true), Criteria.where("field1").ne(false) ) ); List<Foo> result = mongoTemplate.find(query, Foo.class); System.out.println("query - " + query.toString()); for (Foo foo : result) { System.out.println("result - " + foo); }
8.2.4.6. Sort
按照年龄排序
Query query = new Query(); query.with(new Sort(Sort.Direction.ASC, "age")); List<User> users = mongoTemplate.find(query,User.class);
8.2.4.7. Query + PageRequest
final Pageable pageableRequest = new PageRequest(0, 2); Query query = new Query(); query.with(pageableRequest);
8.2.4.8. newAggregation
MultilevelDirectSellingAccountRewardsSettlementDetails multilevelDirectSellingAccountRewardsSettlementDetails = new MultilevelDirectSellingAccountRewardsSettlementDetails(); multilevelDirectSellingAccountRewardsSettlementDetails.setLoginname("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setPhone("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderLoginname("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderPhone("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderName("Neo"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderType("客户"); multilevelDirectSellingAccountRewardsSettlementDetails.setAmount(5.02); multilevelDirectSellingAccountRewardsSettlementDetails.setCreatedDate(new Date()); multilevelDirectSellingAccountRewardsSettlementDetailsRepository.save(multilevelDirectSellingAccountRewardsSettlementDetails); Date beginDate = this.getToday("00:00:00"); Date endDate = this.getToday("23:59:59"); log.info(beginDate.toString() + " ~ " + endDate.toString()); GroupOperation groupOperation = group("loginname").sum("amount").as("amount"); MatchOperation matchOperation = match(new Criteria("createdDate").gte(beginDate).lte(endDate)); SortOperation sortOperation = sort(new Sort(Direction.ASC, "loginname")); Aggregation aggregation = newAggregation(matchOperation, groupOperation, sortOperation); AggregationResults<MultilevelDirectSellingAccountRewardsSettlementDetails> results = mongoTemplate.aggregate(aggregation, MultilevelDirectSellingAccountRewardsSettlementDetails.class, MultilevelDirectSellingAccountRewardsSettlementDetails.class); System.out.println(results.getRawResults().get("result").toString());
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。
时间: 2024-11-11 21:21:18