在学完java基础篇的时候,安排做了一个综合版的java基础项目,可以把之前学到的知识综合运用。
运用这种设计模式的优点:
MVC 是一种程序开发设计模式,它实现了显示模块与功能模块的分离。提高了程序的可维护性、可移植性、可扩展性与可重用性,降低了程序的开发难度。它主要分模型、视图、控制器三层。
M model业务模型(pojo/domain/bean)与现实中实体类联系
V views视图层(views)
图形化界面(gui)
C controller
用户与软件交互,处理信息
dao层(数据持久化)
提供数据
service层(处理逻辑)
处理数据
controller(javaweb里边的内容,servlet)
其中的GUI代码案例:
package org.vector.view;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.vector.bean.Book;
import org.vector.bean.Booktype;
import org.vector.bean.Borrowbook;
import org.vector.bean.Reader;
import org.vector.bean.Readertype;
import org.vector.bean.Users;
import org.vector.serviceImpl.UsersServiceImpl;
public class BookLogin extends JFrame{
public BookLogin() {
buliding();
addListeners();
}
public static String name1;
public static List<Users> list = new ArrayList<Users>();
public static List<Reader> list1 = new ArrayList<Reader>();
public static List<Book> list2 = new ArrayList<Book>();
public static List<Readertype> list3 = new ArrayList<Readertype>();
public static List<Booktype> list4 = new ArrayList<Booktype>();
public static List<Borrowbook> list5 = new ArrayList<Borrowbook>();
private JButton login,reset,register;
private JLabel name,password,label;
private JTextField name_Text;
private JPasswordField password_Text;
private void buliding() {
// TODO Auto-generated method stub
setLayout(null);
background();
setTitle("图书借阅系统登录界面");
Font font = new Font("圆体", Font.BOLD, 50);
label = new JLabel("图书借阅系统");
label.setFont(font);
label.setBounds(40, 20, 400, 50);
add(label);
name = new JLabel("用户名:");
name.setBounds(45, 100, 120, 30);
add(name);
name_Text = new JTextField(20);
name_Text.setBounds(100, 100, 200, 30);
add(name_Text);
password = new JLabel("密 码:");
password.setBounds(45, 150, 120, 30);
add(password);
password_Text = new JPasswordField(20);
password_Text.setBounds(100, 150, 200, 30);
add(password_Text);
login = new JButton("登录");
login.setBounds(50, 200, 100, 35);
add(login);
reset = new JButton("重置");
reset.setBounds(150, 200, 100, 35);
add(reset);
// register = new JButton("注册");
// register.setBounds(250, 200, 100, 35);
// add(register);
setBounds(400,300,400,300);
setLocation((2000-getWidth())/2,(1000-getHeight())/2);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void background() {
setSize(600, 500);
setLocation(100, 100);
String path = "Login.jpg";
ImageIcon background = new ImageIcon(path);
JLabel label = new JLabel(background);
label.setBounds(0, 0, this.getWidth(), this.getHeight());
JPanel imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addListeners() {
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
int flag = 0;
String name = name_Text.getText();
String password = password_Text.getText();
name1 = name;
UsersServiceImpl user = new UsersServiceImpl();
BookLogin.list.clear();
BookLogin.list.addAll(user.findUsers());
for (int i = 0; i < BookLogin.list.size(); i++) {
if(name.equals(BookLogin.list.get(i).getName())&&password.equals(BookLogin.list.get(i).getPassword())){
JOptionPane.showMessageDialog(null, "登录成功", "标题", JOptionPane.WARNING_MESSAGE);
BookLogin.this.dispose();
new BorrowbookView().setVisible(true);
flag = 1;
BookLogin.this.dispose();
new BorrowbookView().setVisible(true);
break;
}
}
if(flag == 0) {
JOptionPane.showMessageDialog(null, "登录失败,该用户不存在", "标题", JOptionPane.WARNING_MESSAGE);
}
}
});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BookLogin.this.dispose();
new BookLogin().setVisible(true);
}
});
// register.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// BookLogin.this.dispose();
// new UserAdd().setVisible(true);
// }
// });
}
}
用了c3p0对原生JDBC的封装思想,大大提高了开发者的效率。
c3p0xml配置文件
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mybook?characterEncoding=UTF8
property>
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="user">rootproperty>
<property name="password">passwordproperty>
<property name="initialPoolSize">6property>
<property name="maxPoolSize">10property>
<property name="maxIdleTime">10000property>
default-config>
c3p0-config>
I/O流是开发过程中,最耗费,最占用资源的一门技术,在开发中尽量减少对I/O的使用。
面向对象的思想是在整个学java期间,都不断去学习,这种思想是潜移默化的,短时间内,往往难以清楚地理解。
由于东西较多,后面的我就不一一演示了,大家有兴趣的可以自己做一个更好的。