自从用了SPRING DATA MONGODB后,增删改查的实现方法都不用自己写了,只需声明方法名称,SPRING会自动添加代码,但用时候SPRING自带的方法不够,难免要添加的,因此如何在原有的方法上叠加自定义的方法呢?
定义自定义的接口
public interface VideoRepositoryCustom {
public List<Cat1UpdateCount> getVideoWithUpdateFrag(List<String> importantCat1List);
}
添加自定义的实现
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import program.video.aggregation.valueobject.Cat1UpdateCount;
import program.video.valueobject.Video;
public class VideoRepositoryImpl implements VideoRepositoryCustom{
private static Logger logger = LoggerFactory.getLogger(VideoRepositoryImpl.class);
@Autowired
private MongoTemplate mongoTemplate;
public List<Cat1UpdateCount> getVideoWithUpdateFrag(List<String> importantCat1List) {
logger.info(new Date().toString());
/**
* db.videos.aggregate(
[
{ $match: { "frags.isnew" : true } },
{ $unwind: "$frags" },
{ $match: { "frags.isnew" : true } },
{ $group: {
_id: {cat1:"$cat1"},
count: { $sum: 1 },
publishdate2: { $max: "$publishdate"}
}
}
]
)
*/
Aggregation agg = newAggregation(
project("frags","cat1","publishdate"),
match(
Criteria.where("frags.isnew").is(Boolean.TRUE)
.and("cat1").in(importantCat1List)
),
unwind("frags"),//展开子项LIST,且是内链接,即如果父和子的关联ID没有的就不会输出
match(Criteria.where("frags.isnew").is(Boolean.TRUE)),
group("cat1")//设置分组字段
.count().as("updateCount")//增加COUNT为分组字段
.last("publishdate").as("publishDate"),//增加publishDate为分组字段
project("publishDate","cat1","updateCount")//重新挑选字段
.and("cat1").previousOperation()//为前一操作所产生的ID FIELD建立别名
);
AggregationResults<Cat1UpdateCount> results = mongoTemplate.aggregate(agg, Video.COLLECTION_NAME, Cat1UpdateCount.class);
List<Cat1UpdateCount> cat1UpdateCountList = results.getMappedResults();
return null;
}
}
原先的接口实现多重继承
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import program.video.valueobject.Video;
public interface VideoRepository extends PagingAndSortingRepository<Video, String>,VideoRepositoryCustom {
@Query("{ title : {$regex : ?0 } }")
public Page<Video> findVideosByKeyword(@Param("title") String keyword, Pageable page);
@Query("{ pid : ?0 }")
public Video findByVideoId(String id);
@Query(value="{ pid : ?0 , ver: { $gt : ?1 }}")
public Video findByIdAndVersion(String id, int ver);
public Page<Video> findByTitleLike(String title, Pageable pageable);
@Query("{ title : {$regex : ?0}, cat1 : ?1}")
public Page<Video> findVideosByTitleAndCat1(String title, String cat1, Pageable pageable);
@Query("{ cat1 : ?0}")
public Page<Video> findVideosByCat1(String cat1, Pageable pageable);
@Query("{ title : {$regex : ?0}, cat1 : ?1, status : ?2}")
public Page<Video> findVideosByTitleAndCat1AndStatus(String title, String cat1, int status, Pageable pageable);
@Query("{ title : {$regex : ?0}, cat1 : ?1, status : { $in : [ ?2, null]}}")
public Page<Video> findVideosByTitleAndCat1AndStatusExist(String title, String cat1, int status, Pageable pageable);
@Query("{ title : {$regex : ?0}, status : ?1}")
public Page<Video> findVideosByTitleAndStatus(String title, int status, Pageable pageable);
@Query("{ title : {$regex : ?0}, status : { $in : [ ?1, null]}}")
public Page<Video> findVideosByTitleAndStatusExist(String title, int status, Pageable pageable);
@Query("{ cat1 : ?0, status : ?1}")
public Page<Video> findVideosByCat1AndStatus(String cat1, int status, Pageable pageable);
@Query("{ cat1 : ?0, status : { $in : [ ?1, null]}}")
public Page<Video> findVideosByCat1AndStatusExist(String cat1, int status, Pageable pageable);
@Query("{ status : ?0}")
public Page<Video> findVideosByStatus(int status, Pageable pageable);
@Query("{status : { $in : [ ?0, null]}}")
public Page<Video> findVidesByStatusExist(int status, Pageable pageable);
}
SPRING DATA 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
<context:annotation-config />
<context:property-placeholder location="classpath*:/properties/mongodb/mongo.properties"/>
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}"
write-number="1" write-timeout="0" write-fsync="true" />
</mongo:mongo>
<!-- <mongo:db-factory
dbname="${mongo.dbname}"
username="${mongo.username}"
password="${mongo.password}"
mongo-ref="mongo" /> -->
<mongo:db-factory
dbname="${mongo.dbname}"
mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<mongo:repositories base-package="com.tcl.project7.boss.**.repository" />
</beans>
注意的是,自定义的实现类要以IMPL后缀,则SPRING可以自动识别的,无需再指定了。
调用REPOSITORY
@Autowired
private VideoRepository videoRepository;