您好,登錄后才能下訂單哦!
這篇文章給大家介紹Spring boot中怎么實(shí)現(xiàn)Nosql功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
如果想要mysql支持文檔型存儲(chǔ),需要mysql預(yù)先安裝好X插件,登錄mysql,然后,運(yùn)行如下命令,即可:
SHOW plugins;
看到如下效果即可:
看到mysqlx插件是激活狀態(tài),表明現(xiàn)在mysql支持文檔方式存儲(chǔ),即mysql支持nosql。
implementation "mysql:mysql-connector-java" implementation "com.google.protobuf:protobuf-java"
-- 創(chuàng)建數(shù)據(jù)庫(kù)里面的SCHEMA create schema xxxx_db character set utf8; -- 配置用戶權(quán)限 grant all privileges on xxxx_db.* to 'xxxx'@'localhost';
mysqlx.datasource.url=mysqlx://localhost:33060/xxxx_db?user=xxxx&password=xxxxx mysqlx.datasource.schema=xxxx_db_sch
注意:這里的密碼,如果有特殊的字符,需要使用百分號(hào)編碼的方式。
package com.xxx.properties; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Mysqlx { private String url; private String schema; }
package com.xxx.config; import com.xxx.properties.Mysqlx; import com.mysql.cj.xdevapi.Schema; import com.mysql.cj.xdevapi.Session; import com.mysql.cj.xdevapi.SessionFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; @Configuration public class MysqlxConfig { @Bean("mysqlx") @ConfigurationProperties(prefix = "mysqlx.datasource") public Mysqlx mysqlx() { return new Mysqlx(); } @Bean("sessionFactory") public SessionFactory sessionFactory() { return new SessionFactory(); } }
package com.xxx.dao; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.xxx.model.FormLog; import com.xxx.model.exception.HandleException; import com.mysql.cj.xdevapi.Schema; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @Repository public class FormLogRepositoryImpl implements FormLogRepository { private static final String name = "logs"; @Resource private Mysqlx mysqlx; @Resource private SessionFactory sessionFactory; @Resource private ObjectMapper jacksonObjectMapper; @Override public FormLog save(FormLog formLog) { Session session = sessionFactory.getSession(mysqlx.getUrl()); try { session.createSchema(mysqlx.getSchema(), true) .createCollection(name, true) .add(jacksonObjectMapper.writeValueAsString(formLog)) .executeAsync(); } catch (JsonProcessingException e) { e.printStackTrace(); } finally { if (session != null) { session.commit(); session.close(); } } return formLog; } @Override public Optional<FormLog> find(String id) { Session session = sessionFactory.getSession(mysqlx.getUrl()); try { return sessionFactory.getSession(mysqlx.getUrl()).createSchema(mysqlx.getSchema(), true).createCollection(name, true).find(String.format("$._id='%s'", id)).execute() .fetchAll().stream() .map( dbDoc -> { try { return jacksonObjectMapper.readValue(dbDoc.toString(), FormLog.class); } catch (IOException e) { throw new HandleException( String.format("讀取日志對(duì)象異常,內(nèi)容為:\n%s", dbDoc.toFormattedString())); } }) .findFirst(); } finally { if (session != null) { session.commit(); session.close(); } } } }
關(guān)于Spring boot中怎么實(shí)現(xiàn)Nosql功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。