spring整合mongoDB-3

使用起来就简单了。

还有像executeCommand支持复杂操作的接口。

使用Criteria可以构造Query,支持大于、小于、in等查询条件,类似于Hibernate的Criteria。



@Service("myService")
public class TestService {
private Logger log = Logger.getLogger(getClass());
private MongoTemplate mongoTemplate;

public List<User> findAll(String collectionName) {
return mongoTemplate.findAll(User.class, collectionName);
}

public void save(String collectionName, List<User> items) {
if (StringUtils.isNotEmpty(collectionName)) {
mongoTemplate.dropCollection(collectionName);
mongoTemplate.insert(items, collectionName);
} else {
log.error("name does not exist.");
}
}

public void delete(String collectionName) {
if (StringUtils.isNotEmpty(collectionName)) {
mongoTemplate.dropCollection(collectionName);
} else {
log.error("name does not exist.");
}
}

@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
}



但是说实话,像涉及到DBObject、Query这样的接口,一般使用起来都比较麻烦,我之前使用Hibernate的Criteria很有感触,所以比较反感使用这些。

比如,有一个深层嵌套的关联,User-Org-Address-postcode。

要按邮编和电话筛选,要用and把两个eq连接起来,但是使用eq之前,你还得判断中间的Org,Address是否为空,否则你调xxx.xxx.xxx.getPostCode会抛空指针。

这样写出来的代码真像某地的护城河,又臭又长。

所以在类似的api里写复杂查询的缺点如下:

1、代码冗长,开发效率低下;
2、逻辑不直观,容易出错,不好维护;
3、复杂的查询性能不好。比如可能在mongoDB里不能使用索引。这是很多nosql产品本身的特点决定的。
4、复杂的查询很难做cache,不容易优化;
5、复杂的查询不容易复用;
6、复杂的查询很难移植。特别是如果有一天要转向K-V存储的时候。
按需引入<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ant Design Vue 纯HTML项目示例</title> <!-- 引入Ant Design Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-vue@3.2.21/dist/antd.css"> <!-- 引入Ant Design X Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-x-vue@1.0.0/dist/antdx.css"> <!-- vue3引入 --> <script src="https://unpkg.com/dayjs/dayjs.min.js"></script> <script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekday.js"></script> <script src="https://unpkg.com/dayjs/plugin/localeData.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script> <!-- vue3 --> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <!-- antdv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.6/dist/antd.min.js"></script> <!-- antdxv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-x-vue@1.2.7/dist/index.umd.min.js"></script> </head> <body> <div id="app"></div> <script> const { createApp, ref, computed } = Vue; const { Button } = antd; const { Bubble, XProvider } = antdx; createApp({ template: ` <AXProvider :theme="{ algorithm: myThemeAlgorithm, }"> <div :style="{ padding: &#39;24px&#39;, backgroundColor: bgColor, }"> UMD <AXBubble content="hello bubble"></AXBubble> <AButton type="primary" @click="setLightTheme">Light</AButton> <AButton type="primary" @click="setDarkTheme">Dark</AButton> </div> </AXProvider> `, setup() { const { theme } = antd; const bgColor = ref("white"); const myThemeAlgorithm = ref(theme.defaultAlgorithm); const setLightTheme = () => { myThemeAlgorithm.value = theme.defaultAlgorithm; bgColor.value = "white"; }; const setDarkTheme = () => { myThemeAlgorithm.value = theme.darkAlgorithm; bgColor.value = "#141414"; }; return { myThemeAlgorithm, bgColor, setLightTheme, setDarkTheme }; } }) .use(XProvider) .use(Button) .use(Bubble) .mount("#app"); </script> <style> .container { max-width: 1200px; margin: 24px auto; padding: 0 16px; } .search-form { margin-bottom: 24px; padding: 16px; background-color: #f5f5f5; border-radius: 4px; } .user-table { margin-top: 16px; } .mb-6 { margin-bottom: 24px; } .mt-2 { margin-top: 8px; } </style> </body> </html>
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值