Development Notes: Use the MongoRepository class of SpringDataMongodb to perform addition, deletion, modification and query
Preface: This article is organized by the editor of Programming Notes#. It mainly introduces the knowledge related to adding, deleting, modifying and checking using the MongoRepository class of Spring Data Mongodb. I hope it will be of certain reference value to you. Spring Data Mongodb provides a set of quick methods to operate mongodb, create Dao, inherit MongoRepository, and specify entity types and primary key types. public interface CmsPageRepository extends MongoRepository { } 1. Paging query @ Test public void testFindPage() { int page = 0; //Start from 0 int size = 10;//Number of records per page Pageable pageable = PageRequest.of(page,size); Page all = cmsPageRepository.findAll(pageable); } 2. Add @Testpublic void testInsert(){ CmsPage cmsPage = new CmsPage(); cmsPageRepository.save(cmsPage); } 3. Delete @Test public void testDelete() { cmsPageRepository.deleteById(“5b17a2c511fe5e0c409e5eb3”) ;} 4. Modification @Test public void testUpdate() { Optional optional = cmsPageRepository.findOne(“5b17a34211fe5e2ee8c116c9”); if(optional.isPresent()){ CmsPage cmsPage = optional.get(); cmsPage.setPageName(” Test page 01″); cmsPageRepository.save(cmsPage); Like Spring Data JPA, Spring Data mongodb also provides rules for custom methods, as follows: Define methods according to rules such as ?ndByXXX, ?ndByXXXAndYYY, countByXXXAndYYY, etc. to implement query operations. public interface CmsPageRepository extends MongoRepository {//Query based on page name CmsPage findByPageName(String pageName); //Query based on page name and type CmsPage findByPageNameAndPageType(String pageName,String…
Example of webpack4+express+mongodb+vue implementing addition, deletion, modification and query
Before explaining, let’s take a look at the effect as follows: 1) The effect of the entire page is as follows: 2) The effect of adding new data is as follows: 3) The new addition is successful as follows: 4) The effect of editing data is as follows: 5) The result of successful editing is as follows: 6) The effect of deleting data is as follows: 7) The effect of successful deletion is as follows: 8) The query results are as follows: With the above effect, we will still do the same as before. Let’s first look at the structure of our entire project as follows: ### The directory structure is as follows: demo1 # project name | |— dist # Directory file generated after packaging | |— node_modules # All dependent packages | |—-database # Database related file directory | | |—db.js # Database connection operation of mongoose class library | | |—user.js # Schema Create model | | |—addAndDelete.js # Add, delete, modify and check operations | |— app | | |—index | | | |– views # Store all vue page files | | | | |– list.vue # List data | | | | |– index.vue…
Development notes: MongoDb addition, deletion, modification and query
Preface to this article: This article is organized by the editor of Programming Notes#. It mainly introduces the knowledge related to MongoDb’s addition, deletion, modification and query. I hope it will be of certain reference value to you. MongoDB creates a database Syntax The syntax format for MongoDB to create a database is as follows: use DATABASE_NAME If the database does not exist, create the database, otherwise switch to the specified database. Example In the following example we created the database runoob: > use span> runoobswitched to db runoob> dbrunoob > If you want to view all databases, you can use show dbs Command: > show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB> As you can see, the database runoob we just created is not in the list of databases. To display it, we need to insert some data into the runoob database. > db.runoob.insert({“name”:”Rookie Tutorial”})WriteResult( { “nInserted” : 1 })> show dbsadmin 0.000GBconfig 0.000GBlocal 0 .000GBrunoob 0.000GB The default database in MongoDB is test. If you No new database is created, the collection will be stored in the test database MongoDB deletes the database Syntax The syntax format of MongoDB to delete the database is as follows: db.dropDatabase() Delete the current database,…
Some trivial things about mogodb mongodb+express+node.js addition, deletion, modification and query
mongod.exe database operation mongo.exe command line query of the database Connect to the database It is mongod.exe mongo.exe can view the data table,So mongod.exe must be started before starting mongo.exe npm init will generate the package.json file,This is a package management file The file downloaded using npm install It will be recorded in the json file It is best to initialize it first before installing the file node_modules are installed using npm install Directory files mongodb command show collections is to query all tables db.Table name.count() queries the number of records db.Table name.find({}) queries all data Because I have also taken a lot of detours from mongo from ex3 to ex4. Now I will release a simple example. The changes from ex3 to ex4 will be commented later. I hope it can help more people. app.js var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); > var path = require('path'); var methodOverride = require('method-override');//Assist in processing POST request disguise PUT, DELETE and other HTTP methods //app.use(express.methodOverride()); var ejs = require('ejs'); var favicon=require('serve-favicon'); var logger=require('morgan');//This is a new version of logger /> var errorHandler = require('errorhandler');//install express.errorHandler() var bodyParser = require('body-parser' );//express.bodyParser old version var app = express();…
Django02 (project creation, data request migration, single table ORM addition, deletion, modification and query)
1. Creation and introduction of a Django project '' 'Install djangoInstall:pip3 install django==1.11.xView version number:django-admin –version Create projectNew project:1. Go to the target directory 2.django-admin startproject proj_nameproj_name:Project directory, Contains some of the most basic configurations of the project–__init__.py:Module configuration file–settings .py:Configuration total file–urls.py:url configuration file,All pages in the Django project need to be configured with url addresses–wsgi.py:(web server gateway interface) ,Server Gateway Interface,An interface for direct communication between python applications and web serverstemplates:Template folder,Storage of html files (pages),Supports the use of Django template language (DTL),You can also use a third party (jinja2)manage.py:Project Manager,Entrance to the command line toolset for interacting with the project,View all supported commands python3 manage.py''' 2. Creation and introduction of the application Introduction '''1.Django is for application development,in applications Complete specific business logic2. What is an application app: Just like a functional module in a project – a project can have multiple functional modules – but at least one – Django name It’s app3. How to create app(In the project directory):python3 manage.py startapp app01migrations:data migration (transplantation) module,The contents are all written by Django Automatically generated– __init__.py__init__.pyadmin.py:Backend management system configuration of the applicationapps.py:After django 1.9,Relevant configuration of this application models.py &# Scriptviews.py: executes the corresponding logical code module'''…
Django’s entry-level CMDB system (4) addition, deletion, modification and query
Getting Started with Django CMDB System (4) Add, Delete, Modify and Check Foreword Author: He Quan, github address: https://github.com/hequan2017 QQ communication group: 620176501 Through this tutorial, you can get started from scratch and be able to independently write a simple CMDB system. The current mainstream method development methods are divided into two types: mvc and mvvc. This tutorial is based on the mvc method, that is, django is responsible for rendering html. An introductory tutorial on mvvc (front-end and back-end separation) will be launched later. Tutorial project address: https://github.com/hequan2017/husky/ Tutorial document address: https://github.com/hequan2017/husky/tree/master/ doc Description cbv Use classes in the view to process requests (the following method is more abstract, but simpler) fbv Use functions in the view to process requests Basic settings pycharm: Menu bar tools –> Select run manage.py task manage.py@husky > startapp asset ##Create asset app Please see the actual page for specific content. The following only shows the key codes. settings.py Add system configuration import sysINSTALLED_APPS = [ “asset”,] asset/modes.py from django.db import modelsclass Ecs(models. Model): TYPE_CHOICES = ( (‘Alibaba Cloud’, ‘Alibaba Cloud’), (‘Tencent Cloud’, ‘Tencent Cloud’), (‘Huawei Cloud’, ‘Huawei Cloud’), (‘Amazon’, ‘Amazon’), (‘Other’, ‘Other’), (None,None), ) hostname = models. CharField(max_length=96, verbose_name=’Host Name’, blank=True, null=True, )…
django–ORM implements author addition, deletion, modification and query
Foreplay Previously we have implemented the addition, deletion, modification and search of the publishing house, the addition, deletion, modification and search of the book, and the corresponding relationship between the book and the publishing house. Now let’s write about the correspondence between the author’s additions, deletions, modifications and the book. What is the relationship between the book and the author? An author can write multiple books, and a book can have multiple authors, so there is a many-to-many relationship between books and authors. In this case, we need a table to record the relationship between books and authors. Think about the SQL statement: How to create a table — Create author table create table author( id int primary key auto_increment, name varchar(32) not null ); — Create a relationship table between authors and books create table author2book( id int primary key auto_increment, author_id int not null, book_id int not null, constraint fk_author foreign key (author_id) references author(id) on delete cascade on update cascade, constraint fk_book foreign key (book_id) references book(id) on delete cascade on update cascade ); ORM creates table Is it troublesome to create a table using the above SQL statement? How do we use ORM to create a…
Java connection to database, and examples of addition, deletion, modification and query
Customize the util class for connecting to the database package com.shuzf.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtil { // Define the path to the driver class private static final String DRIVER = “oracle.jdbc.driver.OracleDriver”; //Define the URL used to connect to the database private static final String URL = “jdbc:oracle:thin****l”; //Define the username and password used to access the database private static final String USERNAME = “s****t”; private static final String PASSWORD = “t***r”; //Load driver class static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //Define the method to get the connection public static Connection getConnection() { Connection cOnn= null; ; try { cOnn= DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } //Define a method to release database resources public static void destroy(Connection con, Statement stat, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } Basic classes package com.shuzf.jdbc; public…
Java basic development JDBC operation database addition, deletion, modification and query, detailed explanation of paging query examples
The operations on the database are nothing more than additions, deletions, modifications and queries, among which the numerical query operation is the most complex, so the query will be explained separately. The Mysql database I use here Add, delete, modify and check operations Paging query operation 1. Query results are returned in list 2. Query results are returned as jsonArray 3. Query the total number of records Let’s take a look at the relevant configuration information first public static final String USER_NAME = “root”; public static final String PWD = “123456789”; public static final String DRIVER = “com.mysql.jdbc.Driver”; public static final String URL = “jdbc:mysql://localhost:3306/web_demon”; /** Paging query default number of records per page */ public static final int PAGE_SIZE_DEFAULT = 10; Add, delete, and modify operations Get the database connection object /** * Get database connection * @return database connection object */ public Connection getConnection(Connection conn) { if(cOnn== null){ try { Class.forName(Config.DRIVER); cOnn= DriverManager.getConnection(Config.URL, Config.USER_NAME, Config.PWD); } catch (Exception e) { e.printStackTrace(); } } return conn; } Encapsulate addition, deletion and modification operations /** * Add, delete, insert operations * @param sql sql statement executed * For example: add the statement “insert into user (name,sex) values (?,?)”; * Delete…