Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Nosql function in Spring boot

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to achieve Nosql function in Spring boot, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Step 1: check whether the X plug-in is installed

If you want mysql to support document storage, you need mysql to pre-install the X plug-in, log in to mysql, and then run the following command:

SHOW plugins

You can see the following effect:

Seeing that the mysqlx plug-in is active indicates that mysql now supports document storage, that is, mysql supports nosql.

Step 2: add dependency library implementation "mysql:mysql-connector-java" implementation "com.google.protobuf:protobuf-java" step 3: add Spring configuration-create SCHEMAcreate schema xxxx_db character set utf8;-- configuration user rights grant all privileges on xxxx_db.* to 'xxxx'@'localhost';application.propertiesmysqlx.datasource.url=mysqlx://localhost:33060/xxxx_db?user=xxxx&password=xxxxxmysqlx.datasource.schema=xxxx_db_sch in the database

Note: the password here, if there are special characters, needs to be encoded with a percent sign.

Mysqlx.javapackage com.xxx.properties;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Mysqlx {private String url; private String schema;} MysqlxConfig.javapackage 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;@Configurationpublic class MysqlxConfig {@ Bean ("mysqlx") @ ConfigurationProperties (prefix = "mysqlx.datasource") public Mysqlx mysqlx () {return new Mysqlx ();} @ Bean ("sessionFactory") public SessionFactory sessionFactory () {return new SessionFactory ();}} step 3:DAO layer calls xdevapiFormLogRepositoryImpl.javapackage 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;@Repositorypublic 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 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 ("read log object exception, content:\ n% s", dbDoc.toFormattedString () }) .findFirst ();} finally {if (session! = null) {session.commit (); session.close () } on how to achieve Nosql function in Spring boot to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report