什么是MVC模式
程序员文章站
2022-07-08 10:26:48
...
什么是MVC模式
MVC是三个单词的缩写,分别为:模型(Model),视图(View)和控制(Controller)。MVC模式的目的就是实现Web系统的职能分工。
Model 是应用对象,所有的操作都在这里实现,它若需要取得视图中的对象或更新视图,需通过控制器来进行处理。
View 是模型在屏幕上的表示,模型在进行操作后,其结果是通过视图显示的。
Controller 用于管理用户与视图发生的交互,定义用户界面对用户输入的响应方式。一旦用户需要对模型进行处理,不能直接执行模型,而必须通过控制器间接实现的。
使用MVC来实现一张表的CRUD(MySQL8.0数据库)
model层
- entity (这里使用了lombok jar包,可以直接使用注解来set、get重写toString等等)
package com.yml.cn.model.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author yml
* @date 2021/06/01
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JudgeQuestion {
private Integer questionId;
private String title;
private String answer;
private String type;
private String level;
private String state;
private String remarks;
private String filepath;
}
- service
- 接口
package com.yml.cn.model.service;
import com.yml.cn.model.entity.JudgeQuestion;
import java.util.List;
/**
* @author yml
* @date 2021/06/01
*/
public interface JudgeQuestionService {
public JudgeQuestion findById(Integer id);
public List<JudgeQuestion> findAll();
public int save(JudgeQuestion judgeQuestion);
public int update(JudgeQuestion judgeQuestion);
public int deleteById(Integer id);
}
- 实现类
package com.yml.cn.model.service;
import com.yml.cn.model.entity.JudgeQuestion;
import com.yml.cn.utils.ConnectionUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MySQLJudgeQuestionServiceImpl implements JudgeQuestionService {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
public MySQLJudgeQuestionServiceImpl(){
connection = ConnectionUtil.getConnection();
}
public Connection getConnection() {
return connection;
}
@Override
public JudgeQuestion findById(Integer id) {
JudgeQuestion judgeQuestion = null;
String sql = "select * from judge_question where questionId = ?";
try {
preparedStatement = connection.prepareStatement(sql);
//填充占位符
preparedStatement.setObject(1,id);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
String title = (String)resultSet.getObject(2);
String answer = (String)resultSet.getObject(3);
String type = (String)resultSet.getObject(4);
String level = (String)resultSet.getObject(5);
String state = (String)resultSet.getObject(6);
String remarks = (String)resultSet.getObject(7);
String filepath = (String)resultSet.getObject(8);
judgeQuestion = new JudgeQuestion(id,title,answer,type,level,state,remarks,filepath);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.closeResource(resultSet,preparedStatement,null);
}
return judgeQuestion;
}
@Override
public List<JudgeQuestion> findAll() {
List<JudgeQuestion> judgeQuestions = new ArrayList<>();
String sql = "select * from judge_question";
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
Integer id = (Integer)resultSet.getObject(1);
String title = (String)resultSet.getObject(2);
String answer = (String)resultSet.getObject(3);
String type = (String)resultSet.getObject(4);
String level = (String)resultSet.getObject(5);
String state = (String)resultSet.getObject(6);
String remarks = (String)resultSet.getObject(7);
String filepath = (String)resultSet.getObject(8);
JudgeQuestion judgeQuestion = new JudgeQuestion(id,title,answer,type,level,state,remarks,filepath);
judgeQuestions.add(judgeQuestion);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.closeResource(resultSet,preparedStatement,null);
}
return judgeQuestions;
}
@Override
public int save(JudgeQuestion judgeQuestion) {
String sql = "insert into judge_question(title, answer, type, level, state, remarks, filepath) values(?, ?, ?, ?, ?, ?, ?)";
int count = 0;
try {
Object[] values = {judgeQuestion.getTitle(), judgeQuestion.getAnswer(), judgeQuestion.getType(), judgeQuestion.getLevel(), judgeQuestion.getState(),judgeQuestion.getRemarks(),judgeQuestion.getFilepath()};
preparedStatement = connection.prepareStatement(sql);
//填充占位符
for(int i = 0;i < values.length;i ++){
preparedStatement.setObject(i+1,values[i]);
}
count = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.closeResource(null,preparedStatement,null);
}
return count;
}
@Override
public int update(JudgeQuestion judgeQuestion) {
String sql = "update judge_question set title = ?, answer = ?, type = ?, level = ?, state = ?, remarks = ?, filepath = ? where questionId = ?";
int count = 0;
try {
String[] values = {judgeQuestion.getTitle(), judgeQuestion.getAnswer(), judgeQuestion.getType(), judgeQuestion.getLevel(), judgeQuestion.getState(),judgeQuestion.getRemarks(),judgeQuestion.getFilepath(),String.valueOf(judgeQuestion.getQuestionId())};
preparedStatement = connection.prepareStatement(sql);
//填充占位符
for(int i = 0;i < values.length;i ++){
preparedStatement.setObject(i+1,values[i]);
}
count = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.closeResource(null,preparedStatement,null);
}
return count;
}
@Override
public int deleteById(Integer id) {
String sql = "delete from judge_question where questionId = ?";
int i = 0;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1,id);
i = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.closeResource(null,preparedStatement,null);
}
return i;
}
}
View层
- AddView
package com.yml.cn.view;
import com.yml.cn.controller.JudgeQuestionController;
import com.yml.cn.model.entity.JudgeQuestion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author yml
* @date 2021/06/8
*/
public class AddView extends JFrame implements ActionListener {
//左半边标签
private JPanel LabelPanel;
private JLabel questionId;
private JLabel title;
private JLabel answer;
private JLabel type;
private JLabel level;
private JLabel state;
private JLabel remarks;
private JLabel filepath;
//右半边文本框
private JPanel valuePanel;
private JTextField questionIdValue;
private JTextField titleValue;
private JTextField answerValue;
private JTextField typeValue;
private JTextField levelValue;
private JTextField statesVale;
private JTextField remarksValue;
private JTextField filepathValue;
//按钮
private JButton submit;
private JButton reSet;
private Font font1;
private Font font2;
private Container contentPane;
public AddView(){
init();
setLabelPanelComponents();
setValuePanelComponents();
setButtons();
setBounds(100, 100, 514, 583);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
contentPane = getContentPane();
contentPane.add(LabelPanel);
contentPane.add(valuePanel);
contentPane.add(submit);
contentPane.add(reSet);
//点击窗口右上角叉号隐藏并释放该窗体
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public void init(){
LabelPanel = new JPanel();
questionId = new JLabel("questionId: ");
title = new JLabel(" title:");
answer = new JLabel(" answer: ");
type = new JLabel(" type: ");
level = new JLabel(" level: ");
state = new JLabel(" state: ");
remarks = new JLabel(" remarks:");
filepath = new JLabel(" filepath: ");
valuePanel = new JPanel();
questionIdValue = new JTextField();
titleValue = new JTextField();
answerValue = new JTextField();
typeValue = new JTextField();
levelValue = new JTextField();
statesVale = new JTextField();
remarksValue = new JTextField();
filepathValue = new JTextField();
submit = new JButton("Submit");
reSet = new JButton("ReSet");
font1 = new Font("Microsoft YaHei UI", Font.PLAIN, 20);
font2 = new Font("Microsoft YaHei UI", Font.PLAIN, 18);
}
public void setLabelPanelComponents(){
LabelPanel.setBounds(10, 10, 118, 453);
LabelPanel.setLayout(new GridLayout(8,1));
questionId = new JLabel("questionId: ");
questionId.setFont(font1);
LabelPanel.add(questionId);
title = new JLabel(" title:");
title.setFont(font1);
LabelPanel.add(title);
answer = new JLabel(" answer: ");
answer.setFont(font1);
LabelPanel.add(answer);
type = new JLabel(" type: ");
type.setFont(font1);
LabelPanel.add(type);
level = new JLabel(" level: ");
level.setFont(font1);
LabelPanel.add(level);
state = new JLabel(" state: ");
state.setFont(font1);
LabelPanel.add(state);
remarks = new JLabel(" remarks:");
remarks.setFont(font1);
LabelPanel.add(remarks);
filepath = new JLabel(" filepath: ");
filepath.setFont(font1);
LabelPanel.add(filepath);
}
public void setValuePanelComponents(){
valuePanel.setBounds(138, 10, 352, 453);
valuePanel.setLayout(new GridLayout(8,1));
questionIdValue.setFont(font2);
valuePanel.add(questionIdValue);
questionIdValue.setColumns(11);
titleValue.setFont(font2);
titleValue.setColumns(11);
valuePanel.add(titleValue);
answerValue.setFont(font2);
answerValue.setColumns(11);
valuePanel.add(answerValue);
typeValue.setFont(font2);
typeValue.setColumns(11);
valuePanel.add(typeValue);
levelValue.setFont(font2);
levelValue.setColumns(11);
valuePanel.add(levelValue);
statesVale.setFont(font2);
statesVale.setColumns(11);
valuePanel.add(statesVale);
remarksValue.setFont(font2);
remarksValue.setColumns(11);
valuePanel.add(remarksValue);
filepathValue.setFont(font2);
filepathValue.setColumns(11);
valuePanel.add(filepathValue);
}
public void setButtons(){
submit.setFont(font1);
submit.setBounds(10, 492, 210, 44);
submit.addActionListener(this);
reSet.setFont(font1);
reSet.setBounds(280, 492, 210, 44);
reSet.addActionListener(this);
}
JudgeQuestionController controller = new JudgeQuestionController();
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
switch(actionCommand){
case "Submit":
String text = questionIdValue.getText();
String text1 = titleValue.getText();
String text2 = answerValue.getText();
String text3 = typeValue.getText();
String text4 = levelValue.getText();
String text5 = statesVale.getText();
String text6 = remarksValue.getText();
String text7 = filepathValue.getText();
JudgeQuestion judgeQuestion = new JudgeQuestion(Integer.valueOf(text),text1,text2,text3,text4,text5,text6,text7);
controller.save(judgeQuestion);
this.dispose();
break;
case "ReSet":
questionIdValue.setText("");
titleValue.setText("");
answerValue.setText("");
typeValue.setText("");
levelValue.setText("");
statesVale.setText("");
remarksValue.setText("");
filepathValue.setText("");
break;
}
}
}
- IndexView
package com.yml.cn.view;
import com.yml.cn.controller.JudgeQuestionController;
import com.yml.cn.model.entity.JudgeQuestion;
import com.yml.cn.utils.GetView;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
/**
* @author yml
* @date 2021/06/8
*/
public class IndexView extends JFrame implements ActionListener {
private JPanel operatePanel;
private JButton addBt;
private JButton delBt;
private JButton updBt;
private JButton queOneBt;
private JButton queAllBt;
private JScrollPane scrollPane;
private JTable table;
private Container contentPane;
private static AddView addView;
private UpdateView updateView;
public IndexView() {
setBounds(100, 100, 765, 439);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
init();
setOperatePanelComponents();
setScrollPaneComponets();
contentPane = this.getContentPane();
contentPane.add(operatePanel);
contentPane.add(scrollPane);
setVisible(true);
}
public void init(){
operatePanel = new JPanel();
addBt = new JButton("Add");
delBt = new JButton("Delete");
updBt = new JButton("Update");
queOneBt = new JButton("QueryOne");
queAllBt = new JButton("QueryAll");
scrollPane = new JScrollPane();
}
public void setOperatePanelComponents(){
operatePanel.setBounds(10, 10, 737, 99);
operatePanel.setLayout(null);
addBt.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
addBt.setBounds(10, 28, 133, 39);
addBt.addActionListener(this);
operatePanel.add(addBt);
delBt.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
delBt.setBounds(153, 28, 133, 39);
delBt.addActionListener(this);
operatePanel.add(delBt);
updBt = new JButton("Update");
updBt.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
updBt.setBounds(296, 28, 133, 39);
updBt.addActionListener(this);
operatePanel.add(updBt);
queOneBt = new JButton("QueryOne");
queOneBt.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
queOneBt.setBounds(439, 28, 133, 39);
queOneBt.addActionListener(this);
operatePanel.add(queOneBt);
queAllBt = new JButton("QueryAll");
queAllBt.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
queAllBt.setBounds(582, 28, 133, 39);
queAllBt.addActionListener(this);
operatePanel.add(queAllBt);
}
Object[][] objects = new Object[100][8];
public void setScrollPaneComponets(){
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 119, 731, 273);
table = new JTable();
table.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
table.setModel(new DefaultTableModel(
objects,
new String[] {
"questionId", "title", "answer", "type", "level", "state", "remarks", "filepath"
}
) {
Class[] columnTypes = new Class[] {
Integer.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
boolean[] columnEditables = new boolean[] {
false, false, false, false, false, false, false, true
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setResizable(false);
table.getColumnModel().getColumn(2).setResizable(false);
table.getColumnModel().getColumn(3).setResizable(false);
table.getColumnModel().getColumn(4).setResizable(false);
table.getColumnModel().getColumn(5).setResizable(false);
table.getColumnModel().getColumn(6).setResizable(false);
scrollPane.setViewportView(table);
}
//表格显示数据, 若第一个参数为空, 则对应的是显示单条数据
//两个参数有且仅一个为空
public void showTableValue(List<JudgeQuestion> judgeQuestions, JudgeQuestion judgeQuestion){
int rowCount = ((DefaultTableModel) (table.getModel())).getRowCount();
int c = 0;
while(table.getValueAt(c, 0) != null){
((DefaultTableModel) (table.getModel())).removeRow(c);
}
if(judgeQuestions != null){
JudgeQuestion question;
for(int i = 0; i < judgeQuestions.size(); i ++ ){
question = judgeQuestions.get(i);
table.setValueAt(question.getQuestionId(),i,0);
table.setValueAt(question.getTitle(),i,1);
table.setValueAt(question.getAnswer(),i,2);
table.setValueAt(question.getType(),i,3);
table.setValueAt(question.getLevel(),i,4);
table.setValueAt(question.getState(),i,5);
table.setValueAt(question.getRemarks(),i,6);
table.setValueAt(question.getFilepath(),i,7);
}
}else{
table.setValueAt(judgeQuestion.getQuestionId(),0,0);
table.setValueAt(judgeQuestion.getTitle(),0,1);
table.setValueAt(judgeQuestion.getAnswer(),0,2);
table.setValueAt(judgeQuestion.getType(),0,3);
table.setValueAt(judgeQuestion.getLevel(),0,4);
table.setValueAt(judgeQuestion.getState(),0,5);
table.setValueAt(judgeQuestion.getRemarks(),0,6);
table.setValueAt(judgeQuestion.getFilepath(),0,7);
}
}
JudgeQuestionController controller = new JudgeQuestionController();
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
switch(actionCommand){
case "Add":
GetView.openAddView();
break;
case "Delete":
int selectedRow = table.getSelectedRow();
if(selectedRow != -1){
Integer valueAt = (Integer)table.getValueAt(selectedRow, 0);
controller.deleteById(valueAt);
JOptionPane.showMessageDialog(this,"删除成功");
//删除所选择的一行数据
((DefaultTableModel)(table.getModel())).removeRow(selectedRow);
}else{
JOptionPane.showMessageDialog(this,"请选择删除的目标");
}
break;
case "Update":
int selectedRow1 = table.getSelectedRow();
if(selectedRow1 != -1){
Integer valueAt = (Integer)table.getValueAt(selectedRow1, 0);
JudgeQuestion question = controller.findById(valueAt, false);
UpdateView updateView = GetView.openUpdateView();
updateView.showValues(question);
}else{
JOptionPane.showMessageDialog(this,"请选择修改的目标");
}
break;
case "QueryOne":
String id = JOptionPane.showInputDialog(this, "输入查询的id");
controller.findById(Integer.parseInt(id),true);
break;
case "QueryAll":
List<JudgeQuestion> all = controller.findAll();
break;
}
}
}
- UpdateView
package com.yml.cn.view;
import com.yml.cn.controller.JudgeQuestionController;
import com.yml.cn.model.entity.JudgeQuestion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author yml
* @date 2021/06/8
*/
public class UpdateView extends JFrame implements ActionListener {
//左半边标签
private JPanel LabelPanel;
private JLabel questionId;
private JLabel title;
private JLabel answer;
private JLabel type;
private JLabel level;
private JLabel state;
private JLabel remarks;
private JLabel filepath;
//右半边文本框
private JPanel valuePanel;
private JTextField questionIdValue;
private JTextField titleValue;
private JTextField answerValue;
private JTextField typeValue;
private JTextField levelValue;
private JTextField statesVale;
private JTextField remarksValue;
private JTextField filepathValue;
//按钮
private JButton submit;
private JButton reSet;
private Font font1;
private Font font2;
private Container contentPane;
public UpdateView(){
init();
setLabelPanelComponents();
setValuePanelComponents();
setButtons();
setBounds(100, 100, 514, 583);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
contentPane = getContentPane();
contentPane.add(LabelPanel);
contentPane.add(valuePanel);
contentPane.add(submit);
contentPane.add(reSet);
//点击窗口右上角叉号隐藏并释放该窗体
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public void init(){
LabelPanel = new JPanel();
questionId = new JLabel("questionId: ");
title = new JLabel(" title:");
answer = new JLabel(" answer: ");
type = new JLabel(" type: ");
level = new JLabel(" level: ");
state = new JLabel(" state: ");
remarks = new JLabel(" remarks:");
filepath = new JLabel(" filepath: ");
valuePanel = new JPanel();
questionIdValue = new JTextField();
titleValue = new JTextField();
answerValue = new JTextField();
typeValue = new JTextField();
levelValue = new JTextField();
statesVale = new JTextField();
remarksValue = new JTextField();
filepathValue = new JTextField();
submit = new JButton("Submit");
reSet = new JButton("ReSet");
font1 = new Font("Microsoft YaHei UI", Font.PLAIN, 20);
font2 = new Font("Microsoft YaHei UI", Font.PLAIN, 18);
}
public void setLabelPanelComponents(){
LabelPanel.setBounds(10, 10, 118, 453);
LabelPanel.setLayout(new GridLayout(8,1));
questionId = new JLabel("questionId: ");
questionId.setFont(font1);
LabelPanel.add(questionId);
title = new JLabel(" title:");
title.setFont(font1);
LabelPanel.add(title);
answer = new JLabel(" answer: ");
answer.setFont(font1);
LabelPanel.add(answer);
type = new JLabel(" type: ");
type.setFont(font1);
LabelPanel.add(type);
level = new JLabel(" level: ");
level.setFont(font1);
LabelPanel.add(level);
state = new JLabel(" state: ");
state.setFont(font1);
LabelPanel.add(state);
remarks = new JLabel(" remarks:");
remarks.setFont(font1);
LabelPanel.add(remarks);
filepath = new JLabel(" filepath: ");
filepath.setFont(font1);
LabelPanel.add(filepath);
}
public void setValuePanelComponents(){
valuePanel.setBounds(138, 10, 352, 453);
valuePanel.setLayout(new GridLayout(8,1));
questionIdValue.setFont(font2);
questionIdValue.setEnabled(false);
valuePanel.add(questionIdValue);
questionIdValue.setColumns(11);
titleValue.setFont(font2);
titleValue.setColumns(11);
valuePanel.add(titleValue);
answerValue.setFont(font2);
answerValue.setColumns(11);
valuePanel.add(answerValue);
typeValue.setFont(font2);
typeValue.setColumns(11);
valuePanel.add(typeValue);
levelValue.setFont(font2);
levelValue.setColumns(11);
valuePanel.add(levelValue);
statesVale.setFont(font2);
statesVale.setColumns(11);
valuePanel.add(statesVale);
remarksValue.setFont(font2);
remarksValue.setColumns(11);
valuePanel.add(remarksValue);
filepathValue.setFont(font2);
filepathValue.setColumns(11);
valuePanel.add(filepathValue);
}
public void setButtons(){
submit.setFont(font1);
submit.setBounds(10, 492, 210, 44);
submit.addActionListener(this);
reSet.setFont(font1);
reSet.setBounds(280, 492, 210, 44);
reSet.addActionListener(this);
}
public void showValues(JudgeQuestion judgeQuestion){
questionIdValue.setText(String.valueOf(judgeQuestion.getQuestionId()));
titleValue.setText(judgeQuestion.getTitle());
answerValue.setText(judgeQuestion.getAnswer());
typeValue.setText(judgeQuestion.getType());
levelValue.setText(judgeQuestion.getLevel());
statesVale.setText(judgeQuestion.getState());
remarksValue.setText(judgeQuestion.getRemarks());
filepathValue.setText(judgeQuestion.getFilepath());
}
JudgeQuestionController controller = new JudgeQuestionController();
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
switch(actionCommand){
case "Submit":
String text = questionIdValue.getText();
String text1 = titleValue.getText();
String text2 = answerValue.getText();
String text3 = typeValue.getText();
String text4 = levelValue.getText();
String text5 = statesVale.getText();
String text6 = remarksValue.getText();
String text7 = filepathValue.getText();
JudgeQuestion judgeQuestion = new JudgeQuestion(Integer.valueOf(text),text1,text2,text3,text4,text5,text6,text7);
controller.update(judgeQuestion);
JOptionPane.showMessageDialog(this,"修改成功");
this.dispose();
break;
case "ReSet":
titleValue.setText("");
answerValue.setText("");
typeValue.setText("");
levelValue.setText("");
statesVale.setText("");
remarksValue.setText("");
filepathValue.setText("");
break;
}
}
}
Controller层
package com.yml.cn.controller;
import com.yml.cn.model.entity.JudgeQuestion;
import com.yml.cn.model.service.JudgeQuestionService;
import com.yml.cn.view.IndexView;
import java.util.List;
/**
* @author yml
* @date 2021/06/01
*/
public class JudgeQuestionController {
private static JudgeQuestionService judgeQuestionService;
private static IndexView indexView;
public JudgeQuestionController(JudgeQuestionService judgeQuestionService1, IndexView indexView1) {
judgeQuestionService = judgeQuestionService1;
indexView = indexView1;
}
public JudgeQuestionController(){
}
//第二个参数表示是否是单一的查询(即最终目的就是查询, 查询后没有update等操作)
public JudgeQuestion findById(Integer id, boolean isQuery){
JudgeQuestion judgeQuestion = judgeQuestionService.findById(id);
if(isQuery) indexView.showTableValue(null,judgeQuestion);
return judgeQuestion;
}
public List<JudgeQuestion> findAll(){
List<JudgeQuestion> all = judgeQuestionService.findAll();
indexView.showTableValue(all,null);
return all;
}
public void save(JudgeQuestion judgeQuestion){
int save = judgeQuestionService.save(judgeQuestion);
List<JudgeQuestion> all = judgeQuestionService.findAll();
indexView.showTableValue(all,null);
}
public void update(JudgeQuestion judgeQuestion){
judgeQuestionService.update(judgeQuestion);
List<JudgeQuestion> all = judgeQuestionService.findAll();
indexView.showTableValue(all,null);
}
public void deleteById(Integer id){
judgeQuestionService.deleteById(id);
}
}
资源
- 数据库连接配置文件(数据库名design_pattern换成自己的,password换成自己的)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/design_pattern?useUnicode=true&Encoding=UTF-8
user=root
password=root123
- 用到的jar包
添加链接描述
上一篇: C#基础笔记