java: DDD using oracle 21c

項目結構:

domain:

/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 17:51* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : domain.entities/School.java* explain   : 學習  類**/package domain.entities;
import java.util.Objects;/****/
public class School {private String schoolId;private String schoolName;private String schoolTelNo;public School(String schoolId, String schoolName, String schoolTelNo) {this.schoolId = schoolId;this.schoolName = schoolName;this.schoolTelNo = schoolTelNo;}// Getters and Setterspublic String getSchoolId() { return schoolId; }public String getSchoolName() { return schoolName; }public String getSchoolTelNo() { return schoolTelNo; }public void setSchoolName(String schoolName) { this.schoolName = schoolName; }public void setSchoolTelNo(String schoolTelNo) { this.schoolTelNo = schoolTelNo; }// 領域行為public void updateContactInfo(String newName, String newTelNo) {if (newName != null && !newName.isEmpty()) {this.schoolName = newName;}if (newTelNo != null && !newTelNo.isEmpty()) {this.schoolTelNo = newTelNo;}}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;School school = (School) o;return schoolId.equals(school.schoolId);}@Overridepublic int hashCode() {return Objects.hash(schoolId);}
}/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:47* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : domain.entities/QueryParams.java* explain   : 學習  類**/package domain.entities;/****/
public class QueryParams {private String searchTerm;  // 搜索關鍵詞private int pageNumber;     // 當前頁碼private int pageSize;       // 每頁大小/**** @param searchTerm* @param pageNumber* @param pageSize*/public QueryParams(String searchTerm, int pageNumber, int pageSize) {this.searchTerm = searchTerm;this.pageNumber = pageNumber;this.pageSize = pageSize;}// Getterspublic String getSearchTerm() { return searchTerm; }public int getPageNumber() { return pageNumber; }public int getPageSize() { return pageSize; }public int getOffset() { return (pageNumber - 1) * pageSize; }
}/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 17:53* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : domain/repositories/SchoolRepository.java* explain   : 學習  類**/package domain.repositories;import domain.entities.School;
import domain.entities.QueryParams;import java.util.List;/****/
public interface SchoolRepository {List<School> findAll(int page, int pageSize);// 新增支持查詢條件的方法List<School> findByParams(QueryParams params);int countByParams(QueryParams params);int countAll();School findById(String schoolId);void save(School school);void delete(String schoolId);}

infrastructure:

/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:03* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : SchoolRepositoryImpl.java* explain   : 學習  類**/package infrastructure.repositories;import domain.entities.School;
import domain.entities.QueryParams;
import domain.repositories.SchoolRepository;
import infrastructure.database.DuOracleHelper;import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;public class SchoolRepositoryImpl implements SchoolRepository {@Overridepublic List<School> findAll(int page, int pageSize) {System.out.println("開始分頁查詢,頁碼: " + page + ",每頁大小: " + pageSize);List<School> schools = new ArrayList<>();String sql = "SELECT * FROM School ORDER BY SchoolId " +"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";try (Connection conn = DuOracleHelper.getConnection()) {System.out.println("成功獲取數據庫連接");try (PreparedStatement pstmt = conn.prepareStatement(sql)) {pstmt.setInt(1, (page - 1) * pageSize);pstmt.setInt(2, pageSize);System.out.println("執行SQL: " + sql);System.out.println("參數1: " + (page - 1) * pageSize);System.out.println("參數2: " + pageSize);try (ResultSet rs = pstmt.executeQuery()) {while (rs.next()) {schools.add(mapToDomain(rs));}System.out.println("查詢成功,返回 " + schools.size() + " 條記錄");}}} catch (SQLException | IOException e) {System.err.println("數據庫操作異常: " + e.getMessage());e.printStackTrace(); // 打印完整堆棧信息throw new RuntimeException("查詢學校列表失敗", e);}return schools;}@Overridepublic int countAll() {String sql = "SELECT COUNT(*) FROM School";try (Connection conn = DuOracleHelper.getConnection();Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery(sql)) {if (rs.next()) {return rs.getInt(1);}} catch (SQLException | IOException e) {throw new RuntimeException("統計學校數量失敗", e);}return 0;}@Overridepublic List<School> findByParams(QueryParams params) {List<School> schools = new ArrayList<>();StringBuilder sql = new StringBuilder("SELECT * FROM School WHERE 1=1 ");// 構建查詢條件if (params.getSearchTerm() != null && !params.getSearchTerm().isEmpty()) {sql.append("AND (SchoolId LIKE ? OR SchoolName LIKE ?) ");}// 添加排序和分頁sql.append("ORDER BY SchoolId OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql.toString())) {int paramIndex = 1;// 設置查詢參數if (params.getSearchTerm() != null && !params.getSearchTerm().isEmpty()) {String searchTerm = "%" + params.getSearchTerm() + "%";pstmt.setString(paramIndex++, searchTerm);pstmt.setString(paramIndex++, searchTerm);}// 設置分頁參數pstmt.setInt(paramIndex++, params.getOffset());pstmt.setInt(paramIndex, params.getPageSize());try (ResultSet rs = pstmt.executeQuery()) {while (rs.next()) {schools.add(mapToDomain(rs));}}} catch (SQLException | IOException e) {throw new RuntimeException("按條件查詢學校失敗", e);}return schools;}@Overridepublic int countByParams(QueryParams params) {StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM School WHERE 1=1 ");// 構建查詢條件if (params.getSearchTerm() != null && !params.getSearchTerm().isEmpty()) {sql.append("AND (SchoolId LIKE ? OR SchoolName LIKE ?)");}try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql.toString())) {int paramIndex = 1;// 設置查詢參數if (params.getSearchTerm() != null && !params.getSearchTerm().isEmpty()) {String searchTerm = "%" + params.getSearchTerm() + "%";pstmt.setString(paramIndex++, searchTerm);pstmt.setString(paramIndex, searchTerm);}try (ResultSet rs = pstmt.executeQuery()) {if (rs.next()) {return rs.getInt(1);}}} catch (SQLException | IOException e) {throw new RuntimeException("統計符合條件的學校數量失敗", e);}return 0;}// 其他方法實現...@Overridepublic School findById(String schoolId) {String sql = "SELECT * FROM School WHERE SchoolId = ?";try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql)) {pstmt.setString(1, schoolId);try (ResultSet rs = pstmt.executeQuery()) {if (rs.next()) {return mapToDomain(rs);}}} catch (SQLException | IOException e) {throw new RuntimeException("查找學校失敗", e);}return null;}@Overridepublic void save(School school) {// 檢查是插入還是更新if (findById(school.getSchoolId()) == null) {insertSchool(school);} else {updateSchool(school);}}private void insertSchool(School school) {String sql = "INSERT INTO School (SchoolId, SchoolName, SchoolTelNo) VALUES (?, ?, ?)";try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql)) {pstmt.setString(1, school.getSchoolId());pstmt.setString(2, school.getSchoolName());pstmt.setString(3, school.getSchoolTelNo());pstmt.executeUpdate();} catch (SQLException | IOException e) {throw new RuntimeException("添加學校失敗", e);}}private void updateSchool(School school) {String sql = "UPDATE School SET SchoolName = ?, SchoolTelNo = ? WHERE SchoolId = ?";try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql)) {pstmt.setString(1, school.getSchoolName());pstmt.setString(2, school.getSchoolTelNo());pstmt.setString(3, school.getSchoolId());pstmt.executeUpdate();} catch (SQLException | IOException e) {throw new RuntimeException("更新學校失敗", e);}}@Overridepublic void delete(String schoolId) {String sql = "DELETE FROM School WHERE SchoolId = ?";try (Connection conn = DuOracleHelper.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql)) {pstmt.setString(1, schoolId);pstmt.executeUpdate();} catch (SQLException | IOException e) {throw new RuntimeException("刪除學校失敗", e);}}private School mapToDomain(ResultSet rs) throws SQLException {return new School(rs.getString("SchoolId"),rs.getString("SchoolName"),rs.getString("SchoolTelNo"));}
}/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:02* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : SchoolDto.java* explain   : 學習  類**/package infrastructure.model;/****/
public class SchoolDto {private String schoolId;private String schoolName;private String schoolTelNo;// Getters and Setterspublic String getSchoolId() { return schoolId; }public void setSchoolId(String schoolId) { this.schoolId = schoolId; }public String getSchoolName() { return schoolName; }public void setSchoolName(String schoolName) { this.schoolName = schoolName; }public String getSchoolTelNo() { return schoolTelNo; }public void setSchoolTelNo(String schoolTelNo) { this.schoolTelNo = schoolTelNo; }
}

application:

/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:08* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : application/server/SchoolService.java* explain   : 學習  類**/package application.server;import domain.entities.QueryParams;
import domain.entities.School;
import domain.repositories.SchoolRepository;
import infrastructure.repositories.SchoolRepositoryImpl;
import java.util.List;public class SchoolService {private final SchoolRepository repository;public SchoolService() {this.repository = new SchoolRepositoryImpl();}// 分頁查詢public PagedResult<School> getSchoolsByPage(int page, int pageSize) {List<School> schools = repository.findAll(page, pageSize);int totalCount = repository.countAll();return new PagedResult<>(schools,page,pageSize,(int) Math.ceil((double) totalCount / pageSize),totalCount);}/*** 新增支持搜索的分頁查詢方法* @param searchTerm* @param page* @param pageSize* @return*/public PagedResult<School> getSchoolsByPageAndSearch(String searchTerm, int page, int pageSize) {QueryParams params = new QueryParams(searchTerm, page, pageSize);List<School> schools = repository.findByParams(params);int totalCount = repository.countByParams(params);return new PagedResult<>(schools,page,pageSize,(int) Math.ceil((double) totalCount / pageSize),totalCount);}// 其他業務方法...public void saveSchool(School school) {if (school.getSchoolId() == null || school.getSchoolId().isEmpty()) {// 生成新ID的邏輯String newId = generateSchoolId();school = new School(newId, school.getSchoolName(), school.getSchoolTelNo());}repository.save(school);}public void updateSchool(School school) {repository.save(school); // 假設save方法可以處理更新}public void deleteSchool(String schoolId) {repository.delete(schoolId);}public int getTotalSchoolCount() {return repository.countAll();}// 生成學校ID的輔助方法private String generateSchoolId() {// 簡單實現,實際應用中可能需要更復雜的ID生成策略return "S" + System.currentTimeMillis();}public int getTotalSchoolCount(String searchTerm) {QueryParams params = new QueryParams(searchTerm, 1, 10);return repository.countByParams(params);}// 分頁結果類public static class PagedResult<T> {private final List<T> items;private final int currentPage;private final int pageSize;private final int totalPages;private final int totalItems;public PagedResult(List<T> items, int currentPage, int pageSize, int totalPages, int totalItems) {this.items = items;this.currentPage = currentPage;this.pageSize = pageSize;this.totalPages = totalPages;this.totalItems = totalItems;}// Getterspublic List<T> getItems() { return items; }public int getCurrentPage() { return currentPage; }public int getPageSize() { return pageSize; }public int getTotalPages() { return totalPages; }public int getTotalItems() { return totalItems; }public boolean hasNextPage() { return currentPage < totalPages; }public boolean hasPreviousPage() { return currentPage > 1; }}
}

presentation:

/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:10* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : SchoolController.java* explain   : 學習  類**/package presentation.controllers;import application.server.SchoolService;
import application.server.SchoolService.PagedResult;
import domain.entities.School;
import presentation.views.SchoolView;public class SchoolController {private final SchoolService service;private final SchoolView view;/**** @param view*/public SchoolController(SchoolView view) {this.service = new SchoolService();this.view = view;}/**** @param searchTerm* @param page* @param pageSize*/public void loadSchools(String searchTerm, int page, int pageSize) {PagedResult<School> result = service.getSchoolsByPageAndSearch(searchTerm, page, pageSize);view.displaySchools(result);}/**** @param school*/public void addSchool(School school) {service.saveSchool(school);// 重新加載數據,保持當前搜索條件和分頁loadSchools(view.getCurrentSearchTerm(), 1, view.getPageSize());}/**** @param school*/public void updateSchool(School school) {service.updateSchool(school);// 重新加載當前頁loadSchools(view.getCurrentSearchTerm(), view.getCurrentPage(), view.getPageSize());}/**** @param schoolId*/public void deleteSchool(String schoolId) {// 獲取當前分頁信息String searchTerm = view.getCurrentSearchTerm();int currentPage = view.getCurrentPage();int pageSize = view.getPageSize();// 執行刪除service.deleteSchool(schoolId);// 獲取刪除后的總記錄數int totalRecords = service.getTotalSchoolCount(searchTerm);// 計算刪除后應該顯示的頁碼int totalPages = Math.max(1, (int) Math.ceil((double) totalRecords / pageSize));int newPage = Math.min(currentPage, totalPages);// 加載正確的頁loadSchools(searchTerm, newPage, pageSize);}/*public void deleteSchool(String schoolId) {// 獲取當前分頁信息String searchTerm = view.getCurrentSearchTerm();int currentPage = view.getCurrentPage();int pageSize = view.getPageSize();// 執行刪除service.deleteSchool(schoolId);// 獲取刪除后的總記錄數int totalRecords = service.getTotalSchoolCount(searchTerm);// 計算刪除后應該顯示的頁碼int totalPages = Math.max(1, (int) Math.ceil((double) totalRecords / pageSize));int newPage = Math.min(currentPage, totalPages);// 加載正確的頁loadSchools(searchTerm, newPage, pageSize);}*/}/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 18:12* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : SchoolView.java* explain   : 學習  類**/package presentation.views;import application.server.SchoolService.PagedResult;
import domain.entities.School;
import presentation.controllers.SchoolController;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;public class SchoolView extends JFrame {private final SchoolController controller;private JTable schoolTable;private SchoolTableModel tableModel;private JButton firstPageBtn, prevPageBtn, nextPageBtn, lastPageBtn;private JButton addButton,editButton,deleteButton,refreshButton;private JLabel pageInfoLabel;private int currentPage = 1;private int pageSize = 10;private String currentSearchTerm = null;  // 新增:當前搜索關鍵詞private JTextField searchField;           // 新增:搜索輸入框public SchoolView() {this.controller = new SchoolController(this);initComponents();loadInitialData();// 添加按鈕事件addButton.addActionListener(e -> showAddSchoolDialog());editButton.addActionListener(e -> showEditSchoolDialog());deleteButton.addActionListener(e -> performDeleteSchool());}private void initComponents() {setTitle("學校管理系統");setSize(800, 600);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 創建工具欄和按鈕JToolBar toolBar = new JToolBar();refreshButton = new JButton("刷新");addButton = new JButton("添加");editButton = new JButton("編輯");deleteButton = new JButton("刪除");toolBar.add(refreshButton);toolBar.add(addButton);toolBar.add(editButton);toolBar.add(deleteButton);// 創建搜索面板JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));JLabel searchLabel = new JLabel("搜索:");searchField = new JTextField(20);JButton searchButton = new JButton("搜索");searchButton.addActionListener(e -> performSearch());searchPanel.add(searchLabel);searchPanel.add(searchField);searchPanel.add(searchButton);// 初始化表格tableModel = new SchoolTableModel();schoolTable = new JTable(tableModel);JScrollPane scrollPane = new JScrollPane(schoolTable);// 添加按鈕事件refreshButton.addActionListener(e -> loadInitialData());addButton.addActionListener(e -> showAddSchoolDialog());editButton.addActionListener(e -> showEditSchoolDialog());deleteButton.addActionListener(e -> performDeleteSchool());// 初始化分頁控件initPaginationControls();// 布局setLayout(new BorderLayout());add(toolBar, BorderLayout.NORTH);add(searchPanel, BorderLayout.SOUTH);add(scrollPane, BorderLayout.CENTER);add(createPaginationPanel(), BorderLayout.SOUTH);}/*** 添加*/private void showAddSchoolDialog() {JDialog dialog = new JDialog(this, "添加學校", true);dialog.setSize(400, 300);dialog.setLocationRelativeTo(this);// 創建表單組件JLabel idLabel = new JLabel("學校ID:");JTextField idField = new JTextField(20);JLabel nameLabel = new JLabel("學校名稱:");JTextField nameField = new JTextField(20);JLabel telLabel = new JLabel("聯系電話:");JTextField telField = new JTextField(20);JButton saveButton = new JButton("保存");JButton cancelButton = new JButton("取消");// 設置布局GroupLayout layout = new GroupLayout(dialog.getContentPane());dialog.getContentPane().setLayout(layout);layout.setAutoCreateGaps(true);layout.setAutoCreateContainerGaps(true);layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(idLabel).addComponent(nameLabel).addComponent(telLabel)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(idField).addComponent(nameField).addComponent(telField))).addGroup(Alignment.TRAILING, layout.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(cancelButton).addPreferredGap(ComponentPlacement.RELATED).addComponent(saveButton)));layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(idLabel).addComponent(idField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(nameLabel).addComponent(nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(telLabel).addComponent(telField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED, 40, Short.MAX_VALUE).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(saveButton).addComponent(cancelButton))));// 添加按鈕事件saveButton.addActionListener(e -> {String id = idField.getText().trim();String name = nameField.getText().trim();String tel = telField.getText().trim();if (id.isEmpty() || name.isEmpty()) {JOptionPane.showMessageDialog(dialog, "學校ID和學校名稱不能為空", "輸入錯誤", JOptionPane.ERROR_MESSAGE);return;}controller.addSchool(new School(id, name, tel));dialog.dispose();});cancelButton.addActionListener(e -> dialog.dispose());dialog.setVisible(true);}/*** 修改*/private void showEditSchoolDialog() {int selectedRow = schoolTable.getSelectedRow();if (selectedRow == -1) {JOptionPane.showMessageDialog(this, "請先選擇要修改的學校", "提示", JOptionPane.INFORMATION_MESSAGE);return;}// 獲取選中的學校數據String schoolId = (String) schoolTable.getValueAt(selectedRow, 0);String schoolName = (String) schoolTable.getValueAt(selectedRow, 1);String schoolTel = (String) schoolTable.getValueAt(selectedRow, 2);JDialog dialog = new JDialog(this, "修改學校", true);dialog.setSize(400, 300);dialog.setLocationRelativeTo(this);// 創建表單組件JLabel idLabel = new JLabel("學校ID:");JTextField idField = new JTextField(schoolId, 20);idField.setEditable(false); // ID不可編輯JLabel nameLabel = new JLabel("學校名稱:");JTextField nameField = new JTextField(schoolName, 20);JLabel telLabel = new JLabel("聯系電話:");JTextField telField = new JTextField(schoolTel, 20);JButton saveButton = new JButton("保存");JButton cancelButton = new JButton("取消");// 設置布局(與添加對話框類似)GroupLayout layout = new GroupLayout(dialog.getContentPane());dialog.getContentPane().setLayout(layout);layout.setAutoCreateGaps(true);layout.setAutoCreateContainerGaps(true);layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(idLabel).addComponent(nameLabel).addComponent(telLabel)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(idField).addComponent(nameField).addComponent(telField))).addGroup(Alignment.TRAILING, layout.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(cancelButton).addPreferredGap(ComponentPlacement.RELATED).addComponent(saveButton)));layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(idLabel).addComponent(idField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(nameLabel).addComponent(nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(telLabel).addComponent(telField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED, 40, Short.MAX_VALUE).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(saveButton).addComponent(cancelButton))));// 添加按鈕事件saveButton.addActionListener(e -> {String id = idField.getText().trim();String name = nameField.getText().trim();String tel = telField.getText().trim();if (name.isEmpty()) {JOptionPane.showMessageDialog(dialog, "學校名稱不能為空", "輸入錯誤", JOptionPane.ERROR_MESSAGE);return;}controller.updateSchool(new School(id, name, tel));dialog.dispose();});cancelButton.addActionListener(e -> dialog.dispose());dialog.setVisible(true);}/*** 刪除*/private void performDeleteSchool() {int selectedRow = schoolTable.getSelectedRow();if (selectedRow == -1) {JOptionPane.showMessageDialog(this, "請先選擇要刪除的學校", "提示", JOptionPane.INFORMATION_MESSAGE);return;}// 獲取選中的學校IDString schoolId = (String) schoolTable.getValueAt(selectedRow, 0);String schoolName = (String) schoolTable.getValueAt(selectedRow, 1);// 確認對話框int confirm = JOptionPane.showConfirmDialog(this,"確定要刪除學校 [" + schoolName + "] 嗎?","確認刪除",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);if (confirm == JOptionPane.YES_OPTION) {controller.deleteSchool(schoolId);}}private void loadInitialData() {controller.loadSchools(currentSearchTerm, currentPage, pageSize);}private void performSearch() {currentSearchTerm = searchField.getText();currentPage = 1;  // 搜索時重置到第一頁controller.loadSchools(currentSearchTerm, currentPage, pageSize);}private void initPaginationControls() {firstPageBtn = new JButton("首頁");prevPageBtn = new JButton("上一頁");nextPageBtn = new JButton("下一頁");lastPageBtn = new JButton("末頁");pageInfoLabel = new JLabel("第 1 頁,共 0 頁");firstPageBtn.addActionListener(e -> goToFirstPage());prevPageBtn.addActionListener(e -> goToPreviousPage());nextPageBtn.addActionListener(e -> goToNextPage());lastPageBtn.addActionListener(e -> goToLastPage());}private JPanel createPaginationPanel() {JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));panel.add(firstPageBtn);panel.add(prevPageBtn);panel.add(pageInfoLabel);panel.add(nextPageBtn);panel.add(lastPageBtn);return panel;}public void displaySchools(PagedResult<School> result) {tableModel.setSchools(result.getItems());tableModel.fireTableDataChanged();currentPage = result.getCurrentPage();updatePageInfo(result);}private void updatePageInfo(PagedResult<School> result) {pageInfoLabel.setText(String.format("第 %d 頁,共 %d 頁,總計 %d 條記錄",result.getCurrentPage(),result.getTotalPages(),result.getTotalItems()));// 啟用/禁用按鈕firstPageBtn.setEnabled(result.hasPreviousPage());prevPageBtn.setEnabled(result.hasPreviousPage());nextPageBtn.setEnabled(result.hasNextPage());lastPageBtn.setEnabled(result.hasNextPage());}// 獲取總頁數的輔助方法private int getTotalPages() {String text = pageInfoLabel.getText();try {// 從"第 X 頁,共 Y 頁,總計 Z 條記錄"中提取YString[] parts = text.split(",");if (parts.length >= 2) {String totalPagesPart = parts[1].trim();// 提取"共 Y 頁"中的YString totalPagesStr = totalPagesPart.substring(totalPagesPart.indexOf("共") + 1,totalPagesPart.indexOf("頁")).trim(); // 去除可能的空格// 增加調試輸出System.out.println("提取的總頁數字符串: '" + totalPagesStr + "'");return Integer.parseInt(totalPagesStr);}} catch (Exception e) {System.err.println("解析總頁數失敗: " + e.getMessage());e.printStackTrace();}return 1; // 默認返回1頁}// 添加獲取當前搜索詞的方法public String getCurrentSearchTerm() {return currentSearchTerm;}// 修復導航方法,傳遞搜索參數private void goToPreviousPage() {if (currentPage > 1) {controller.loadSchools(currentSearchTerm, currentPage - 1, pageSize);}}// 其他導航方法也需要類似修改private void goToNextPage() {if (currentPage < getTotalPages()) {controller.loadSchools(currentSearchTerm, currentPage + 1, pageSize);}}private void goToFirstPage() {controller.loadSchools(currentSearchTerm, 1, pageSize);}private void goToLastPage() {int totalPages = getTotalPages();if (totalPages > 0) {controller.loadSchools(currentSearchTerm, totalPages, pageSize);}}// Getters for pagination statepublic int getCurrentPage() { return currentPage; }public int getPageSize() { return pageSize; }// 表格模型private static class SchoolTableModel extends AbstractTableModel {private List<School> schools = new ArrayList<>();private final String[] columnNames = {"學校ID", "學校名稱", "聯系電話"};public void setSchools(List<School> schools) {this.schools = schools;}@Overridepublic int getRowCount() {return schools.size();}@Overridepublic int getColumnCount() {return columnNames.length;}@Overridepublic String getColumnName(int column) {return columnNames[column];}@Overridepublic Object getValueAt(int rowIndex, int columnIndex) {School school = schools.get(rowIndex);switch (columnIndex) {case 0: return school.getSchoolId();case 1: return school.getSchoolName();case 2: return school.getSchoolTelNo();default: return null;}}}
}

調用:

/*** encoding: utf-8* 版權所有 2025 ?涂聚文有限公司 ?* 許可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:* Author    : geovindu,Geovin Du 涂聚文.* IDE       : IntelliJ IDEA 2024.3.6 Java 17* # database  : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS        : window10* Datetime  : 2025 - 2025/7/13 - 17:51* User      : geovindu* Product   : IntelliJ IDEA* Project   : oracleDDDDemo* File      : main.java* explain   : 學習  類**/import presentation.views.SchoolView;public class Main {public static void main(String[] args) {// 使用SwingUtilites確保在事件調度線程上創建和操作GUIjavax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {SchoolView view = new SchoolView();view.setVisible(true);}});System.out.println("Hello, Geovin Du!");}
}

輸出:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/914383.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/914383.shtml
英文地址,請注明出處:http://en.pswp.cn/news/914383.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

兩張圖片對比clip功能

<!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>圖片拖動Clip對比功能</title><style&g…

7.11 dp 圖

lcr148.棧按放入順序推棧&#xff0c;能彈出的就及時彈出&#xff0c;最后棧空則符合要求。判斷 takeOut 序列是否符合棧的操作邏輯&#xff0c;因為題目中“特殊的數據結構”其實就是棧&#xff08;先進后出&#xff09;。思路如下&#xff1a;1. 用一個棧來模擬圖書放入的過程…

react16-react19都更新哪些內容?

React 16 到 React 19 是 React 發展非常關鍵的階段&#xff0c;每個版本都帶來了深遠影響。以下是 React 16 → 19 的重要更新列表&#xff0c;按版本詳細說明每一代的核心特性、重要變化、對開發者的意義&#xff0c;并附簡評&#xff1a;? React 16&#xff08;2017 年&…

【AI大模型】RAG系統組件:向量數據庫(ChromaDB)

RAG 系統中的關鍵組件&#xff1a;向量數據庫&#xff08;Vector Database&#xff09;&#xff0c;并以 ChromaDB 為例進行說明。什么是向量數據庫&#xff1f;核心概念&#xff1a; 向量數據庫是一種專門設計用于高效存儲、索引和檢索高維向量的數據庫。向量是什么&#xff1…

006_測試評估與安全實踐

測試評估與安全實踐 目錄 建立成功標準評估方法測試策略安全最佳實踐隱私保護性能監控 建立成功標準 定義原則 1. 具體明確 清晰定義精確目標避免模糊表述如"良好性能"制定可操作的標準 不好的標準&#xff1a; 模型應該表現良好好的標準&#xff1a; 情感分…

時序預測 | Pytorch實現CNN-KAN電力負荷時間序列預測模型

預測效果 代碼功能 該代碼實現了一個結合卷積神經網絡&#xff08;CNN&#xff09;和Kolmogorov–Arnold網絡&#xff08;KAN&#xff09;的混合模型&#xff08;CNN-KAN&#xff09;&#xff0c;用于時間序列預測任務。核心功能包括&#xff1a; 數據加載與預處理&#xff1…

UI前端與數字孿生結合實踐探索:智慧物流的倉儲優化與管理系統

hello寶子們...我們是艾斯視覺擅長ui設計和前端數字孿生、大數據、三維建模、三維動畫10年經驗!希望我的分享能幫助到您!如需幫助可以評論關注私信我們一起探討!致敬感謝感恩!一、引言&#xff1a;倉儲管理的 “數字孿生革命”傳統物流倉儲正面臨 “效率瓶頸、可視化差、響應滯…

【Android】在平板上實現Rs485的數據通訊

前言 在工業控制領域&#xff0c;Android 設備通過 RS485 接口與 PLC&#xff08;可編程邏輯控制器&#xff09;通信是一種常見的技術方案。最近在實現一個項目需要和plc使用485進行通訊&#xff0c;記錄下實現的方式。 我這邊使用的從平的Android平板&#xff0c;從平里面已經…

MySQL技術筆記-備份與恢復完全指南

目錄 前言 一、備份概述 &#xff08;一&#xff09;備份方式 &#xff08;二&#xff09;備份策略 二、物理備份及恢復 &#xff08;一&#xff09;備份操作 &#xff08;二&#xff09;恢復操作 三、邏輯備份及恢復 &#xff08;一&#xff09;邏輯備份 &#xff0…

SpringBoot或OpenFeign中 Jackson 配置參數名蛇形、小駝峰、大駝峰、自定義命名

SpringBoot或OpenFeign中 Jackson 配置參數名蛇形、小駝峰、大駝峰、自定義命名 前言 在調用外部接口時&#xff0c;對方給出的接口文檔中&#xff0c;入參參數名一會大寫加下劃線&#xff0c;一會又是駝峰命名。 示例如下&#xff1a; {"MOF_DIV_CODE": "xx…

uni-app 途徑站點組件開發與實現分享

在移動應用開發中&#xff0c;涉及到出行、物流等場景時&#xff0c;途徑站點的展示是一個常見的需求。本文將為大家分享一個基于 uni-app 開發的途徑站點組件&#xff0c;該組件能夠清晰展示路線中的各個站點信息&#xff0c;包括站點名稱、到達時間、是否已到達等狀態&#x…

kotlin中集合的用法

從一個實際應用看起以下kotlin中代碼語法正確嗎 var testBeanAIP0200()var testList:List<AIP0200> ArrayList()testList.add(testBean)這段Kotlin代碼存在語法錯誤&#xff0c;主要問題在于&#xff1a;List<AIP0200> 是Kotlin中的不可變集合接口&#xff0c;不能…

深入理解 Java Map 與 Set

文章目錄前言1. 搜索樹1.1 什么是搜索樹1.2 查找1.3 插入1.4 刪除情況一&#xff1a;cur 沒有子節點&#xff08;即為葉子節點&#xff09;情況二&#xff1a;cur 只有一個子節點&#xff08;只有左子樹或右子樹&#xff09;情況三&#xff1a;cur 有兩個子節點&#xff08;左右…

excel如何只保留前幾行

方法一&#xff1a;手動刪除多余行 選中你想保留的最后一行的下一行&#xff08;比如你只保留前10行&#xff0c;那選第11行&#xff09;。按住 Shift Ctrl ↓&#xff08;Windows&#xff09;或 Shift Command ↓&#xff08;Mac&#xff09;&#xff0c;選中從第11行到最…

實時連接,精準監控:風丘科技數據遠程顯示方案提升試驗車隊管理效率

風丘科技推出的數據遠程實時顯示方案更好地滿足了客戶對于試驗車隊遠程實時監控的需求&#xff0c;并真正實現了試驗車隊的遠程管理。隨著新的數據記錄儀軟件IPEmotion RT和相應的跨平臺顯示解決方案的引入&#xff0c;讓我們的客戶端不僅可在線訪問記錄器系統狀態&#xff0c;…

灰盒級SOA測試工具Parasoft SOAtest重新定義端到端測試

還在為脆弱的測試環境、強外部依賴和低效的測試復用拖慢交付而頭疼&#xff1f;尤其在銀行、醫療、制造等關鍵領域&#xff0c;傳統的端到端測試常因環境不穩、接口難模擬、用例難共享而舉步維艱。 灰盒級SOA測試工具Parasoft SOAtest以可視化編排簡化復雜測試流程&#xff0c…

OKHttp 核心知識點詳解

OKHttp 核心知識點詳解 一、基本概念與架構 1. OKHttp 簡介 類型&#xff1a;高效的HTTP客戶端特點&#xff1a; 支持HTTP/2和SPDY&#xff08;多路復用&#xff09;連接池減少請求延遲透明的GZIP壓縮響應緩存自動恢復網絡故障2. 核心組件組件功能OkHttpClient客戶端入口&#…

從“被動巡檢”到“主動預警”:塔能物聯運維平臺重構路燈管理模式

從以往的‘被動巡檢’轉變至如今的‘主動預警’&#xff0c;塔能物聯運維平臺對路燈管理模式展開了重新構建。城市路燈屬于極為重要的市政基礎設施范疇&#xff0c;它的實際運行狀態和市民出行安全以及城市形象有著直接且緊密的關聯。不過呢&#xff0c;傳統的路燈管理模式當下…

10. 常見的 http 狀態碼有哪些

總結 1xx: 正在處理2xx: 成功3xx: 重定向&#xff0c;302 重定向&#xff0c;304 協商緩存4xx: 客戶端錯誤&#xff0c;401 未登錄&#xff0c;403 沒權限&#xff0c;404 資源不存在5xx: 服務器錯誤常見的 HTTP 狀態碼詳解 HTTP 狀態碼&#xff08;HTTP Status Code&#xff0…

springBoot對接第三方系統

yml文件 yun:ip: port: username: password: controller package com.ruoyi.web.controller.materials;import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.materials.service.IYunService; import o…