| | |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.yeshi.buwan.domain.VideoType; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.aggregation.Aggregation; |
| | | import org.springframework.data.mongodb.core.aggregation.AggregationOperation; |
| | | import org.springframework.data.mongodb.core.aggregation.AggregationResults; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.data.mongodb.core.query.Update; |
| | |
| | | |
| | | @Repository |
| | | public abstract class MongodbBaseDao<T> { |
| | | @Resource |
| | | protected MongoTemplate mongoTemplate; |
| | | @Resource |
| | | protected MongoTemplate mongoTemplate; |
| | | |
| | | /** |
| | | * 插入数据 |
| | | * |
| | | * @param bean |
| | | * @return |
| | | */ |
| | | public T save(T bean) { |
| | | mongoTemplate.save(bean); |
| | | return bean; |
| | | } |
| | | /** |
| | | * 插入数据 |
| | | * |
| | | * @param bean |
| | | * @return |
| | | */ |
| | | public T save(T bean) { |
| | | mongoTemplate.save(bean); |
| | | return bean; |
| | | } |
| | | |
| | | /** |
| | | * 根据主键更新数据 |
| | | * |
| | | * @param query |
| | | * @param update |
| | | */ |
| | | public void update(Query query, Update update) { |
| | | mongoTemplate.upsert(query, update, this.getEntityClass()); |
| | | } |
| | | /** |
| | | * 根据主键更新数据 |
| | | * |
| | | * @param query |
| | | * @param update |
| | | */ |
| | | public void update(Query query, Update update) { |
| | | mongoTemplate.upsert(query, update, this.getEntityClass()); |
| | | } |
| | | |
| | | /** |
| | | * 查询一个数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public T findOne(Query query) { |
| | | return (T) mongoTemplate.findOne(query, this.getEntityClass()); |
| | | } |
| | | /** |
| | | * 查询一个数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public T findOne(Query query) { |
| | | return (T) mongoTemplate.findOne(query, this.getEntityClass()); |
| | | } |
| | | |
| | | /** |
| | | * 查询多个数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public List<T> findList(Query query) { |
| | | return mongoTemplate.find(query, this.getEntityClass()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 统计数量 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public long count(Query query) { |
| | | return mongoTemplate.count(query, this.getEntityClass()); |
| | | } |
| | | /** |
| | | * 查询多个数据 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public List<T> findList(Query query) { |
| | | return mongoTemplate.find(query, this.getEntityClass()); |
| | | } |
| | | |
| | | /** |
| | | * 主键查询 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public T get(Object id) { |
| | | return (T) mongoTemplate.findById(id, this.getEntityClass()); |
| | | } |
| | | |
| | | /** |
| | | * 通过主键删除 |
| | | * |
| | | * @param id |
| | | */ |
| | | public void delete(Object id) { |
| | | Query query = Query.query(Criteria.where("id").is(id)); |
| | | mongoTemplate.remove(query, getEntityClass()); |
| | | } |
| | | /** |
| | | * 统计数量 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | public long count(Query query) { |
| | | return mongoTemplate.count(query, this.getEntityClass()); |
| | | } |
| | | |
| | | public void deleteByPrimaryKey(Object id) { |
| | | Query query = Query.query(Criteria.where("_id").is(id)); |
| | | mongoTemplate.remove(query, getEntityClass()); |
| | | } |
| | | /** |
| | | * 主键查询 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public T get(Object id) { |
| | | return (T) mongoTemplate.findById(id, this.getEntityClass()); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | protected Class<T> getEntityClass() { |
| | | Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) |
| | | .getActualTypeArguments()[0]; |
| | | return tClass; |
| | | } |
| | | /** |
| | | * 通过主键删除 |
| | | * |
| | | * @param id |
| | | */ |
| | | public void delete(Object id) { |
| | | Query query = Query.query(Criteria.where("id").is(id)); |
| | | mongoTemplate.remove(query, getEntityClass()); |
| | | } |
| | | |
| | | public void delete(Query query) { |
| | | mongoTemplate.remove(query, getEntityClass()); |
| | | } |
| | | |
| | | public void deleteByPrimaryKey(Object id) { |
| | | Query query = Query.query(Criteria.where("_id").is(id)); |
| | | mongoTemplate.remove(query, getEntityClass()); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | protected Class<T> getEntityClass() { |
| | | Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) |
| | | .getActualTypeArguments()[0]; |
| | | return tClass; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 聚合查询 |
| | | * |
| | | * @param opts |
| | | * @param output |
| | | * @return |
| | | */ |
| | | public AggregationResults aggregate(List<? extends AggregationOperation> opts, Class output) { |
| | | Aggregation aggregation = Aggregation.newAggregation(opts); |
| | | return mongoTemplate.aggregate(aggregation, this.getEntityClass(), output); |
| | | } |
| | | |
| | | } |