MVP模式

1、创建数据库连接类:

package com.db.mvp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//数据库连接类
public class DatabaseManager {private static DatabaseManager instance;private Connection connection;private DatabaseManager() {// 私有构造函数,禁止外部直接创建实例}public static synchronized DatabaseManager getInstance() {if (instance == null) {instance = new DatabaseManager();}return instance;}public Connection getConnection() throws SQLException {if (connection == null || connection.isClosed()) {// 数据库连接配置String url = "jdbc:mysql://localhost:3306/test_data";String username = "root";String password = "admin";connection = DriverManager.getConnection(url, username, password);}return connection;}
}

2、创建Model的实体类:

package com.db.mvp;
//实体类
public class WordsInfo {private int id;private String engName;private String chiVal;private String lastUsedTime;private int usedTimes;private String createdTime;private int priority;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getEngName() {return engName;}public void setEngName(String engName) {this.engName = engName;}public String getChiVal() {return chiVal;}public void setChiVal(String chiVal) {this.chiVal = chiVal;}public String getLastUsedTime() {return lastUsedTime;}public void setLastUsedTime(String lastUsedTime) {this.lastUsedTime = lastUsedTime;}public int getUsedTimes() {return usedTimes;}public void setUsedTimes(int usedTimes) {this.usedTimes = usedTimes;}public String getCreatedTime() {return createdTime;}public void setCreatedTime(String createdTime) {this.createdTime = createdTime;}public int getPriority() {return priority;}public void setPriority(int priority) {this.priority = priority;}
}

3、创建Model类:

package com.db.mvp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.db.daomodule.DatabaseManager;
//Model层
public class WordsInfoModel {private Connection connection;public List<WordsInfo> getAllWordsInfo() {List<WordsInfo> wordsInfoList = new ArrayList<>();try {connection = DatabaseManager.getInstance().getConnection();Statement statement = connection.createStatement();String sql = "SELECT * FROM words_info";ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()) {WordsInfo wordsInfo = new WordsInfo();wordsInfo.setId(resultSet.getInt("id"));wordsInfo.setEngName(resultSet.getString("eng_name"));wordsInfo.setChiVal(resultSet.getString("chi_val"));wordsInfo.setLastUsedTime(resultSet.getString("last_used_time"));wordsInfo.setUsedTimes(resultSet.getInt("used_times"));wordsInfo.setCreatedTime(resultSet.getString("created_time"));wordsInfo.setPriority(resultSet.getInt("priority"));wordsInfoList.add(wordsInfo);}resultSet.close();statement.close();connection.close();} catch (SQLException e) {e.printStackTrace();}return wordsInfoList;}public void insertWordsInfo(WordsInfo wordsInfo) {try {connection = DatabaseManager.getInstance().getConnection();String sql = "INSERT INTO words_info (eng_name, chi_val, last_used_time, used_times, created_time, priority) VALUES (?, ?, ?, ?, ?, ?)";PreparedStatement statement = connection.prepareStatement(sql);statement.setString(1, wordsInfo.getEngName());statement.setString(2, wordsInfo.getChiVal());statement.setString(3, wordsInfo.getLastUsedTime());statement.setInt(4, wordsInfo.getUsedTimes());statement.setString(5, wordsInfo.getCreatedTime());statement.setInt(6, wordsInfo.getPriority());statement.executeUpdate();statement.close();connection.close();} catch (SQLException e) {e.printStackTrace();}}public void updateWordsInfo(WordsInfo wordsInfo) {try {connection = DatabaseManager.getInstance().getConnection();String sql = "UPDATE words_info SET eng_name=?, chi_val=?, last_used_time=?, used_times=?, created_time=?, priority=? WHERE id=?";PreparedStatement statement = connection.prepareStatement(sql);statement.setString(1, wordsInfo.getEngName());statement.setString(2, wordsInfo.getChiVal());statement.setString(3, wordsInfo.getLastUsedTime());statement.setInt(4, wordsInfo.getUsedTimes());statement.setString(5, wordsInfo.getCreatedTime());statement.setInt(6, wordsInfo.getPriority());statement.setInt(7, wordsInfo.getId());statement.executeUpdate();statement.close();connection.close();} catch (SQLException e) {e.printStackTrace();}}public void deleteWordsInfo(int id) {try {connection = DatabaseManager.getInstance().getConnection();String sql = "DELETE FROM words_info WHERE id=?";PreparedStatement statement = connection.prepareStatement(sql);statement.setInt(1, id);statement.executeUpdate();statement.close();connection.close();} catch (SQLException e) {e.printStackTrace();}}
}

