Springboot配置MongoDB连接
<p> 之前有个项目,用的是Springboot框架,对接的数据库是mongodb,当时花了一些时间去做这个配置MongoDB的连接,现在把这个过程记录下来,以免遗忘。</p><p>一、在pom中添加依赖。</p>
<table style="width: 1085px; height: 106px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(128, 128, 128, 1)"><!-- 增加mongodb支持 --><br><span style="color: rgba(232, 191, 106, 1)"><dependency><br><span style="color: rgba(232, 191, 106, 1)"> <groupId>org.springframework.boot<span style="color: rgba(232, 191, 106, 1)"></groupId><br><span style="color: rgba(232, 191, 106, 1)"> <artifactId>spring-boot-starter-data-mongodb<span style="color: rgba(232, 191, 106, 1)"></artifactId><br><span style="color: rgba(232, 191, 106, 1)"> <version>1.5.9.RELEASE<span style="color: rgba(232, 191, 106, 1)"></version><br><span style="color: rgba(232, 191, 106, 1)"></dependency></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<p>二、配置数据源</p>
<p> 在项目中,使用的配置文件是yaml格式的,所以配置信息如下:</p>
<table style="width: 1089px; height: 101px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(204, 120, 50, 1)">spring:<br> <span style="color: rgba(204, 120, 50, 1)">data:<br> <span style="color: rgba(204, 120, 50, 1)">mongodb:<br> <span style="color: rgba(204, 120, 50, 1)">uri: mongodb://地址</span></span></span><br></span></pre>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p> 如果使用的是properties格式的话,配置信息则是;</p>
<table style="width: 1102px; height: 22px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(204, 120, 50, 1)">spring.<span style="color: rgba(204, 120, 50, 1)">data.<span style="color: rgba(204, 120, 50, 1)">mongodb.<span style="color: rgba(204, 120, 50, 1)">uri= mongodb://地址</span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<p>三、在实体中添加注解</p>
<p> 在实体添加@Document注解,collection= "对应的表名"。</p>
<p> 在属性上添加@Filed注解,值为对应的字段名。</p>
<table style="width: 1094px; height: 334px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(187, 181, 41, 1)">@Getter<br><span style="color: rgba(187, 181, 41, 1)">@Setter<br><span style="color: rgba(187, 181, 41, 1)">@Document(<span style="color: rgba(208, 208, 255, 1)">collection = <span style="color: rgba(106, 135, 89, 1)">"User")<br><span style="color: rgba(204, 120, 50, 1)">public class User {<br> <span style="color: rgba(128, 128, 128, 1)">/*<br><span style="color: rgba(128, 128, 128, 1)"> 用户id:<br><span style="color: rgba(128, 128, 128, 1)"> */<br> <span style="color: rgba(128, 128, 128, 1)"><span style="color: rgba(204, 120, 50, 1)"><span style="color: rgba(152, 118, 170, 1)"><span style="color: rgba(204, 120, 50, 1)"><span style="color: rgba(204, 120, 50, 1)"><span style="color: rgba(128, 128, 128, 1)"><span style="color: rgba(128, 128, 128, 1)"><span style="color: rgba(128, 128, 128, 1)"><span style="color: rgba(128, 128, 128, 1)"><span style="color: rgba(187, 181, 41, 1)">@Field(<span style="color: rgba(106, 135, 89, 1)">"id")</span></span></span></span></span></span></span></span></span></span></span><br><span style="color: rgba(128, 128, 128, 1)"> <span style="color: rgba(204, 120, 50, 1)">private String <span style="color: rgba(152, 118, 170, 1)">id<span style="color: rgba(204, 120, 50, 1)">;<br><span style="color: rgba(204, 120, 50, 1)"> <span style="color: rgba(128, 128, 128, 1)">/*<br><span style="color: rgba(128, 128, 128, 1)"> 用户名<br><span style="color: rgba(128, 128, 128, 1)"> */<br><span style="color: rgba(128, 128, 128, 1)"> <span style="color: rgba(187, 181, 41, 1)">@Field(<span style="color: rgba(106, 135, 89, 1)">"UserName")<br> <span style="color: rgba(204, 120, 50, 1)">private String <span style="color: rgba(152, 118, 170, 1)">userName<span style="color: rgba(204, 120, 50, 1)">;<br><span style="color: rgba(204, 120, 50, 1)"> <span style="color: rgba(128, 128, 128, 1)">/*<br><span style="color: rgba(128, 128, 128, 1)"> 性别<br><span style="color: rgba(128, 128, 128, 1)"> */<br><span style="color: rgba(128, 128, 128, 1)"> <span style="color: rgba(187, 181, 41, 1)">@Field(<span style="color: rgba(106, 135, 89, 1)">"sex")<br> <span style="color: rgba(204, 120, 50, 1)">private String <span style="color: rgba(152, 118, 170, 1)">sex<span style="color: rgba(204, 120, 50, 1)">;<br><span style="color: rgba(204, 120, 50, 1)">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p>四、在业务层使用MongoDb的方法:</p>
<table style="width: 1093px; height: 22px" border="0">
<tbody>
<tr>
<td>
<p>@Service<br>public class UserServiceImpl implements UserService {<br><br> @Resource<br> private MongoTemplate mongoTemplate;<br><br><br> @Override<br> public long getCount() {<br> Query query = new Query();<br> long count = mongoTemplate.count(query,User.class);<br> return count;<br> }</p>
<p>}</p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p>五、MongoTemplate常使用的方法</p>
<p> 1.插入</p>
<table style="width: 1111px; height: 22px" border="0">
<tbody>
<tr>
<td>
<pre name="code" class="prettyprint"><code class="hljs javascript has-numbering"> mongoTemplate.insert(<span class="hljs-built_in">Object);</span></code></pre>
</td>
</tr>
</tbody>
</table>
<p> 2.删除</p>
<table style="width: 1109px; height: 22px" border="0">
<tbody>
<tr>
<td>
<pre name="code" class="prettyprint"><code class="hljs vbnet has-numbering"> Query query=<span class="hljs-keyword">new Query(Criteria.<span class="hljs-keyword">where(<span class="hljs-string">"_id").<span class="hljs-keyword">is(id));
mongoTemplate.remove(query,AutomaticAlarm.<span class="hljs-keyword">class); </span></span></span></span></span></code></pre>
</td>
</tr>
</tbody>
</table>
<p> 3. 修改</p>
<table style="width: 1109px; height: 22px" border="0">
<tbody>
<tr>
<td>
<p> Query query=new Query(Criteria.where("_id").is(id));</p>
<p> Update update = Update.update("要更新的字段", "更新的值");</p>
<p> mongoTemplate.updateFirst(query, update, Object.class);<br><br></p>
</td>
</tr>
</tbody>
</table>
<p> 4.查询</p>
<table style="width: 1110px; height: 452px" border="0">
<tbody>
<tr>
<td>1.查找所有<br><br> mongoTemplate.findAll(Object.class);<br><br>2.条件查询(具体某个字段的值)<br><br> Query query=new Query(Criteria.where("字段1").is("值1"));<br> mongoTemplate.find(query, Object.class);<br><br>3.条件查询(大于小于)<br><br> Criteria criteria = Criteria.where("字段").gte(某个值).lte(某个值);<br> Query query = new Query(criteria);<br> mongoTemplate.find(query, Object.class);<br><br>4.模糊查询<br><br> Pattern pattern = Pattern.compile("^.*" + searchKey + ".*$");//这里时使用的是正则匹配,searchKey是关键字,接口传参,也可以自己定义。<br> Criteria criteria = Criteria.where("_id").regex(pattern);<br> mongoTemplate.find(query, Object.class); <br><br>5.分页查询<br><br> Query query = new Query();<br> query.skip("跳过的数据条数").limit("一页的数据条数"); <br> mongoTemplate.find(query, Object.class);<br> <br>6.聚合查询<br> Aggregation aggregation1 = Aggregation.newAggregation(Aggregation.group("sex").count().as("peopleCount"));//这里的聚合条件由自己定义<br> AggregationResults<BasicDBObject> outputTypeCount1 = mongoTemplate.aggregate(aggregation1, "User", BasicDBObject.class);//取出的结果需要自行进行处理,比如可以用getMappedResults来转换</td>
</tr>
</tbody>
</table>
<p>六、增强配置</p>
<p> 如果我们在项目中需要管理MongoDB的最大连接时长、socket保持活跃、最大等待时长等,那么我们在pom文件中需要引入一个增强管理包。</p>
<table style="width: 1113px; height: 102px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(128, 128, 128, 1)"><!-- 增加mongoplus支持 --><br><span style="color: rgba(232, 191, 106, 1)"><dependency><br><span style="color: rgba(232, 191, 106, 1)"> <groupId>com.spring4all<span style="color: rgba(232, 191, 106, 1)"></groupId><br><span style="color: rgba(232, 191, 106, 1)"> <artifactId>mongodb-plus-spring-boot-starter<span style="color: rgba(232, 191, 106, 1)"></artifactId><br><span style="color: rgba(232, 191, 106, 1)"> <version>1.0.0.RELEASE<span style="color: rgba(232, 191, 106, 1)"></version><br><span style="color: rgba(232, 191, 106, 1)"></dependency></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<p> 同时在项目启动类SpringBootMainApplication上添加一个注解@EnableMongoPlus。</p>
<p> 我们就可以在配置文件中添加这些配置了。</p>
<table style="width: 1115px" border="0">
<tbody>
<tr>
<td>
<pre><span style="color: rgba(204, 120, 50, 1)">spring:</span><br><span style="color: rgba(204, 120, 50, 1)">data:<br> <span style="color: rgba(204, 120, 50, 1)">mongodb:<br> <span style="color: rgba(204, 120, 50, 1)">uri: mongodb://地址<br> <span style="color: rgba(204, 120, 50, 1)">option:<br> <span style="color: rgba(204, 120, 50, 1)">socket-keep-alive: <span style="color: rgba(204, 120, 50, 1)">true<br><span style="color: rgba(204, 120, 50, 1)"> max-connection-idle-time: <span style="color: rgba(104, 151, 187, 1)">60000<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># connect-timeout: 36000<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># min-connection-per-host: 5<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># threads-allowed-to-block-for-connection-multiplier: 5<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># max-wait-time: 120000<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># socket-timeout: 0<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># max-connection-life-time: 0<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># heartbeat-socket-timeout: 36000<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># heartbeat-connect-timeout: 36000<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># min-heartbeat-frequency: 5<br><span style="color: rgba(98, 151, 85, 1); font-style: italic"># heartbeat-frequency: 10</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<p><span style="color: rgba(187, 181, 41, 1)"> </span></p><br><br>
来源:https://www.cnblogs.com/kylne/p/11193878.html
頁:
[1]