Mongodb ORM是基于java的ORM框架,简化了SDK的使用,使代码变得更清晰、简单。
与Ibatis类似,将查询、执行语句封装在xml中,与代码隔离。简称MQL。
orm的支持包
1. 添加jar包或maven支持
com.mongodborm mongodb-orm 0.0.1-RELEASE
2. 初始化mongodb templet
spring中初始化
<bean id="mongoTemplet" class="com.mongodb.client.MongoClientTemplet"> <property name="factory"> <bean class="com.mongodb.client.MongoORMFactoryBean"> <property name="dataSource"> <bean class="com.mongodb.client.MongoDataSource"> <property name="nodeList" value="127.0.0.1:27017" /> <property name="dbName" value="your db name" /> <property name="userName" value="user name" /> <property name="passWord" value="password" /> <!-- 可使用默认值 --> <property name="connectionsPerHost" value="" /> <property name="threadsAllowedToBlock" value="" /> <property name="connectionTimeOut" value="" /> <property name="maxRetryTime" value="" /> <property name="socketTimeOut" value="" /> </bean> </property> <property name="configLocations"> <list> <value>classpath:mql/mongo-mql.xml</value> </list> </property> </bean> </property> </bean>
代码初始化
try { Resource resource = new ClassPathResource("mongo-mql.xml"); MongoORMFactoryBean factory = new MongoORMFactoryBean(); factory.setConfigLocations(new Resource[]{resource}); factory.init(); MongoClientTemplet templet = new MongoClientTemplet(); templet.setFactory(factory); templet.init(); } catch(Exception e) { e.printStackTrace(); }
Mapping
select
update/findAndModify
0,1
有嵌套的查询
Model model = mongoTemplet.findOne("queryModelList", "yuxiangping"); List<Model> list = mongoTemplet.findOne("queryModelList", ""); Model model = new Model(); model.setTime(1L); Map<String, String> action = new HashMap<String, String>(); action.put("name", "yuxiangping-update"); int update = mongoT emplet.update("updateModel", model, action);
更多的使用方法参见 sample.xml