三層架構(gòu)
三層架構(gòu)
用三層架構(gòu)做一個(gè)登錄,注冊(cè)
項(xiàng)目環(huán)境:
Java+eclipse+MySQL+Navicat/sqlyog
三層思想分析:
登錄注冊(cè)三層思想分析
步驟:
1:創(chuàng)建項(xiàng)目,建立層次
edu.eurasia.app 存放主方法(即程序入口)
edu.eurasia.dao dao層
edu.eurasia.service service層
edu.eurasia.domain JavaBean層(實(shí)體類層)
edu.eurasia.utils 工具類層
edu.eurasia.view 存放實(shí)現(xiàn)抽象頁面
edu.eurasia.abview 存放抽象頁面
創(chuàng)建項(xiàng)目冈欢,建立層次
2:創(chuàng)建數(shù)據(jù)庫、表盈简、插入測(cè)試數(shù)據(jù)
創(chuàng)建數(shù)據(jù)庫凑耻、表、插入測(cè)試數(shù)據(jù)
3:導(dǎo)入JDBC相關(guān)的jar包和工具類
jar包
連接數(shù)據(jù)庫
工具類
4:在edu.eurasia.domain下創(chuàng)建相關(guān)實(shí)體類
實(shí)體類的書寫規(guī)范:
a)該類有私有屬性(屬性名 要和 數(shù)據(jù)庫 表中的列名 要一樣)
b)該類具備無參構(gòu)造
c)該類提供 set get方法
實(shí)體類
User類代碼:
/*
* 實(shí)體類
* javabean類
*/
public class User {
private int uid;
private String uname;
private String pwd;
public User() {
super();
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd + "]";
}
}
5:將做好的抽象GUI頁面類柠贤,存放到抽象頁面層edu.eurasia.abview
登錄和注冊(cè)的抽象GUI類
AbstractRegDialog類:
public abstract class AbstractRegDialog extends JDialog {
public JLabel registLabel1 = new JLabel("用戶注冊(cè)"); // 注冊(cè)標(biāo)簽1
public JLabel userNameLabel = new JLabel("用戶名"); // 用戶名標(biāo)簽
public JTextField userNameField = new JTextField(); // 用戶名文本框
public JLabel passwordLabel = new JLabel("密碼"); // 密碼標(biāo)簽
public JPasswordField passwordField = new JPasswordField(); // 密碼文本框
public JLabel rePasswordLabel = new JLabel("重復(fù)密碼"); // 重復(fù)密碼標(biāo)簽
public JPasswordField rePasswordField = new JPasswordField(); // 重復(fù)密碼文本框
public JLabel verifyCodeLabel = new JLabel("驗(yàn)證碼"); // 驗(yàn)證碼標(biāo)簽
public JTextField verifyCodeField = new JTextField(); // 驗(yàn)證碼標(biāo)簽
public JLabel verifyCodeLabel2 = new JLabel(); // 顯示驗(yàn)證碼標(biāo)簽
public JButton registBtn = new JButton("注\t冊(cè)");// 注冊(cè)按鈕
public JButton resetBtn = new JButton("重\t置");// 重置按鈕
public AbstractRegDialog(Frame owner) {
init(); // 初始化窗口
addComponent(); // 添加組件
addlistener(); // 事件加載
}
public AbstractRegDialog() {
init(); // 初始化窗口
addComponent(); // 添加組件
addlistener(); // 事件加載
}
// 初始化窗口
private void init() {
setTitle("注冊(cè)");
setSize(600, 360);
// 去除布局
setLayout(null);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
private void addComponent() {
// 添加注冊(cè)標(biāo)題
registLabel1.setFont(new Font("楷體", Font.BOLD, 20));
registLabel1.setBounds(220, 20, 100, 30);
this.add(registLabel1);
// 用戶名文本框
userNameLabel.setBounds(100, 80, 60, 25);
userNameField.setBounds(160, 80, 120, 25);
this.add(userNameLabel);
this.add(userNameField);
// 密碼文本框
passwordLabel.setBounds(100, 110, 60, 25);
passwordField.setBounds(160, 110, 120, 25);
this.add(passwordLabel);
this.add(passwordField);
// 密碼文本框
rePasswordLabel.setBounds(290, 110, 60, 25);
rePasswordField.setBounds(350, 110, 120, 25);
this.add(rePasswordLabel);
this.add(rePasswordField);
// 驗(yàn)證碼
verifyCodeLabel.setBounds(100, 140, 60, 25);
verifyCodeField.setBounds(160, 140, 120, 25);
verifyCodeLabel2.setBounds(290, 140, 60, 25);
verifyCodeLabel2.setBackground(new Color(255, 255, 255));
verifyCodeLabel2.setOpaque(true); //設(shè)置組件JLabel不透明香浩,只有設(shè)置為不透明,設(shè)置背景色才有效
verifyCodeLabel2.setText(" 1111");
this.add(verifyCodeLabel);
this.add(verifyCodeField);
this.add(verifyCodeLabel2);
// 注冊(cè)按鈕
registBtn.setBounds(160, 240, 80, 25);
this.add(registBtn);
// 重置按鈕
resetBtn.setBounds(260, 240, 80, 25);
this.add(resetBtn);
}
// 添加監(jiān)聽器(事件加載)
public void addlistener() {
// 表單重置
resetBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userNameField.setText("");
passwordField.setText("");
rePasswordField.setText("");
verifyCodeField.setText("");
userNameField.requestFocus();
}
});
registBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 獲取用戶輸入的注冊(cè)信息
String userName = userNameField.getText();
char[] passwordArray = passwordField.getPassword();
String password = new String(passwordArray);
char[] repasswordArray = rePasswordField.getPassword();
String rePassword = new String(repasswordArray);
String verifyCode = verifyCodeField.getText();
// 使用用戶輸入的注冊(cè)信息調(diào)用regist方法完成注冊(cè)
try {
regist(userName, password, rePassword, verifyCode);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
// 注冊(cè)方法
public abstract void regist(String userName, String password, String rePassword, String verifyCode) throws Exception ;
}
注冊(cè)對(duì)話框
AbstractRLFrame類:
public abstract class AbstractRLFrame extends JFrame {
public AbstractRLFrame() {
this("登錄注冊(cè)");
}
public AbstractRLFrame(String title) {
super(title);
init();
addPanel();
addButton();
addListener();
}
private void addListener() {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
register();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
login();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
private void init() {
this.setSize(600, 400);
this.setLocation(300, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
}
JButton button = new JButton("注冊(cè)");
JButton button2 = new JButton("登錄");
private void addButton() {
button.setBounds(100, 250, 100, 50);
button2.setBounds(350, 250, 100, 50);
this.add(button);
this.add(button2);
}
public JLabel registLabel1 = new JLabel(); // 歡迎標(biāo)簽1
JPanel jPanel = new JPanel();
JLabel jLabel = new JLabel();
JLabel jLabel2 = new JLabel();
protected TextField tf = new TextField();
protected TextField tf2 = new TextField();
private void addPanel() {
jPanel.setLayout(null);
jPanel.setBackground(new Color(0x808000));
jPanel.setBounds(0, 0, 600, 200);
this.add(jPanel);
jLabel.setText("用戶名:");
jLabel.setBounds(100, 100, 50, 30);
jPanel.add(jLabel);
tf.setBounds(200, 100, 200, 30);
tf.setFont(new Font("宋體", Font.BOLD, 20));
jPanel.add(tf);
jLabel2.setText("密碼:");
jLabel2.setBounds(100, 150, 50, 30);
jPanel.add(jLabel2);
tf2.setBounds(200, 150, 200, 30);
tf2.setFont(new Font("宋體", Font.BOLD, 20));
jPanel.add(tf2);
// 添加注冊(cè)標(biāo)題
registLabel1.setText("登錄注冊(cè)");
registLabel1.setFont(new Font("楷體", Font.BOLD, 20));
registLabel1.setBounds(220, 20, 100, 30);
jPanel.add(registLabel1);
}
public abstract void login() throws SQLException;
public abstract void register() throws SQLException;
}
登錄注冊(cè)主界面
6:創(chuàng)建程序入口MainApp.java在edu.eurasia.app層里
在這個(gè)類里寫主方法(只有一個(gè)主方法)
/*
* 程序入口
*/
public class MainApp {
public static void main(String[] args) {
//創(chuàng)建主頁面對(duì)象
RLFrame rf = new RLFrame();
//設(shè)置顯示可見
rf.setVisible(true);
}
}
7:創(chuàng)建實(shí)現(xiàn)抽象GUI類的RLFrame.java和RegDialog.java
RegDialog類
/*
* 注冊(cè)頁面
*/
public class RegDialog extends AbstractRegDialog{
/*
*注冊(cè)完成 提交 操作
*/
@Override
public void regist(String userName, String password, String rePassword, String verifyCode) throws Exception {
System.out.println("提交注冊(cè)操作");
/*
* 驗(yàn)證 碼 不管了 沒有用到
*
* 驗(yàn)證 密碼兩次是否一致
*/
if(!password.equals(rePassword)){
JOptionPane.showMessageDialog(this, "兩次密碼不一致");
return;
}
//驗(yàn)證用戶名是否存在
//依賴于 service層
UserService userService = new UserService();
boolean b = userService.isHave(userName);
if(b){
//說明用戶存在
JOptionPane.showMessageDialog(this, "用戶已經(jīng)存在");
return;
}
//完成注冊(cè)
userService.addUser(userName, password);
JOptionPane.showMessageDialog(this, "注冊(cè)成功");
}
}
RLFrame類
/*
* 登錄注冊(cè)主頁面
*/
public class RLFrame extends AbstractRLFrame{
/*
* 登錄功能
* 1:獲取頁面上
* 用戶名和密碼的兩個(gè)文本框
* tf 變量 代表 用戶名框
* tf2變量 代表 密碼框
*
*
* 2:檢查 文本框中的用戶名或密碼是不是空格
*
* 3:需要查詢數(shù)據(jù)庫 進(jìn)行校驗(yàn)
* 如果 用戶名密碼匹配成功 登錄成功
* 否則登錄失敗
*/
@Override
public void login() throws SQLException {
System.out.println("登錄");
//1:獲取頁面上的用戶名和密碼 兩個(gè)文本框中的內(nèi)容
String username = tf.getText();//獲取用戶名 文本框中內(nèi)容
String password = tf2.getText();//獲取密碼 文本框中的容
//2:校驗(yàn)用戶名 或密碼 是不是 空
if(username.trim().equals("")||password.trim().equals("")){
//給個(gè)友情提示
//完成彈框
JOptionPane.showMessageDialog(this, "用戶名或密碼不能為空!!");
return;
}
//3:需要查詢數(shù)據(jù)庫 進(jìn)行校驗(yàn)
/*
* 校驗(yàn) 操作 交給 service去完成
*
*/
//需要?jiǎng)?chuàng)建Service對(duì)象
UserService userService = new UserService();
boolean login = userService.login(username, password);
if(login){
JOptionPane.showMessageDialog(this, "登錄成功~");
}else{
JOptionPane.showMessageDialog(this, "用戶名或密碼不正確臼勉,登錄失敗");
}
}
/*
* 注冊(cè)功能
*/
@Override
public void register() throws SQLException {
System.out.println("注冊(cè)");
/*
* 彈出注冊(cè)頁面
*/
RegDialog rd = new RegDialog();
rd.setVisible(true);
}
}
8:在edu.eurasia.service層邻吭,存放UserService
edu.eurasia.dao層,存放UserDao
UserService類:
/*
* 提取Service 層(服務(wù)層)
*
* a 創(chuàng)建一個(gè)service包
* b 創(chuàng)建一個(gè)XxxService類 Xxx代表我們將要操作的那個(gè)類 對(duì)象
* 就是給用戶 提供服務(wù)的
* 這里類中包含了 對(duì)所有 User以及user表 他們所有的業(yè)務(wù)處理
* 一些校驗(yàn)等等
* c 在Service層中 完成業(yè)務(wù)代碼編寫
*
*/
public class UserService {
//成員位置創(chuàng)建dao對(duì)象 這樣所有方法都可以使用
UserDao userDao = new UserDao();
/*
* 因?yàn)閣eb層 要將 登錄校驗(yàn)的功能 交給service層處理
* 那么 這里需要提供一個(gè)功能
* 完成 登錄校驗(yàn)操作
*
* 返回值 : boolean 是否校驗(yàn)成功
* 參數(shù)列表:要兩個(gè)參數(shù) 獲取到的用戶名和密碼
*/
public boolean login(String uname,String pwd) throws SQLException{
/*
* 校驗(yàn) uname pwd
* 根據(jù)用戶名 和密碼 去數(shù)據(jù)庫中查詢
* 返回的對(duì)象 不是null說明 登錄成
* 如果返回的對(duì)象是null說明用戶名或密碼錯(cuò)誤
*/
//寫查詢數(shù)據(jù)庫代碼 分層思想中 查詢數(shù)據(jù)庫代碼要提取出去 提取到dao層
//依賴 dao層
User user = userDao.getUser(uname, pwd);
/*
* 檢測(cè) 用戶是否為空 是空的話 沒有登錄成
*/
if(user==null){
return false;
}else{
return true;
}
}
/*
* 判斷用戶是否存在
*/
public boolean isHave(String uname) throws SQLException{
/*
* 校驗(yàn)uname用戶名是否存儲(chǔ)在
* 根據(jù)用戶名 去數(shù)據(jù)庫中查找
* dao已經(jīng)完成了 該功能
* 返回的對(duì)象 是null說明已經(jīng)存在
* 不是null說明用戶名不存在
*/
//需要dao 所以 創(chuàng)建dao對(duì)象
User user = userDao.getUserByName(uname);
if(user==null){//用戶不存在
return false;
}else{ //用戶存在
return true;
}
}
/*
* 添加用戶功能
*/
public void addUser(String uname,String pwd) throws SQLException{
//這個(gè)事情交給userdao做了
userDao.addUser(uname, pwd);
}
}
UserDao類:
/*
* 提取 dao層(數(shù)據(jù)訪問層)
*
* a 創(chuàng)建一個(gè)包 dao包
* b 創(chuàng)建XxxDao Xxx代表操作的那個(gè)類 也就是跟表對(duì)應(yīng)的實(shí)體
* 這個(gè)類中包含了所有 關(guān)于 該表的數(shù)據(jù)庫的訪問
* c 提供數(shù)據(jù)庫訪問代碼
*/
public class UserDao {
//在成員位置 創(chuàng)建QueryRunner對(duì)象 所有方法都可以使用
QueryRunner qr = new QueryRunner(JDBCUtils.getDS());
/*
* 根據(jù) 用戶名和密碼 去數(shù)據(jù)庫中查詢 用戶信息
*
* 返回值 是一個(gè)User對(duì)象
* 參數(shù) 用戶名和密碼
*/
public User getUser(String uname,String pwd) throws SQLException{
//2:寫sql
String sql = "select * from users where uname = ? and pwd = ?";
Object[] params = {uname,pwd};
User user = qr.query(sql, new BeanHandler<User>(User.class), params);
return user;
}
/*
* 根據(jù)用戶名 查詢 該用戶
*
*/
public User getUserByName(String uname) throws SQLException{
//2寫sql
String sql = "select * from users where uname = ?";
Object[] params = {uname};
User user = qr.query(sql, new BeanHandler<User>(User.class), params);
return user;
}
/*
* 完成用戶的添加
*/
public void addUser(String uname,String pwd) throws SQLException{
String sql = "insert into users values(?,?,?)";
Object[] params = {null,uname,pwd};
qr.update(sql, params);
}
}
以上就是登錄和注冊(cè)的全過程
但在一些大項(xiàng)目里宴霸,一般會(huì)將項(xiàng)目分為以下幾層:
但最核心的還是三層架構(gòu)
大項(xiàng)目分層架構(gòu)