我使用的版本是SpringBoot 2.6.4 可以像Mybaits Plus 一样的功能
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
spring:
data:
mongodb:
open: true
host: 127.0.0.1
port: 27017
username: test
password: mongodb
authentication-database: admin
database: test_db
配置类,默认不配置 添加的时候会有一个class字段,_class定义了每一条数据映射的实体类的类型,在使用SpringBoot-MongoDB的api插入数据时,即使引用类型是父类型,_class的值会插入对象的实际类型
转换器的作用一看就能理解 ,只是查询量比较大的时候,转换器会消耗很大的资源,慎用!
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.convert.*;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import java.util.Date;
@Configuration
public class MongoConfig {
@Bean
public MongoCustomConversions customConversions() {
return MongoCustomConversions.create(i -> {
i.registerConverter(new DateToLongConverter());
i.registerConverter(new LongToDateConverter());
});
}
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoMappingContext context, BeanFactory beanFactory, MongoCustomConversions customConversions) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
mappingConverter.setCustomConversions(customConversions);
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return mappingConverter;
}
@ReadingConverter
private static class LongToDateConverter implements Converter<Long, Date> {
@Override
public Date convert(Long source) {
if (source.toString().length() < 11) {
return new Date(source * 1000);
} else return new Date(source);
}
}
@WritingConverter
private static class DateToLongConverter implements Converter<Date, Long> {
@Override
public Long convert(Date source) {
return source.getTime() / 1000;
}
}
}
Mongodb注解含义
注解 | 含义 |
---|
@Id | 主键,不可重复,自带索引 | @Document | 标注在实体类上,类似于Mp的TableName注解,标明由mongo来维护该表 | @Indexed | 声明该字段需要加索引,加索引后以该字段为条件检索将大大提高速度。唯一索引的话是@Indexed(unique = true)。也可以对数组进行索引,如果被索引的列是数组时,MongoDB会索引这个数组中的每一个元素。也可以对整个Document进行索引,排序是预定义的按插入BSON据的先后升序排列。 | @CompoundIndex | 复合索引,加复合索引后通过复合索引字段查询将大大提高速度。是类注解@Document@CompoundIndexes({@CompoundIndex(name = “age_idx”, def = “{‘lastName’: 1, ‘age’: -1}”)}),写法如上,lastName和age将作为复合索引,数字参数指定索引的方向,1为正序,-1为倒序。方向对单键索引和随机存不要紧,但如果你要执行分组和排序操作的时候,它就非常重要了。 | @Field | 代表一个字段,可以不加,不加的话默认以参数名为列名。给映射存储到 mongodb 的字段取别名 | @Transient | 被该注解标注的,将不会被录入到数据库中。只作为普通的javaBean属性。 | @DBRef | 关联另一个document对象。类似于mysql的表关联,但并不一样,mongo不会做级联的操作。 |
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import hss.server.handler.DateLongTypeHandler;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.util.Date;
@Data
@TableName(value = "hss_history", autoResultMap = true)
@Document(collection = "hss_history")
@CompoundIndexes({@CompoundIndex(name = "age_idx", def = "{‘lastName’: 1, ‘age’: -1}")})
public class HssHistoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
@Id
private String id;
private Long equipmentId;
private Long typeId;
@TableField(typeHandler = JacksonTypeHandler.class)
private JSONObject data;
@TableField(typeHandler = JacksonTypeHandler.class)
private JSONObject parseData;
private Integer state;
private Long parseTime;
private Long createTime;
}
工具类
package hss.server.mongodb;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.result.UpdateResult;
import hss.server.hss.entity.HssHistoryEntity;
import hss.server.utils.DateUtils;
import hss.server.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.data.mongodb.core.query.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
@Component
@Slf4j
public class MongoDBUtil {
private static final Query EMPTY_QUERY = new BasicQuery("{}");
private static MongoTemplate template;
@Autowired
public void setTemplate(MongoTemplate template) {
MongoDBUtil.template = template;
}
private void setIndex() {
IndexOperations indexOps = template.indexOps(HssHistoryEntity.class);
Index index = new Index("equipmentId", Sort.Direction.ASC);
index.on("typeId", Sort.Direction.ASC);
indexOps.ensureIndex(index);
}
private static Query idEqQuery(Serializable id) {
Criteria criteria = Criteria.where("id").is(id);
return Query.query(criteria);
}
private Query idInQuery(Collection<? extends Serializable> idList) {
Criteria criteria = Criteria.where("id").in(idList);
return Query.query(criteria);
}
private Query eqQuery(Map<String, Object> data) {
if (CollectionUtils.isEmpty(data)) {
return EMPTY_QUERY;
} else {
Criteria criteria = new Criteria();
data.forEach((k, v) -> criteria.and(k).is(v));
return Query.query(criteria);
}
}
private static <T> Serializable getIdValue(T entity) {
try {
Field field = entity.getClass().getDeclaredField("id");
field.setAccessible(true);
return (Serializable) field.get(entity);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private <T> Update getUpdate(T entity) {
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
System.out.println(field.getName() + " " + field.get(entity));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
public <T> void save(T entity) {
template.save(entity);
}
public <T> void inset(T entity) {
template.insert(entity);
}
public <T> void saveBatch(Collection<T> entityList) {
template.insertAll(entityList);
}
public void removeById(Serializable id, Class<?> clazz) {
template.remove(idEqQuery(id.toString()), clazz);
}
public void removeByMap(Map<String, Object> columnMap, Class<?> clazz) {
template.remove(eqQuery(columnMap), clazz);
}
public void removeByIds(Collection<? extends Serializable> idList, Class<?> clazz) {
template.remove(idInQuery(idList), clazz);
}
public void remove(Query query, Class<?> clazz) {
template.remove(query, clazz);
}
public <T> boolean updateById(T entity) {
Assert.notNull(entity, "entity must not be null!");
JSONObject obj = (JSONObject) JSONObject.toJSON(entity);
DBObject update = new BasicDBObject();
update.put("$set", obj);
UpdateResult result = template.updateFirst(idEqQuery(getIdValue(entity)), new BasicUpdate(update.toString()), entity.getClass());
return result.getModifiedCount() == 1L;
}
public <T> void updateBatchById(Collection<T> entityList) {
entityList.forEach(e -> updateById(e));
}
public void update(Query query, Update update, Class<?> clazz) {
template.updateMulti(query, update, clazz);
}
public static <T> void saveOrUpdate(T entity) {
Assert.notNull(entity, "entity must not be null!");
String key = JSONObject.toJSONString(entity);
Update inc = new Update().inc(key, 1);
template.upsert(idEqQuery(getIdValue(entity)), inc, entity.getClass());
}
public <T> void saveOrUpdateBatch(Collection<T> entityList) {
entityList.forEach(MongoDBUtil::saveOrUpdate);
}
public <T> T getById(Serializable id, Class<T> clazz) {
return template.findById(id.toString(), clazz);
}
public <T> T getOne(Query query, Class<T> clazz) {
return template.findOne(query, clazz);
}
public <T> List<T> listByIds(Collection<? extends Serializable> idList, Class<T> clazz) {
return template.find(idInQuery(idList), clazz);
}
public <T> List<T> listByMap(Map<String, Object> columnMap, Class<T> clazz) {
return template.find(eqQuery(columnMap), clazz);
}
public <T> List<T> list(Class<T> clazz) {
return template.findAll(clazz);
}
public <T> List<T> list(Query query, Class<T> clazz) {
return template.find(query, clazz);
}
public <T> long count(Class<T> clazz) {
return template.count(EMPTY_QUERY, clazz);
}
public <T> long count(Query query, Class<T> clazz) {
return template.count(query, clazz);
}
public <T> PageUtils page(Map<String, Object> params, Query query, Class<T> clazz) {
IPage<T> page = new hss.server.utils.Query<T>().getPage(params);
page.setTotal(count(query, clazz));
Pageable pageable = PageRequest.of((int) page.getCurrent() - 1, (int) page.getSize());
query.with(pageable);
List<T> records = template.find(query, clazz);
page.setPages(page.getPages());
page.setRecords(records);
return new PageUtils(page);
}
}
分页工具类
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.List;
public class PageUtils implements Serializable {
private static final long serialVersionUID = 1L;
private int totalCount;
private int pageSize;
private int totalPage;
private int currPage;
private List<?> list;
public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
this.list = list;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.currPage = currPage;
this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
}
public PageUtils(IPage<?> page) {
this.list = page.getRecords();
this.totalCount = (int)page.getTotal();
this.pageSize = (int)page.getSize();
this.currPage = (int)page.getCurrent();
this.totalPage = (int)page.getPages();
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
Query query = new Query();
Criteria criteria1 = new Criteria();
QueryWrapper<Object> wrapper = new QueryWrapper<>();
Criteria 方法含义
方法名 | 释义 | 栗子 |
---|
where() | where | Criteria.where(“name”).is(“帅哥”) | andOperator() | and | Criteria.where(“name”).is(“帅哥”).andOperator(Criteria.where(“age”).is(18)); Criteria.andOperator(Criteria.where(“name”).is(“帅哥”), Criteria.where(“age”).is(25)); | orOperator() | or | Criteria.where(“name”).is(“美女”).orOperator(Criteria.where(“age”).is(18)); Criteria.orOperator(Criteria.where(“name”).is(“美女”), Criteria.where(“age”).is(25)); | is() | == | Criteria.where(“name”).is(“帅哥”) | not() | != | Criteria.where(“name”).not(“丑男”) | regex() | like | Criteria.where(“name”).regex(“.**?” +“帅哥”+ “.*”) | in() | in | Criteria.where(“age”).in(List)) | gt() | > | Criteria.where(“age”).gt(18) | gte() | >= | Criteria.where(“age”).gte(18) | lt() | < | Criteria.where(“age”).lt(18) | lte() | <= | Criteria.where(“age”).lte(18) | | | |
|