一、mongo实体类的映射
实体类
/*
*功能:用户
*@author:victor.z
*@date:2018/8/17 下午5:37
*@version:1.0
*/
@Document(collection="user")
public class User implements Serializable {
@Id
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(id, user.id) &&
Objects.equals(name, user.name) &&
Objects.equals(age, user.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
}
二、通过mongoTemplate对mongo的CRUD
@Service
public class UserServiceImpl implements IUserService {
private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
public static final String KEY_ALL_USER = "child:user:all";
public static final String KEY_NAME_USER = "child:user:name:";
public static final String KEY_ID_USER = "child:user:id:";
@Autowired 这个别问我从哪里来的,是springboot本身就有这个bean如果你配置了spring.data.mongodb.uri=
MongoTemplate mongoTemplate;
@Autowired 这个是我自己定义的bean
RedisUtil redisUtil;
@Override
public List
List
//此处只判断为Null,防止redis穿透
if(Objects.isNull(userList)){
userList = mongoTemplate.findAll(User.class);
//即便mongo里user数量为0也要记录redis,防止穿透问题
if(ListUtil.isEmpty(userList)){
userList = Collections.EMPTY_LIST;
}
redisUtil.set(KEY_ALL_USER,userList,5);
log.info("{}:没有命中redis","findAll");
}else{
log.info("{}:命中redis","findAll");
}
return userList;
}
@Override
public List
List
//此处只判断为Null,防止redis穿透
if(Objects.isNull(userList)){
Criteria criteria = Criteria.where("name").is(name);
Query query = new Query(criteria);
userList = mongoTemplate.find(query,User.class);
//即便mongo里user数量为0也要记录redis,防止穿透问题
if(ListUtil.isEmpty(userList)){
userList = Collections.EMPTY_LIST;
}
redisUtil.set(KEY_NAME_USER,userList,5);
log.info("{}:没有命中redis","findByName");
}else{
log.info("{}:命中redis","findByName");
}
return userList;
}
@Override
public User findById(String id) {
User u = (User) redisUtil.get(id);
if(Objects.isNull(u)){
u = mongoTemplate.findById(id,User.class);
if(Objects.isNull(u)){
u = new User();
}
redisUtil.set(KEY_ID_USER,u,15);
}
return u;
}
@Override
public boolean insertUser(User user) {
try{
mongoTemplate.insert(user);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
@Override
public boolean insertUserList(List
try{
mongoTemplate.insert(userList,User.class);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
@Override
public boolean updateUserById(User u) {
Criteria criteria = Criteria.where("id").is(u.getId());
Update update = new Update();
update.set("name",u.getName()).set("age",u.getAge());
Query query = new Query(criteria);
try{
WriteResult wr = mongoTemplate.updateFirst(query,update,User.class);
return Objects.isNull(wr)?false:wr.getN()>0;
}catch (Exception e){
e.printStackTrace();
}
return true;
}
@Override
public boolean updateUserList(List
userList.forEach(user -> {
updateUserById(user);
});
return false;
}
@Override
public boolean deleteUserById(User u) {
Criteria criteria = Criteria.where("id").is(u.getId());
Query query = new Query(criteria);
try{
WriteResult wr = mongoTemplate.remove(query,User.class);
return Objects.isNull(wr)?false:wr.getN()>0;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
@Override
public boolean deleteUserList(List
userList.forEach(user -> {
deleteUserById(user);
});
return false;
}
}