美文网首页
Jpa 操作mongodb

Jpa 操作mongodb

作者: 架构师与哈苏 | 来源:发表于2020-03-31 14:33 被阅读0次

    MondoDB实体类

    package com.meeno.inner.oa.common.employee.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.Setter;
    import org.springframework.data.annotation.CreatedBy;
    import org.springframework.data.annotation.CreatedDate;
    import org.springframework.data.annotation.LastModifiedBy;
    import org.springframework.data.annotation.LastModifiedDate;
    import org.springframework.data.jpa.domain.support.AuditingEntityListener;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    import javax.persistence.EntityListeners;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import java.util.Date;
    
    /**
     * @description: 员工登录记录
     * @author: Wzq
     * @create: 2020-03-19 17:50
     */
    @Document
    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @EntityListeners(AuditingEntityListener.class)
    public class EmployeeLoginRecord {
    
        @Id
        private Long objectId;
    
        /**
         * 用户id
         */
        private Long userId;
    
        /**
         * 登录时间
         */
        private Date loginDate;
    
        @CreatedDate
        private Date createDate;
    
        @LastModifiedDate
        private Date UpdateDate;
    
        @CreatedBy
        private Long creatorId;
    
        @LastModifiedBy
        private Long updateId;
    
        public EmployeeLoginRecord(Long userId, Date loginDate) {
            this.userId = userId;
            this.loginDate = loginDate;
        }
    }
    
    

    Jpa Repository

    package com.meeno.inner.oa.common.employee.repository;
    
    import com.meeno.inner.oa.common.employee.entity.EmployeeLoginRecord;
    import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
    import org.springframework.data.mongodb.repository.MongoRepository;
    import org.springframework.stereotype.Repository;
    
    /**
     * @description: 员工登录记录Repository
     * @author: Wzq
     * @create: 2020-03-20 09:59
     */
    @Repository
    public interface EmployeeLoginRecordRepository extends MongoRepository<EmployeeLoginRecord,Long>{
    
        Integer countByUserId(Long userId);
    
    }
    
    

    使用JPA

    @Autowired
        MongoTemplate mongoTemplate;
        @Autowired
        EmployeeLoginRecordRepository employeeLoginRecordRepository;
    
    
     @Override
        public void addLoginRecord(Long userId) {
            EmployeeLoginRecord record = new EmployeeLoginRecord(userId, new Date());
            this.mongoTemplate.insert(record);
        }
    
     @Override
        public Boolean isFirstLogin(Long userId) {
            //mysql中记录的是否第一次登陆
            Employee employee = this.employeeRepository.getByUserId(userId);
            Integer isFirstLogin = employee == null ? null : employee.getIsFirstLogin();
            boolean isMysqlFirstLogin  = (isFirstLogin == null || !Constants.NOT_IS_FIRST_LOGIN.equals(isFirstLogin));
            //mongodb 判断是否第一次登陆
            Integer count = this.employeeLoginRecordRepository.countByUserId(userId);
            count = count == null ? 0 : count;
            boolean isMongoFirstLoginCount = count.intValue() < 1 ? true : false;
            //mysql 和 mongodb中一个找个不是第一次登陆就返回true
            if (isMysqlFirstLogin || isMongoFirstLoginCount) {
                return true;
            }
            return false;
        }
    

    个人微信公众,经常更新一些实用的干货:


    image.png

    相关文章

      网友评论

          本文标题:Jpa 操作mongodb

          本文链接:https://www.haomeiwen.com/subject/sqmwuhtx.html