JavaSwing + Mybatis 学生信息管理系统
这是最近两天做的项目(非常简陋,代码也比较臃肿,但是功能基本还是实现),实现了学生信息的增删改查功能
- 项目结构&jar包
-
1. 登录页面
1.1页面效果
1.2页面代码。
public class Login extends JFrame implements ActionListener{
private JLabel jl_title,jl_id,jl_password,jl_code;
private JTextField jtf_num,jtf_code;
private JPasswordField jpf_password;
private JButton jb_login,jb_exit,jb_code;
private ImageIcon icon;
private Code code = new Code();
public Login() {
getContentPane().setLayout(null);
this.setSize(445, 350);
this.setTitle("学生信息管理系统");
jl_title = new JLabel("学生信息管理系统");
jl_title.setFont(new Font("微软雅黑", Font.BOLD, 25));
jl_title.setBounds(121, 10, 200, 56);
getContentPane().add(jl_title);
jl_id = new JLabel("账 号");
jl_id.setFont(new Font("微软雅黑", Font.PLAIN, 16));
jl_id.setBounds(67, 88, 58, 31);
getContentPane().add(jl_id);
jtf_num = new JTextField();
jtf_num.setBounds(135, 89, 224, 31);
getContentPane().add(jtf_num);
jtf_num.setColumns(10);
jl_password = new JLabel("密 码");
jl_password.setFont(new Font("微软雅黑", Font.PLAIN, 16));
jl_password.setBounds(67, 145, 54, 31);
getContentPane().add(jl_password);
jpf_password = new JPasswordField();
jpf_password.setBounds(135, 146, 224, 31);
getContentPane().add(jpf_password);
jb_login = new JButton("登 录");
jb_login.setFont(new Font("微软雅黑", Font.PLAIN, 16));
jb_login.setBounds(83, 251, 110, 31);
getContentPane().add(jb_login);
jb_exit = new JButton("退 出");
jb_exit.setFont(new Font("微软雅黑", Font.PLAIN, 16));
jb_exit.setBounds(232, 251, 110, 31);
getContentPane().add(jb_exit);
jl_code = new JLabel("验证码");
jl_code.setFont(new Font("微软雅黑", Font.PLAIN, 16));
jl_code.setBounds(67, 192, 54, 31);
getContentPane().add(jl_code);
icon = new ImageIcon(code.codeImage());
jb_code = new JButton();
jb_code.setBounds(233, 187, 126, 44);
jb_code.setIcon(icon);
getContentPane().add(jb_code);
jtf_code = new JTextField();
jtf_code.setBounds(135, 187, 88, 43);
getContentPane().add(jtf_code);
jb_login.addActionListener(this);
jb_exit.addActionListener(this);
jb_code.addActionListener(this);
this.setVisible(true);
this.setResizable(false);
}
}
1.3处理方法
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jb_login) {
String aId = jtf_num.getText();
String aPassword = jpf_password.getText();
String co = jtf_code.getText();
if (code.verify(co)) {
SqlSession openSession = null;
try {
openSession = new SqlSessionFactoryTool().getSqlSession();
mapper login = openSession.getMapper(mapper.class);
Admin admin = login.login(aId, aPassword);
if (admin != null) {
//保存临时变量
TAdmin.setaId(admin.getaId());
TAdmin.setaName(admin.getaName());
TAdmin.setaPassword(admin.getaPassword());
JOptionPane.showMessageDialog(this,"登录成功!","提示",JOptionPane.INFORMATION_MESSAGE);
mune mune = new mune();
Toolkit tookit = mune.getToolkit();
Dimension dm = tookit.getScreenSize();
mune.setLocation((dm.width - mune.getWidth()) / 2,
(dm.height - mune.getHeight()) / 2);
mune.setVisible(true);
this.dispose();
}else {
JOptionPane.showMessageDialog(this,"账号或密码错误!","提示",JOptionPane.INFORMATION_MESSAGE);
icon.setImage(code.newCodeImage());
}
} catch (IOException e1) {
JOptionPane.showMessageDialog(this,"登录失败!","提示",JOptionPane.INFORMATION_MESSAGE);
e1.printStackTrace();
}
finally {
openSession.close();
}
}else {
JOptionPane.showMessageDialog(this,"验证码错误,请重试!","提示",JOptionPane.INFORMATION_MESSAGE);
icon.setImage(code.newCodeImage());
}
}
if (e.getSource()==jb_exit) {
int exitChoose = JOptionPane.showConfirmDialog(this, "是否要退出本系统?", "退出系统", JOptionPane.OK_CANCEL_OPTION);
if (exitChoose == JOptionPane.OK_OPTION) {
System.exit(0);
} else {
return;
}
}
if (e.getSource()==jb_code) {
icon.setImage(code.newCodeImage());
}
}
1.4验证码、保存用户信息和返回SqlSession的工具类
1.4.1验证码
这里我用到了hutool工具类的jar包
public class Code {
private LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(126, 44);
//获取初始验证图片
public Image codeImage(){
//System.out.println(lineCaptcha.getCode());
return lineCaptcha.createImage(lineCaptcha.getCode());
}
//重新生成验证码和图片
public Image newCodeImage() {
lineCaptcha.createCode();
//System.out.println(lineCaptcha.getCode());
return lineCaptcha.createImage(lineCaptcha.getCode());
}
//验证验证是否正确
public Boolean verify(String code) {
//System.out.println("code:"+code);
return lineCaptcha.verify(code);
}
}
1.4.2保存类对象临时信息类
public class TAdmin {
private static Integer aId = null;
private static String aName = null;
private static String aPassword = null;
public static Integer getaId() {
return aId;
}
public static void setaId(Integer aId) {
TAdmin.aId = aId;
}
public static String getaName() {
return aName;
}
public static void setaName(String aName) {
TAdmin.aName = aName;
}
public static String getaPassword() {
return aPassword;
}
public static void setaPassword(String aPassword) {
TAdmin.aPassword = aPassword;
}
}
1.4.3 返回SqlSession操作对象类
public class SqlSessionFactoryTool {
public SqlSession getSqlSession() throws IOException{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
}
2. 主页面
主页面中有一个jPanel组件,其它操作时就把组件内容用.removeAll()方法清楚所有内容,再调用相应的生成组件的类 调用Jpanel的add()方法,再调用getContentPane().add()添加jPanel组件(详细过程看处理方法)
2.1页面效果
2.2 界面代码
public class mune extends JFrame implements ActionListener{
private JButton jb_add,jb_update_password,jb_update,jb_delete,jb_select,jb_exit;
private JPanel panel;
private JLabel jl_title;
private JLabel lblNewLabel;
public mune() {
this.setSize(700, 530);
this.setTitle("学生信息管理系统");
getContentPane().setLayout(null);
jb_select = new JButton("查询学生信息");
jb_select.setBounds(10, 50, 119, 36);
jb_select.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_select);
jb_add = new JButton("添加学生信息");
jb_add.setBounds(10, 110, 119, 36);
jb_add.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_add);
jb_update = new JButton("修改学生信息");
jb_update.setBounds(10, 170, 119, 36);
jb_update.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_update);
jb_delete = new JButton("删除学生信息");
jb_delete.setBounds(10, 240, 119, 36);
jb_delete.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_delete);
jb_update_password = new JButton("修改登录密码");
jb_update_password.setBounds(10, 310, 119, 36);
jb_update_password.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_update_password);
jb_exit = new JButton("退出系统");
jb_exit.setBounds(10, 380, 119, 36);
jb_exit.setFont(new Font("微软雅黑", Font.PLAIN, 14));
getContentPane().add(jb_exit);
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(140, 10, 554, 481);
getContentPane().add(panel);
jl_title = new JLabel("学生信息管理系统");
jl_title.setFont(new Font("微软雅黑", Font.PLAIN, 26));
jl_title.setBounds(173, 10, 213, 36);
panel.add(jl_title);
lblNewLabel = new JLabel("欢迎使用本系统!");
lblNewLabel.setForeground(Color.BLUE);
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 28));
lblNewLabel.setBounds(162, 166, 224, 107);
panel.add(lblNewLabel);
jb_add.addActionListener(this);
jb_update_password.addActionListener(this);
jb_update.addActionListener(this);
jb_delete.addActionListener(this);
jb_select.addActionListener(this);
jb_exit.addActionListener(this);
this.setVisible(true);
this.setResizable(false);
}
}
2.3处理方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb_add) {
panel.removeAll();
panel.add(new add().getAddJPanel());
getContentPane().add(panel);
this.repaint();
}if(e.getSource()==jb_update_password) {
panel.removeAll();
panel.add(new updatePassword().getJpanel());
getContentPane().add(panel);
this.repaint();
}if(e.getSource()==jb_update) {
panel.removeAll();
panel.add(new update().getUpdateJPanel());
getContentPane().add(panel);
this.repaint();
}if(e.getSource()==jb_delete) {
panel.removeAll();
panel.add(new delete().getDelectJPanel());
getContentPane().add(panel);
this.repaint();
}if(e.getSource()==jb_select) {
panel.removeAll();
panel.add(new selete().getSelectJPanel());
getContentPane().add(panel);
this.repaint();
}if(e.getSource()==jb_exit) {
int exitChoose = JOptionPane.showConfirmDialog(this, "是否要退出本系统?", "退出系统", JOptionPane.OK_CANCEL_OPTION);
if (exitChoose == JOptionPane.OK_OPTION) {
System.exit(0);
} else {
return;
}
}
}
3. 查询页面
3.1页面效果(涉及信息的我就加了马赛克了),实现了多条件模糊分页查询,并显示页面,生成表格需要把数据转为二维数组
3.2页面代码
public class selete extends JPanel implements ActionListener {
private JTable table;
private JLabel jl_title, jl_name, jl_id, jl_di, jl_ye, jl_gong;
private JTextField jtf_id, jtf_name, jft_go;
private JPanel panel;
private JButton jb_first, jb_up, jb_down, jb_seach, jb_jump;
private JScrollPane line;
private DefaultTableModel stuTable;
private int page = 1, limit = 10, fristPage = 1, lastPage, count;
private List<Student> students;
private String[][] result = null;
private SqlSession session = null;
private static String[] columnNames = { "学号", "姓名", "性别", "生日", "年龄", "系别", "宿舍" };
private JButton jb_last;
public JPanel getSelectJPanel() {
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(null, null, (page - 1) * limit, limit);
count = mapper.countStu(null, null);
lastPage = count / limit + 1;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "初始化失败!", JOptionPane.ERROR_MESSAGE);
} finally {
session.close();
}
setLayout(null);
panel = new JPanel();
panel.setSize(554, 481);
panel.setLayout(null);
jl_title = new JLabel("学生信息查询");
jl_title.setFont(new Font("微软雅黑", Font.PLAIN, 26));
jl_title.setBounds(174, 10, 165, 34);
panel.add(jl_title);
// 表格显示
result = toArray(students);
// 转为二维数组
stuTable = new DefaultTableModel(result, columnNames) {
public boolean isCellEditable(int row, int column) {
return false;
}
};// 可以被选中但不可被编辑
table = new JTable(stuTable);
setWidth();
table.setEnabled(false);
line = new JScrollPane(table);
line.setBounds(1, 117, 552, 313);
panel.add(line);
jl_id = new JLabel("学 号");
jl_id.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_id.setBounds(23, 63, 40, 25);
panel.add(jl_id);
jl_name = new JLabel("姓 名");
jl_name.setBounds(210, 63, 40, 25);
jl_name.setFont(new Font("微软雅黑", Font.PLAIN, 14));
panel.add(jl_name);
jtf_id = new JTextField();
jtf_id.setBounds(73, 63, 100, 25);
jtf_id.setColumns(10);
panel.add(jtf_id);
jtf_name = new JTextField();
jtf_name.setColumns(10);
jtf_name.setBounds(262, 63, 100, 25);
panel.add(jtf_name);
jb_seach = new JButton("搜 索");
jb_seach.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_seach.setBounds(403, 63, 80, 25);
panel.add(jb_seach);
jb_first = new JButton("首页");
jb_first.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_first.setBounds(10, 450, 65, 25);
panel.add(jb_first);
jb_up = new JButton("上页");
jb_up.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_up.setBounds(73, 450, 65, 25);
panel.add(jb_up);
jb_down = new JButton("下页");
jb_down.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_down.setBounds(136, 450, 65, 25);
panel.add(jb_down);
jb_last = new JButton("尾页");
jb_last.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_last.setBounds(197, 450, 65, 25);
panel.add(jb_last);
jft_go = new JTextField();
jft_go.setBounds(441, 453, 25, 20);
panel.add(jft_go);
jft_go.setColumns(10);
jb_jump = new JButton("跳转");
jb_jump.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_jump.setBounds(483, 450, 65, 25);
panel.add(jb_jump);
jl_di = new JLabel("第");
jl_di.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_di.setBounds(427, 450, 14, 25);
panel.add(jl_di);
jl_ye = new JLabel("页");
jl_ye.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_ye.setBounds(466, 450, 14, 25);
panel.add(jl_ye);
jl_gong = new JLabel("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
jl_gong.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_gong.setBounds(275, 451, 142, 25);
panel.add(jl_gong);
jb_seach.addActionListener(this);
jb_first.addActionListener(this);
jb_up.addActionListener(this);
jb_down.addActionListener(this);
jb_last.addActionListener(this);
jb_jump.addActionListener(this);
panel.setVisible(true);
return panel;
}
}
3.3List转二维数组方法
private String[][] toArray(List<Student> stuList) {
String[][] result = new String[students.size()][7];
for (int i = 0; i < result.length; i++) {
result[i][0] = students.get(i).getNum();
result[i][1] = students.get(i).getName();
result[i][2] = students.get(i).getGender();
result[i][3] = students.get(i).getBirthday();
result[i][4] = "" + students.get(i).getAge();
result[i][5] = students.get(i).getMajor();
result[i][6] = students.get(i).getDorm();
}
return result;
}
3.4JTable设置列宽方法
// 设置表单列宽,每次更新完都有设置
private void setWidth() {
table.setRowHeight(29);
table.getColumnModel().getColumn(2).setPreferredWidth(40);
table.getColumnModel().getColumn(4).setPreferredWidth(40);
table.getColumnModel().getColumn(5).setPreferredWidth(110);
table.getColumnModel().getColumn(6).setPreferredWidth(110);
table.setRowSelectionAllowed(false);
}
3.5处理方法
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb_seach) {
String num = jtf_id.getText();
String name = jtf_name.getText();
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
page = fristPage;
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
lastPage = count / limit + 1;
result = toArray(students);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
stuTable.setDataVector(result, columnNames);
setWidth();
jft_go.setText("");
panel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
session.close();
}
}
if (e.getSource() == jb_first) {
if (page == 1) {
JOptionPane.showMessageDialog(this, "当前为首页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
page = fristPage;
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
lastPage = count / limit + 1;
result = toArray(students);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
session.close();
}
}
}
if (e.getSource() == jb_up) {
if (page == 1) {
JOptionPane.showMessageDialog(this, "当前为首页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
page = page - 1;
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
result = toArray(students);
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
session.close();
}
}
}
if (e.getSource() == jb_down) {
if (page == lastPage) {
JOptionPane.showMessageDialog(this, "当前为最后一页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
page = page + 1;
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
result = toArray(students);
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
session.close();
}
}
}
if (e.getSource() == jb_last) {
if (page == lastPage) {
JOptionPane.showMessageDialog(this, "当前为最后一页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
page = lastPage;
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
result = toArray(students);
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
session.close();
}
}
}
if (e.getSource() == jb_jump) {
String num = jtf_id.getText();
String name = jtf_name.getText();
String toPage = jft_go.getText();
try {
page = Integer.parseInt(toPage);
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, "只能输入数字 !", "提示", JOptionPane.INFORMATION_MESSAGE);
}
if (page < fristPage || page > lastPage) {
JOptionPane.showMessageDialog(this, "请输入正确的范围 !", "提示", JOptionPane.INFORMATION_MESSAGE);
return ;
}
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
result = toArray(students);
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
session.close();
}
}
}
4. 其它页面
- 都是页面代码,没什么特别的地方,所以不展示出来了
5. 数据操作
5.1 Mybatis配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/studentsm" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper.xml" />
</mappers>
</configuration>
5.2 mapper接口
public interface mapper {
//管理员登录
public Admin login(@Param("aName")String aName,@Param("aPassword")String aPassword);
//管理员登录修改密码
public Integer updatePassword(Admin admin);
//根据Id查询学生信息
public Student selectStuByNum(String num);
//多条件查询学生信息并分页
public List<Student> selectStuBySomething(@Param("num")String num,@Param("name")String name,
@Param("page")Integer page,@Param("limit")Integer limit);
//多条件查询符合要求学生的总数
public Integer countStu(@Param("num")String num,@Param("name")String name);
//根据Id删除学生信息
public Integer deleteStu(String num);
//根据Id修改学生信息
public Integer updateStu(Student student);
//添加学生信息
public Integer addStu(Student student);
}
5.3 mapper实现
不展示