Mongodb操作与Java(四)MongoTemplate的使用

mongoClient和MongoTemplate二者区别

Springboot 操作 MongoDB 有两种方式。

第一种方式是采用 Springboot 官方推荐的 JPA 方式,这种操作方式,使用简单但是灵活性比较差。也就是MongoClient

第二种方式是采用 Spring Data MongoDB 基于 MongoDB 官方 Java API 封装的 MongoTemplate 操作类对 MongoDB 进行操作,这种方式非常灵活,能满足绝大部分需求。也就是MongoTemplate

本文将采用第二种方式进行介绍!

MongoClient

MongoClient 是 MongoDB 官方 Java 驱动库提供的类。可以理解为是mysql的Jdbc框架,它直接与 MongoDB 服务器进行通信,负责建立连接、发送查询和命令以及接收响应。使用 MongoClient 通常涉及到编写比较底层的代码。你需要自己管理连接、编写查询语句、处理结果集等。它提供了与 MongoDB 交互的最大灵活性,允许你执行几乎所有类型的数据库操作,包括那些 Spring Data MongoDB 可能尚未提供支持的操作。

MongoTemplate

定义MongoTemplate 是 Spring Data MongoDB 提供的一个高级抽象,它封装了 MongoClient,提供了一个更高层次的模板方法 API 来简化 MongoDB 的操作。可以理解为是mysql的Mybatis框架。MongoTemplate 提供了相对简单的方法来执行查询、更新、删除等操作,同时集成了 Spring 的转换和异常处理机制。使用 MongoTemplate,你不需要关心低层次的数据库连接和错误处理。

代码实践

配置

SpringBoot 工程依赖和配置

<!-- 引入springboot -->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version>
</parent><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

添加配置文件

在application.properties文件中添加mongodb相关配置!

#配置数据库连接地址
spring.data.mongodb.uri=mongodb://userName:password@127.0.0.1:27017/dbName

  • userName:表示用户名,根据实际情况填写即可

  • password:表示用户密码,根据实际情况填写即可

  • dbName:表示数据库,可以自定义,初始化数据的时候,会自动创建

准备代码

创建实体类

创建一个实体类Person,其中注解@Document(collection="persons")表示当前实体类对应的集合名称是persons,类似于关系型数据库中的表名称。

注解@Id表示当前字段,在集合结构中属于主键类型。

/*** 使用@Document注解指定集合名称*/
@Document(collection="persons")
public class Person implements Serializable {private static final long serialVersionUID = -3258839839160856613L;/*** 使用@Id注解指定MongoDB中的 _id 主键*/@Idprivate Long id;private String userName;private String passWord;private Integer age;private Date createTime;//...get/set@Overridepublic String toString() {return "Person{" +"id=" + id +", userName='" + userName + '\'' +", passWord='" + passWord + '\'' +", age=" + age +", createTime=" + createTime +'}';}
}

新增 

插入文档

MongoTemplate提供了insert()方法,用于插入文档,示例代码如下:

  • 用于插入文档

没指定集合名称时,会取 @Document注解中的集合名称

 

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 插入文档* @throws Exception*/@Testpublic void insert() throws Exception {Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());mongoTemplate.insert(person);}
}
  • 自定义集合名称,插入文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 自定义集合,插入文档* @throws Exception*/@Testpublic void insertCustomCollection() throws Exception {Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());mongoTemplate.insert(person, "custom_person");}
}
  • 自定义集合,批量插入文档

如果采用批量插入文档,必须指定集合名称
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 自定义集合,批量插入文档* @throws Exception*/@Testpublic void insertBatch() throws Exception {List<Person> personList = new ArrayList<>();Person person1 =new Person();person1.setId(10l);person1.setUserName("张三");person1.setPassWord("123456");person1.setCreateTime(new Date());personList.add(person1);Person person2 =new Person();person2.setId(11l);person2.setUserName("李四");person2.setPassWord("123456");person2.setCreateTime(new Date());personList.add(person2);mongoTemplate.insert(personList, "custom_person");}
}
存储文档

