Springboot整合MongoDB使用baseDao模式

通过baseDao封装通用接口

1.添加依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

2.applicaton.yml配置

这里端口修改为37017,可通过创建MongoDB配置文件修改日志、数据库、端口等配置

spring:
  data:
    mongodb:
      uri: mongodb://localhost:37017/dota

3.创建BaseDao接口

public interface BaseDao<T> {

    public List<T> findAll();

    public T findById(String id);

    public void insertEntity(T entity);

    public T getEntity(String name);

    public void deleteEntity(String name);
}

4.BaseDaoImpl实现

BaseDao子类通过传入泛型,反射得到对应的实体
collectionName为集合名称(可理解为数据库表的概念),MongoTemplate的api中如果不传collectionName,则集合名称默认为实体名称;
entityClass为传入泛型实体的类对象。

public class BaseDaoImpl<T> implements BaseDao<T> {

    private String collectionName;

    private Class<T> entityClass;

    public BaseDaoImpl(String collectionName) {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        this.entityClass = (Class<T>) pt.getActualTypeArguments()[0];
        this.collectionName = collectionName;
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<T> findAll() {
        return mongoTemplate.findAll(entityClass, collectionName);
    }

    @Override
    public T findById(String id) {
        return mongoTemplate.findById(id, entityClass, collectionName);
    }

    @Override
    public void insertEntity(T entity) {
        mongoTemplate.save(entity, collectionName);
    }

    @Override
    public T getEntity(String name) {
        Query query = new Query(Criteria.where(entityClass.getSimpleName() + "Name").is(name));
        return mongoTemplate.findOne(query, entityClass, collectionName);
    }

    @Override
    public void deleteEntity(String name) {
        Query query = new Query(Criteria.where(entityClass.getSimpleName() + "Name").is(name));
        mongoTemplate.remove(query, entityClass, collectionName);
    }

}

5.子类Dao实现

这里通过有参构造传入集合名称collectionName,如果有其它操作,可在这里扩展方法

@Repository
public class HeroDaoImpl extends BaseDaoImpl<Hero> implements HeroDao {

    public HeroDaoImpl() {
        super("hero");
    }

}

6._class域问题

MongoTemplate插入一个实体的时候,会自动在数据库加上一个字段:_class,value为当前实体的全路径类名,如果我们不需要,手动配置一下MappingMongoConverter,设置DefaultMongoTypeMapper的构造参数为null

    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoDbFactory mongoDbFactory,
                                                       MongoMappingContext mongoMappingContext, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        mappingMongoConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));

        //设置DefaultMongoTypeMapper构造参数为null,去掉"_class"域
        mappingMongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return mappingMongoConverter;
    }

你可能感兴趣的:(Springboot,Springboot,MongoDB)