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() {}
}