MongoTemplate提供了save()方法,用于存储文档。

在存储文档的时候会通过主键 ID 进行判断,如果存在就更新,否则就插入,示例代码如下:

  • 存储文档,如果没有插入,否则通过主键ID更新

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 存储文档,如果没有插入,否则更新* @throws Exception*/@Testpublic void save() throws Exception {Person person =new Person();person.setId(13l);person.setUserName("八八");person.setPassWord("123456");person.setAge(40);person.setCreateTime(new Date());mongoTemplate.save(person);}
}
  • 自定义集合,存储文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 自定义集合,存储文档* @throws Exception*/@Testpublic void saveCustomCollection() throws Exception {Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());mongoTemplate.save(person, "custom_person");}
}

更新

MongoTemplate提供了updateFirst()和updateMulti()方法,用于更新文档,示例代码如下:

  • 更新文档,匹配查询到的文档数据中的第一条数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 更新文档,匹配查询到的文档数据中的第一条数据* @throws Exception*/@Testpublic void updateFirst() throws Exception {//更新对象Person person =new Person();person.setId(1l);person.setUserName("张三123");person.setPassWord("123456");person.setCreateTime(new Date());//更新条件Query query= new Query(Criteria.where("id").is(person.getId()));//更新值Update update= new Update().set("userName", person.getUserName()).set("passWord", person.getPassWord());//更新查询满足条件的文档数据(第一条)UpdateResult result =mongoTemplate.updateFirst(query,update, Person.class);if(result!=null){System.out.println("更新条数:" + result.getMatchedCount());}}
}
  • 更新文档,匹配查询到的文档数据中的所有数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 更新文档,匹配查询到的文档数据中的所有数据* @throws Exception*/@Testpublic void updateMany() throws Exception {//更新对象Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());//更新条件Query query= new Query(Criteria.where("id").is(person.getId()));//更新值Update update= new Update().set("userName", person.getUserName()).set("passWord", person.getPassWord());//更新查询满足条件的文档数据(全部)UpdateResult result = mongoTemplate.updateMulti(query, update, Person.class);if(result!=null){System.out.println("更新条数:" + result.getMatchedCount());}}
}

 

删除文档

MongoTemplate提供了remove()、findAndRemove()和findAllAndRemove()方法,用于删除文档,示例代码如下:

  • 删除符合条件的所有文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 更新文档,匹配查询到的文档数据中的所有数据* @throws Exception*/@Testpublic void updateMany() throws Exception {//更新对象Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());//更新条件Query query= new Query(Criteria.where("id").is(person.getId()));//更新值Update update= new Update().set("userName", person.getUserName()).set("passWord", person.getPassWord());//更新查询满足条件的文档数据(全部)UpdateResult result = mongoTemplate.updateMulti(query, update, Person.class);if(result!=null){System.out.println("更新条数:" + result.getMatchedCount());}}
}
  • 删除符合条件的单个文档,并返回删除的文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 删除符合条件的单个文档,并返回删除的文档* @throws Exception*/@Testpublic void findAndRemove() throws Exception {Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());Query query = new Query(Criteria.where("id").is(person.getId()));Person result = mongoTemplate.findAndRemove(query, Person.class);System.out.println("删除的文档数据:" + result.toString());}
}
  • 删除符合条件的所有文档,并返回删除的文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 删除符合条件的所有文档,并返回删除的文档* @throws Exception*/@Testpublic void findAllAndRemove() throws Exception {Person person =new Person();person.setId(1l);person.setUserName("张三");person.setPassWord("123456");person.setCreateTime(new Date());Query query = new Query(Criteria.where("id").is(person.getId()));List<Person> result = mongoTemplate.findAllAndRemove(query, Person.class);System.out.println("删除的文档数据:" + result.toString());}
}

查询

 