4、创建Presenter层:

package com.db.mvp;
import java.util.List;
//Presenter层,同时持有Model对象和View对象。
public class WordsInfoPresenter {private WordsInfoModel model;private WordsInfoView view;public WordsInfoPresenter(WordsInfoView view) {this.view = view;model = new WordsInfoModel();}public void getAllWordsInfo() {List<WordsInfo> wordsInfoList = model.getAllWordsInfo();view.showWordsInfo(wordsInfoList);}public void insertWordsInfo(WordsInfo wordsInfo) {model.insertWordsInfo(wordsInfo);getAllWordsInfo(); // 刷新数据显示}public void updateWordsInfo(WordsInfo wordsInfo) {model.updateWordsInfo(wordsInfo);getAllWordsInfo(); // 刷新数据显示}public void deleteWordsInfo(int id) {model.deleteWordsInfo(id);getAllWordsInfo(); // 刷新数据显示}
}

5、创建View层:

package com.db.mvp;
import java.util.List;
//View层
public interface WordsInfoView {void getAllWordsInfo();void showWordsInfo(List<WordsInfo> wordsInfoList); 
}

6、创建UserView类实现View层接口,并进行测试:

package com.db.mvp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;//在MVP模式中,View层负责与用户交互和展示数据的内容,
//Presenter层负责处理业务逻辑并更新View层,
//Model层负责存储和管理数据。
//MVC与MVP模式的主要区别如下:
//职责分配:
//在MVC模式中,Model负责管理应用程序的数据和业务逻辑,View负责呈现数据给用户,Controller负责接受用户的输入并作出响应。
//在MVP模式中,Model同样负责管理数据和业务逻辑,View负责呈现数据给用户,
//Presenter作为中间人负责处理用户的输入,将输入与Model交互,并更新View。
//数据流:
//在MVC模式中,数据流是双向的。用户的输入通过Controller传递给Model进行处理,处理后的数据通过View呈现给用户。
//同时,Model也可以直接更新View。
//在MVP模式中,数据流是单向的。用户的输入通过View传递给Presenter进行处理,Presenter将数据交给Model进行处理,
//处理完成后,Model将结果交给Presenter,再由Presenter更新View。
//单向依赖:
//在MVC模式中,View依赖于Controller和Model,Controller依赖于View和Model,Model独立存在。
//在MVP模式中,View只依赖于Presenter,Presenter依赖于View和Model,Model独立存在。
//可测试性:
//在MVC模式中,由于Controller处理用户的输入和输出,因此可以很容易地对Controller进行单元测试。
//在MVP模式中,由于Presenter处理用户的输入和输出,因此可以很容易地对Presenter进行单元测试。
public class UserView implements WordsInfoView {private WordsInfoPresenter presenter;
//this关键字指当前类的实例,即UserView类的实例。
//在UserView类的构造方法中,实例化一个WordsInfoPresenter对象,并将this作为参数传递给它。
//这样做的目的是将WordsInfoView接口的实现传递给WordsInfoPresenter,
//以便在WordsInfoPresenter中调用WordsInfoView接口的方法。
//通过将this传递给WordsInfoPresenter,
//WordsInfoPresenter就可以调用UserView类中实现的showWordsInfo方法,以便在控制台上显示单词信息。public UserView() {presenter = new WordsInfoPresenter(this);}public static void main(String[] args) {// 获取当前时间SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String currentTime = dateFormat.format(new Date());UserView userView = new UserView();userView.getAllWordsInfo();WordsInfo wordsInfo = new WordsInfo();wordsInfo.setEngName("test");wordsInfo.setChiVal("测试");wordsInfo.setLastUsedTime(currentTime);wordsInfo.setUsedTimes(1);wordsInfo.setCreatedTime(currentTime);wordsInfo.setPriority(0);//presenter作为中间人负责处理用户的输入,将输入与Model交互,并更新View。userView.presenter.insertWordsInfo(wordsInfo);userView.presenter.getAllWordsInfo();userView.presenter.updateWordsInfo(wordsInfo);userView.presenter.getAllWordsInfo();userView.presenter.deleteWordsInfo(98);userView.presenter.getAllWordsInfo();}@Overridepublic void showWordsInfo(List<WordsInfo> wordsInfoList) {for (WordsInfo wordsInfo : wordsInfoList) {System.out.println(wordsInfo.getEngName() + " - " + wordsInfo.getChiVal());}}@Overridepublic void getAllWordsInfo() {}
}

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

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

相关文章

flutter如何实现一个应用位于前台时全局页面每隔三分钟弹出一次一天最多弹出5次的GroMore半插屏广告,处于付费页和后台时停止

1&#xff0c;首先添加一个全局的生命周期监听类 class AppLifecycleObserver with WidgetsBindingObserver {bool IsCold false;bool isAgree false;void getIsCold() async {isAgree await SPManager().getBool(SPKeys.isAgreePrivacy, defaultValue: true);IsCold awai…

MQ的延迟队列

1&#xff0c;场景 1.定时发布文章 2.秒杀之后&#xff0c;给30分钟时间进行支付&#xff0c;如果30分钟后&#xff0c;没有支付&#xff0c;订单取消。 3.预约餐厅&#xff0c;提前半个小时发短信通知用户。 A -> 13:00 17:00 16:30 延迟时间&#xff1a; 7*30 * 60 * …

Linux_Debian学习笔记

文章目录 系统管理软件源Debian11debian11 ustc中国科技大学软件源debian11 清华大学软件源debian11 阿里云软件源 Debian12debian12 清华大学软件源 系统全局设置debian12 修改静态IP地址修改语言环境成中文 系统输入法Debian11 fcxe 安装rime中州韵五笔输入法 常用软件安装Do…

Python操作SQLite数据库

SQLite 是一种轻量级的数据库引擎&#xff0c;广泛用于嵌入式设备和小型应用程序。在Python中&#xff0c;SQLite 是一个流行的选择&#xff0c;因为它易于使用、快速、可靠&#xff0c;并且无需独立的服务器进程。本文将深入探讨如何使用 Python 操作 SQLite 数据库&#xff0…

Excel·VBA考勤打卡记录整理

看到一个帖子《excel吧-考勤一天四次打卡&#xff0c;快速找出缺卡》&#xff0c;每个人每天有4次打卡记录&#xff0c;需要整理出所有缺少的打卡记录 与之前的文章《ExcelVBA考勤打卡记录统计结果》结果形式类似 与之前的文章《ExcelVBA考勤打卡记录数据整理》查找上下班打卡…

Linux、Docker、Brew、Nginx常用命令

Linux、Docker、Brew、Nginx常用命令 Linuxvi编辑器文件操作文件夹操作磁盘操作 DockerBrewNginx参考 Linux vi编辑器 Vi有三种模式。命令模式、输入模式、尾行模式&#xff0c;简单的关系如下&#xff1a; i -- 切换到输入模式&#xff0c;在光标当前位置开始输入文本。&a…

【go从入门到精通】初识struct结构体

作者简介&#xff1a; 高科&#xff0c;先后在 IBM PlatformComputing从事网格计算&#xff0c;淘米网&#xff0c;网易从事游戏服务器开发&#xff0c;拥有丰富的C&#xff0c;go等语言开发经验&#xff0c;mysql&#xff0c;mongo&#xff0c;redis等数据库&#xff0c;设计模…

Flutter中工厂方法的多种实现方法与使用场景分析

在Flutter应用程序的开发中&#xff0c;使用工厂方法是一种常见的设计模式&#xff0c;它可以帮助我们更好地组织和管理代码&#xff0c;提高代码的可读性和可维护性。本文将介绍Flutter中工厂方法的多种实现方法&#xff0c;并分析其在不同场景下的使用情况。 什么是工厂方法…

虚拟网络设备性能优化

在现代网络架构中&#xff0c;虚拟网络设备扮演着越来越重要的角色&#x1f310;&#xff0c;特别是在云计算☁️和容器化技术&#x1f4e6;广泛应用的背景下。虚拟网络设备如虚拟以太网设备&#xff08;veth&#xff09;、虚拟交换机&#xff08;vSwitch&#xff09;、和虚拟路…

【阅读笔记】《同意》

未成年幼女与男恋童癖的故事 作者: [法]瓦内莎斯普林格拉 翻译&#xff1a;李溪月 kindle看的电子书 笔记 传记形式的书。作者记录了14岁时与一个50岁的恋童癖患者进行第一次性行为的经历以及前后的故事。 恋童癖被作者命名为“G”&#xff0c;作者自称“M”&#xff0c;G是…

适用于 Mac 的 10 大数据恢复工具,具有优点、缺点

数据丢失很常见&#xff0c;并且可能由于许多不同的原因而发生。这种情况在我和我们团队的其他成员身上发生过很多次&#xff0c;即使我们格外小心我们的个人存储设备。 幸运的是&#xff0c;数据恢复软件在大多数情况下都可以工作。但是&#xff0c;由于数据丢失场景彼此之间…

arcgis10.x创建镶嵌数据集

在ArcGIS中创建新的镶嵌数据集的步骤如下&#xff1a;打开ArcGIS软件&#xff0c;并在工具箱中选择“Data Management Tools.tbx”→“Raster”→“Mosaic Dataset”→“Create Mosaic Dataset”。在弹出的窗口中&#xff0c;配置镶嵌数据集的地理数据库位置、名称和坐标系…

centos7 安装 rabbitmq3.8.5

1.首先安装 erlang 环境&#xff1a; curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash sudo yum install erlang-21.3.8.14-1.el7.x86_64 yum install socat -y 2.安装 rabbitmq https://github.com/rabbitmq/rabbitmq-s…

STM32学习和实践笔记(7):改变系统时钟的实验

今天完成了这个改变系统时钟的实验。实验是成功的。 #include "stm32f10x.h" #include "led.h"void delay(u32 i) {while(i--); }void RCC_HSE_Config(u32 div,u32 pllm) //自定义系统时间&#xff08;可以修改时钟&#xff09; {RCC_DeInit(); //将外设R…

【CKA模拟题】边车容器Shared-Volume的具体用法

Useful Resources: Persistent Volumes Claim , Pod to Use a PV 题干 For this question, please set this context (In exam, diff cluster name) kubectl config use-context kubernetes-adminkubernetes An existing nginx pod, my-pod-cka and Persistent Volume Claim…

2024年注册安全工程师考试真题及答案1

一、选择题 31.2019年12月10日&#xff0c;某热力公司供热锅炉发生故障。故障抢修过程中&#xff0c;发现锅炉房桥式起重机主钩抱闸故障&#xff0c;存在溜钩现象。由于起重设备厂家人员不能及时到场&#xff0c;工作负责人急于恢复供热&#xff0c;安排检修工甲站在桥式起重机…

macOS制作C/C++ app

C/C制作macOS .app 一、 .app APP其实是一个文件夹结构&#xff0c;只不过mac的界面中让它看起来像一个单独的文件。 在shell终端或者右键查看包结构即可看到APP的目录结构。 通常的app目录结构如下&#xff1a; _CodeSignature, CodeResources 一般为Mac APP Store上架程序…

【aws】在DBeaver上用终端节点连接Redshift

碎碎念 最近想要尝试redshift的一个叫做重新定位的功能&#xff0c;重新定位触发之后会停止当前的集群&#xff0c;转而在同一个区域的另一个可用区中启动一个一样的集群&#xff0c;这个过程视情况会花上10到60分钟不等。 但是目前项目中连接到redshift用的是私有ip&#xf…

保研线性代数复习4

一.范数&#xff08;Norms&#xff09; 1.什么是范数&#xff1f; 范数是一个向量空间V的函数&#xff0c;每一个属于向量空间V的向量x都匹配了一个实数&#xff08;它的长度&#xff09;&#xff1a; 2.范数的性质&#xff1f; 齐次性&#xff1a; 正定性&#xff1a; 三…

大创项目推荐 深度学习 机器视觉 车位识别车道线检测 - python opencv

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习 机器视觉 车位识别车道线检测 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f947;学长这里给一个题目综合评分(每项满分5分) …