mongodb - MongoRepository接口应用-验证码保存检查方法


mongodb - MongoRepository接口应用

CaptchaManager.java

package com.hssfg.core.utils.captcha;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 验证码保存检查方法
 * @author Twony
 *
 */
@Component
public class CaptchaManager {

	@Autowired
	private CaptchaRepository captchaRepository;
	

	/**
	 * 将验证码保存到MongoDB
	 * @param key
	 * @param value
	 */
	public void randCaptcha(String key, String value){
		
		// 存库
		Captcha captcha = new Captcha();
		captcha.setCptKey(key);
		captcha.setCptValue(value);
		captchaRepository.save(captcha);
	}

	/**
	 * 检查验证码是否正确
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean checkCaptcha(String key, String value) {
		Captcha captcha = captchaRepository.findByKey(key);
		if (captcha == null || !captcha.getCptValue().equals(value)) {
			return false;
		}
		return true;
	}
}


CaptchaRepository.java

package com.hssfg.core.utils.captcha;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface CaptchaRepository extends MongoRepository {

	@Query("{'cptKey':?0}")
	public Captcha findByKey(String kecptKeyy);

}


你可能感兴趣的:(数据库)