MongoTemplate提供了非常多的文档查询方法,日常开发中用的最多的就是find()方法,示例代码如下:

  • 查询集合中的全部文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 查询集合中的全部文档数据* @throws Exception*/@Testpublic void findAll() throws Exception {List<Person> result = mongoTemplate.findAll(Person.class);System.out.println("查询结果:" + result.toString());}
}
  • 查询集合中指定的ID文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 查询集合中指定的ID文档数据* @throws Exception*/@Testpublic void findById() {long id = 1l;Person result = mongoTemplate.findById(id, Person.class);System.out.println("查询结果:" + result.toString());}
}
  • 根据条件查询集合中符合条件的文档,返回第一条数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据条件查询集合中符合条件的文档,返回第一条数据*/@Testpublic void findOne() {String userName = "张三";Query query = new Query(Criteria.where("userName").is(userName));Person result = mongoTemplate.findOne(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 根据条件查询集合中符合条件的文档

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据条件查询集合中符合条件的文档*/@Testpublic void findByCondition() {String userName = "张三";Query query = new Query(Criteria.where("userName").is(userName));List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 根据【AND】关联多个查询条件,查询集合中的文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据【AND】关联多个查询条件,查询集合中的文档数据*/@Testpublic void findByAndCondition() {// 创建条件Criteria criteriaUserName = Criteria.where("userName").is("张三");Criteria criteriaPassWord = Criteria.where("passWord").is("123456");// 创建条件对象,将上面条件进行 AND 关联Criteria criteria = new Criteria().andOperator(criteriaUserName, criteriaPassWord);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}
  • 根据【OR】关联多个查询条件,查询集合中的文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据【OR】关联多个查询条件,查询集合中的文档数据*/@Testpublic void findByOrCondition() {// 创建条件Criteria criteriaUserName = Criteria.where("userName").is("张三");Criteria criteriaPassWord = Criteria.where("passWord").is("123456");// 创建条件对象,将上面条件进行 OR 关联Criteria criteria = new Criteria().orOperator(criteriaUserName, criteriaPassWord);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 根据【IN】关联多个查询条件,查询集合中的文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据【IN】关联多个查询条件,查询集合中的文档数据*/@Testpublic void findByInCondition() {// 设置查询条件参数List<Long> ids = Arrays.asList(1l, 10l, 11l);// 创建条件Criteria criteria = Criteria.where("id").in(ids);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 根据【逻辑运算符】查询集合中的文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据【逻辑运算符】查询集合中的文档数据*/@Testpublic void findByOperator() {// 设置查询条件参数int min = 20;int max = 35;Criteria criteria = Criteria.where("age").gt(min).lte(max);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 根据【正则表达式】查询集合中的文档数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据【正则表达式】查询集合中的文档数据*/@Testpublic void findByRegex() {// 设置查询条件参数String regex = "^张*";Criteria criteria = Criteria.where("userName").regex(regex);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}
  • 根据单个条件查询集合中的文档数据,并按指定字段进行排序与限制指定数目

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 根据单个条件查询集合中的文档数据,并按指定字段进行排序与限制指定数目*/@Testpublic void findByConditionAndSortLimit() {String userName = "张三";//从第一行开始,查询2条数据返回Query query = new Query(Criteria.where("userName").is(userName)).with(Sort.by("createTime")).limit(2).skip(1);List<Person> result = mongoTemplate.find(query, Person.class);System.out.println("查询结果:" + result.toString());}
}

 

  • 统计集合中符合【查询条件】的文档【数量】

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;/*** 统计集合中符合【查询条件】的文档【数量】*/@Testpublic void countNumber() {// 设置查询条件参数String regex = "^张*";Criteria criteria = Criteria.where("userName").regex(regex);// 创建查询对象,然后将条件对象添加到其中Query query = new Query(criteria);long count = mongoTemplate.count(query, Person.class);System.out.println("统计结果:" + count);}
}

事务

单节点 mongodb 不支持事务,需要搭建 MongoDB 复制集才有事务,使用方式和mysql一样在springboot中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/12866.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

白酒:酒精度数对白酒风味的影响与品鉴技巧

云仓酒庄豪迈白酒作为品质的白酒品牌&#xff0c;其酒精度数对白酒风味的影响与品鉴技巧是品鉴爱好者关注的重点。酒精度数作为衡量白酒质量的一项重要指标&#xff0c;不仅决定了白酒的口感和风格&#xff0c;更在一定程度上体现了白酒的品质和价值。本文将探讨酒精度数对云仓…

Vue插槽总结

Vue.js中的插槽&#xff08;slot&#xff09;是一种机制&#xff0c;它允许你在子组件中预留位置&#xff0c;以便父组件可以在这个位置插入内容。插槽可以使组件更加灵活和可复用。Vue中主要有三种类型的插槽&#xff1a; 默认插槽&#xff1a; 语法&#xff1a;<slot>…

用友U8_dialog_moreUser_check.jsp SQL注入漏洞复现

简介 用友GRP-U8是用友软件针对政府及公共部门推出的管理软件产品。 GRP是Government Resource Planning的缩写,即政府资源计划。 这个产品设计用于满足政府部门在财务管理、人力资源管理、资产管理、供应链管理等方面的需求。 漏洞复现 FOFA: app="用友-GRP-U8&quo…

【Mysql数据库进阶02】第一范式~第四范式 Normal Form

第一范式~第四范式Normal Form 0 引言1 第一范式2 第二范式3 第三范式4 BC范式5 第四范式总结 0 引言 因为软考&#xff0c;我又重新拾起了数据库&#xff0c;那么到底如何去判断它属于第几范式呢 1 第一范式 设R是一个关系模式&#xff0c;R属于第一范式当且仅当R中每一个…

「AIGC算法」readLink实现url识别pdf、网页标题和内容

本文主要介绍AIGC算法,readLink实现url识别pdf、html标题和内容 一、设计思路 识别url是pdf或者网页网页处理逻辑,使用cheerio解析网页PDF处理逻辑,使用pdf-parse解析PDF文件自定义的函数来提取标题和内容二、可执行核心代码 const express = require("express")…

Zookeeper and RPC dubbo

javaguide zookeeper面试题 Zookeeper 啥是Zookeeper干啥的 ZooKeeper 可以被用作注册中心、分布式锁&#xff1b; ZooKeeper 是 Hadoop 生态系统的一员&#xff1b; 构建 ZooKeeper 集群的时候&#xff0c;使用的服务器最好是奇数台。 启动ZK 下载安装解压 不过多赘述 我的…

完整的软件定义网络(SDN)

实现一个完整的软件定义网络&#xff08;SDN&#xff09;以及部署自动化的方案需要详细的技术讨论和代码示例。在这个篇幅有限的平台上&#xff0c;我将提供一个概述性的指南&#xff0c;介绍 SDN 的基本概念、Python 实现 SDN 的关键技术、部署自动化的原理和实现方法。由于篇…

仿C#或Java基础类型自定义

class Int{ private:int _value 0; public:operator int() const{ // 隐式转换return _value;}// 显式转换explicit operator int*() const { return nullptr; }operator(const int page){_value page;}operator float() const{return static_cast<float>(_value);}ope…

字节跳动在2024年春季火山引擎Force原动力大会上隆重推出了“豆包大模型”家族

此次大会以AI为主题&#xff0c;聚焦大模型的应用与发展&#xff0c;旨在引领AI技术的落地和推动各行各业的数字化转型。 字节跳动官网&#xff1a;https://www.bytedance.com/zh/ 豆包官网&#xff1a;https://www.doubao.com/chat/ 更多消息&#xff1a;https://heehel.co…

Transformer - Self-Attention层的复杂度的计算

Transformer - Self-Attention层的复杂度的计算 flyfish 矩阵的维度 下面矩阵的维度是32即 3行&#xff0c;2列 6,10等都是矩阵里的元素 如果矩阵A的列数与矩阵B的行数相同&#xff0c;那么这两个矩阵可以相乘。即&#xff0c;若A是一个mn矩阵&#xff0c;B是一个np矩阵&am…

字符串第5/7题--右旋转字符串

题目描述&#xff1a; 字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k&#xff0c;请编写一个函数&#xff0c;将字符串中的后面 k 个字符移到字符串的前面&#xff0c;实现字符串的右旋转操作。 例如&#xff0c;对于输…

(论文笔记)TABDDPM:使用扩散模型对表格数据进行建模

了解diffusion model&#xff1a;什么是diffusion model? 它为什么好用&#xff1f; - 知乎 摘要 去噪扩散概率模型目前正成为许多重要数据模式生成建模的主要范式。扩散模型在计算机视觉社区中最为流行&#xff0c;最近也在其他领域引起了一些关注&#xff0c;包括语音、NLP…

nginx文件夹内文件解释<三>

koi-utf 文件解释 [rootrelease nginx]# more koi-utf # This map is not a full koi8-r <> utf8 map: it does not contain # box-drawing and some other characters. Besides this map contains # several koi8-u and Byelorussian letters which are not in koi8-r…

什么是Docker容器的基础镜像

2024年5月15日&#xff0c;周三下午 Docker 容器的基础镜像&#xff08;Base Image&#xff09;是创建新容器时使用的起始点。基础镜像是一个层叠的文件系统&#xff0c;包含了一系列操作系统的基础层&#xff0c;这些层可以包含操作系统、运行时环境、工具和库等。当使用 Dock…

k8s证书续期

证书即将到期了如何进行证书续签 k8s版本V1.23.6 1.查看证书期限 kubeadm certs check-expiration如果证书即将到期&#xff0c;此处的天数应该是几天&#xff0c;在过期之前进行续期&#xff0c;保证集群的可用 2. 备份证书 避免出现问题可以回退 cp -r /etc/kubernetes …

使用websocket和服务建立链接慢的原因分析

1、java 项目使用websocketHandler创建websocket服务&#xff0c;在拦截器HttpSessionHandshakeInterceptor中&#xff0c;beforeHandshake日志到的很快&#xff0c;afterHandshake很慢 建立链接一直在连接中 2、原因分析&#xff1a; 找到服务器上的进程名 jps -l 3、使用…

电脑数据丢失如何恢复?简单数据恢复的办法分享!

在使用电脑的过程中&#xff0c;数据丢失问题几乎是每位用户都可能遭遇的困境。那么&#xff0c;当电脑数据丢失时&#xff0c;我们该如何恢复呢&#xff1f;下面小编就分享几种电脑数据丢失后的恢复方法&#xff0c;轻松找回丢失的数据。 一、回收站找回 电脑上数据丢失的常…

error in ./src/assets/css/element-variables.scss

报错解释&#xff1a; 这个错误表明你的项目中使用的sass-loader需要node-sass的版本至少为4&#xff0c;但是当前项目依赖中的node-sass版本低于4。sass-loader是一个Webpack的loader&#xff0c;它允许你在编译Sass/SCSS文件时使用Node.js。 解决方法&#xff1a; 你需要将项…

java医院信息系统HIS源码SaaS模式Java版云HIS系统 接口技术RESTful API + WebSocket + WebService

java医院信息系统HIS源码SaaS模式Java版云HIS系统 接口技术RESTful API WebSocket WebService 云HIS是基于云计算的医疗卫生信息系统&#xff08;Cloud-Based Healthcare Information System&#xff09;&#xff0c;它运用云计算、大数据、物联网等新兴信息技术&#xff0c;…

java作用域

在面对对象中&#xff0c;变量作用是非常重要知识点 1 重要的变量是属性&#xff08;成员变量&#xff09;和局部变量 2局部变量在成员方法中定义的变量 cat类&#xff1a;cry 全局变量&#xff1a;属性。局部变量&#xff0c;方法中的变量 我们先来看看吧 主函数区 public cl…