Springboot統合mongo
1770 ワード
学習チュートリアル: https://blog.csdn.net/vbirdbest/article/details/76037827 http://blog.didispace.com/springbootmongodb/
クライアントのデフォルト設定 ipデフォルトはlocalhost ポートのデフォルトは27017 です. databaseのデフォルトはtestで、mongodbのデフォルトは2つのデータベースadmin、local です.
導入依存
クイックスタート
すべては自動構成を使用し、デフォルトではtestデータベースを使用します.このループはmybatisを使用するのと同じで、xmlのsql文を構成する必要がないため、mybatisを使用するよりも簡単です.
保存するBoxVideoPageエンティティの作成
インタフェースを作る
使用
mongo shellを開き、挿入したデータを表示します.
関連する構成を行う
デフォルト設定を使用せずに、関連する構成
クライアントのデフォルト設定
導入依存
org.springframework.boot
spring-boot-starter-data-mongodb
クイックスタート
すべては自動構成を使用し、デフォルトではtestデータベースを使用します.このループはmybatisを使用するのと同じで、xmlのsql文を構成する必要がないため、mybatisを使用するよりも簡単です.
保存するBoxVideoPageエンティティの作成
public class BoxVideoPage {
@Id
private Long id;
private Integer boxId;
private String boxVideoPageUrl;
private String boxVideoPageHtml;
public BoxVideoPage(Long id, Integer boxId, String boxVideoPageUrl, String boxVideoPageHtml) {
this.id = id;
this.boxId = boxId;
this.boxVideoPageUrl = boxVideoPageUrl;
this.boxVideoPageHtml = boxVideoPageHtml;
}
// getter and setter
インタフェースを作る
public interface BoxVideoPageMapper extends MongoRepository {
BoxVideoPage findByBoxId(Integer boxId);
}
使用
@RequestMapping("test/box-video")
public String getBoxVideo(){
boxVideoPageMapper.save(new BoxVideoPage(2L,2,"pageUrl2","html2"));
System.out.println(boxVideoPageMapper.findByBoxId(1));
return "";
}
mongo shellを開き、挿入したデータを表示します.
use test
db.boxVideoPage.find()
関連する構成を行う
デフォルト設定を使用せずに、関連する構成
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test