IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 初步使用MVC三层架构 -> 正文阅读

[开发测试]初步使用MVC三层架构

Model

实体类

public class Boys {
    private int id;
    private String boyName;
    private int userCP;

    @Override
    public String toString() {
        return "Boys{" +
                "id=" + id +
                ", boyName='" + boyName + '\'' +
                ", userCP=" + userCP +
                '}';
    }

    public Boys(int id, String boyName, int userCP) {
        this.id = id;
        this.boyName = boyName;
        this.userCP = userCP;
    }

    public Boys() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBoyName() {
        return boyName;
    }

    public void setBoyName(String boyName) {
        this.boyName = boyName;
    }

    public int getUserCP() {
        return userCP;
    }

    public void setUserCP(int userCP) {
        this.userCP = userCP;
    }
}

DAO层

import com.miyon.entity.Boys;
import com.miyon.utils.JDBCUtilsByDruid;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BoysDao {

    public void addBoys(Boys boys) throws Exception{
        Connection conn = JDBCUtilsByDruid.getConnection();
        String sql = "insert into boys values (null,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,boys.getBoyName());
        ps.setInt(2,boys.getUserCP());

        ps.execute();
    }


    public void updateBoys(Boys boys) throws Exception{
        Connection conn = JDBCUtilsByDruid.getConnection();
        String sql = " update boys set boyName=?, userCp=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,boys.getBoyName());
        ps.setInt(2,boys.getUserCP());
        ps.setInt(3,boys.getId());

        ps.execute();
    }


    //筛选条件
    public List<Boys> queryAll(List<Map<String,Object>> params) throws Exception{
        List<Boys> result = new ArrayList<>();

        Connection conn = JDBCUtilsByDruid.getConnection();
        StringBuilder sb = new StringBuilder();
        // 1=1 永远成立                       小细节,以连接后边的sql语句
        sb.append("select * from boys where 1=1");

        if(params!=null && params.size()!=0){
            for (int i = 0; i < params.size(); i++) {
                Map<String,Object> map = params.get(i);
                sb.append(" and "+map.get("name")+ " "+map.get("relation")+" "+map.get("value")+" ");

            }
            PreparedStatement ps = conn.prepareStatement(sb.toString());
            ResultSet set = ps.executeQuery();

            Boys boys = null;
            while (set.next()){
                boys = new Boys();
                boys.setId(set.getInt("id"));
                boys.setBoyName(set.getString("boyName"));
                boys.setUserCP(set.getInt("userCp"));
                result.add(boys);

            }
            System.out.println(sb);
            return result;
        }
        return null;
    }


    public void deleteBoys(Boys boys) throws Exception{
        Connection conn = JDBCUtilsByDruid.getConnection();
        String sql = " update boys set userCp = ? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,boys.getUserCP());
        ps.setInt(2,boys.getId());

        ps.execute();
    }


    //查询所有信息
    public List<Boys> queryBoys() throws Exception{
        Connection conn = JDBCUtilsByDruid.getConnection();
        String sql = "select * from boys ";
        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet set = ps.executeQuery();

        List<Boys> list  = new ArrayList<>();
        Boys boys = null;
        while (set.next()){
            boys = new Boys();
            boys.setId(set.getInt("id"));
            boys.setBoyName(set.getString("boyName"));
            boys.setUserCP(set.getInt("userCp"));
            list.add(boys);
        }
        return list;
    }


    //查询单条信息
    public Boys querySingle(int id) throws Exception{
        Connection conn = JDBCUtilsByDruid.getConnection();
        String sql = " select * from boys where id =? ";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);


        ResultSet set = ps.executeQuery();
        Boys boys =null;
        while (set.next()){
            boys = new Boys();
            boys.setId(set.getInt("id"));
            boys.setBoyName(set.getString("boyName"));
            boys.setUserCP(set.getInt("userCp"));
        }
        return boys;
    }

}

Control(Action)

import com.miyon.dao.impl.BoysDao;
import com.miyon.entity.Boys;

import java.util.List;
import java.util.Map;

public class BoysAction {

    public void add(Boys boys) throws Exception {
        BoysDao boysDao = new BoysDao();
        boysDao.addBoys(boys);
    }

    public void update(Boys boys) throws Exception {
        BoysDao boysDao = new BoysDao();
        boysDao.updateBoys(boys);
    }

    public void delete(Boys boys) throws Exception {
        BoysDao boysDao = new BoysDao();
        boysDao.deleteBoys(boys);
    }

    public Boys querySingle(int id) throws Exception {
        BoysDao boysDao = new BoysDao();
        return boysDao.querySingle(id);
    }

    //条件查询全部
    public List<Boys> queryAllByCondition(List<Map<String,Object>> params) throws Exception {
        BoysDao boysDao = new BoysDao();
        return boysDao.queryAll(params);
    }

    //查询全部
    public List<Boys> queryAll() throws Exception {
        BoysDao boysDao = new BoysDao();
        return boysDao.queryBoys();
    }

}

View(逻辑跳转)

登录界面

import com.miyon.dao.impl.AdminDaoImpl;
import com.miyon.entity.Admin;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.util.ResourceBundle;

public class LoginFrame {
    public static void main(String[] args) {
        LoginFrame loginFrame = new LoginFrame();
        loginFrame.LoginGui();
    }
    // 初始化字体
    Font d = new Font("楷体", Font.BOLD, 36);
    Font f = new Font("楷体", Font.BOLD, 18);
    // 初始化对象
    JFrame logingui = new JFrame("仓库管理系统");
    JLabel adminlogin = new JLabel("管理员登录");
    JLabel username = new JLabel("用户名:");
    JLabel password = new JLabel("密 码:");
    JTextField name = new JTextField();
    JTextField pwd = new JPasswordField();
//    JComboBox box = new JComboBox(new String[] { "管理员", "普通用户" });
    JButton login = new JButton("登录");
    JButton reset = new JButton("重置");
    // Image icon = Toolkit.getDefaultToolkit().getImage("src\\1.jpg");

    // 给Admin类初始化对象admin
    Admin admin = new Admin();

    public void LoginGui(){
        //框架大小、坐标
        logingui.setBounds(600,300,700,500);
        logingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        logingui.setLayout(null);

        adminlogin.setBounds(240,50,220,50);
        adminlogin.setFont(d);
        username.setBounds(110,150,120,50);
        username.setFont(f);
        password.setBounds(110,210,120,50);
        password.setFont(f);
        name.setBounds(250,150,300,50);
        name.setFont(f);
        pwd.setBounds(250,210,300,50);
        pwd.setFont(f);
        login.setBounds(230,350,150,60);
        login.setFont(f);
        reset.setBounds(415,350,150,60);
        reset.setFont(f);

        logingui.add(adminlogin);
        logingui.add(username);
        logingui.add(password);
        logingui.add(name);
        logingui.add(pwd);
        logingui.add(login);
        logingui.add(reset);

        logingui.setVisible(true);

        logingui.setResizable(false);

        logingui.setLocationRelativeTo(null);

        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name_text = name.getText();
                String pwd_text = pwd.getText();
                admin.setUsername(name_text);
                admin.setPassword(pwd_text);

                AdminDaoImpl ad = new AdminDaoImpl();
                int i = ad.login(name_text, pwd_text);
                if(i==1){
                    JOptionPane.showMessageDialog(null,"登录成功!");
                    logingui.setVisible(false);
                    new MainFrame();
                }
                if(i==0){
                    JOptionPane.showMessageDialog(null,"登录失败,检查用户名或密码");
                }

            }
        });

        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                resetValueActionPerformed(arg0);
            }
        });
    }

    /**
     * 重置事件
     *
     * @param arg0
     */
    public void resetValueActionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        this.resetValue();
    }

    /**
     * 重置登录输入事件
     */
    private void resetValue(){
        this.name.setText("");
        this.pwd.setText("");
    }

    /**
     * @author unknown
     */
    public static class MainFrame extends JFrame {

        public MainFrame() {
            initComponents();
        }



        private void okButtonActionPerformed(ActionEvent e) {
            // TODO add your code here

        }

        private void addGoodsActionPerformed(ActionEvent e) throws PropertyVetoException {
            desktopPane1.add(new AddGoodsInnerFrame());
        }

        private void exitSystemActionPerformed(ActionEvent e) {
            int n = JOptionPane.showConfirmDialog(null,"确定离开?");
            if(n==0){
                System.exit( 0);
                return;
            }
        }

        public static void main(String[] args) {
            new MainFrame().setVisible(true);
        }

        private void desktopPane1PropertyChange(PropertyChangeEvent e) {
            // TODO add your code here
        }





        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            // Generated using JFormDesigner Evaluation license - qwer
            ResourceBundle bundle = ResourceBundle.getBundle("com.miyon.jform");
            dialogPane = new JPanel();
            contentPanel = new JPanel();
            menuBar1 = new JMenuBar();
            menu1 = new JMenu();
            menuItem1 = new JMenuItem();
            menuItem2 = new JMenuItem();
            menuItem3 = new JMenuItem();
            menu2 = new JMenu();
            menuItem4 = new JMenuItem();
            menuItem5 = new JMenuItem();
            menuItem6 = new JMenuItem();
            menu3 = new JMenu();
            menuItem7 = new JMenuItem();
            panel1 = new JPanel();
            desktopPane1 = new JDesktopPane();

            //======== this ========
            setTitle(bundle.getString("this.title"));
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setResizable(false);
            setName("\u8d27\u4ed3\u7ba1\u7406\u7cfb\u7edf\u4e3b\u9875\u9762");
            setVisible(true);
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());

            //======== dialogPane ========
            {
                dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
                dialogPane.setPreferredSize(new Dimension(1024, 580));
                dialogPane.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder
                . CENTER ,javax . swing. border .TitledBorder . BOTTOM, new Font ( "Dia\u006cog", Font. BOLD ,12 ) , Color .red ) ,dialogPane. getBorder () ) )
                ; dialogPane. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (PropertyChangeEvent e
                ) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } )
                ;
                dialogPane.setLayout(new BorderLayout());

                //======== contentPanel ========
                {
                    contentPanel.setPreferredSize(new Dimension(1000, 50));
                    contentPanel.setMaximumSize(null);
                    contentPanel.setMinimumSize(null);
                    contentPanel.setName(bundle.getString("this.title"));
                    contentPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

                    //======== menuBar1 ========
                    {
                        menuBar1.setPreferredSize(new Dimension(300, 24));

                        //======== menu1 ========
                        {
                            menu1.setText(bundle.getString("menu1.text"));
                            menu1.setPreferredSize(new Dimension(100, 21));

                            //---- menuItem1 ----
                            menuItem1.setText("\u6dfb\u52a0\u8d27\u54c1");
                            menuItem1.setPreferredSize(new Dimension(100, 21));
                            menuItem1.addActionListener(e -> {
                                try {
                                    addGoodsActionPerformed(e);
                                } catch (PropertyVetoException propertyVetoException) {
                                    propertyVetoException.printStackTrace();
                                }
                            });
                            menu1.add(menuItem1);

                            //---- menuItem2 ----
                            menuItem2.setText("\u66f4\u65b0\u8d27\u54c1");
                            menu1.add(menuItem2);

                            //---- menuItem3 ----
                            menuItem3.setText("\u67e5\u8be2\u8d27\u54c1");
                            menu1.add(menuItem3);
                        }
                        menuBar1.add(menu1);

                        //======== menu2 ========
                        {
                            menu2.setText("\u4ed3\u5e93\u7ba1\u7406");
                            menu2.setPreferredSize(new Dimension(100, 21));

                            //---- menuItem4 ----
                            menuItem4.setText("\u6dfb\u52a0\u4ed3\u5e93");
                            menuItem4.setPreferredSize(new Dimension(100, 21));
                            menu2.add(menuItem4);

                            //---- menuItem5 ----
                            menuItem5.setText("\u66f4\u65b0\u4ed3\u5e93");
                            menu2.add(menuItem5);

                            //---- menuItem6 ----
                            menuItem6.setText("\u67e5\u627e\u4ed3\u5e93");
                            menu2.add(menuItem6);
                        }
                        menuBar1.add(menu2);

                        //======== menu3 ========
                        {
                            menu3.setText("\u5b89\u5168\u9000\u51fa");
                            menu3.setPreferredSize(new Dimension(100, 21));

                            //---- menuItem7 ----
                            menuItem7.setText("\u9000\u51fa");
                            menuItem7.setPreferredSize(new Dimension(100, 21));
                            menuItem7.addActionListener(e -> exitSystemActionPerformed(e));
                            menu3.add(menuItem7);
                        }
                        menuBar1.add(menu3);
                    }
                    contentPanel.add(menuBar1);
                }
                dialogPane.add(contentPanel, BorderLayout.NORTH);

                //======== panel1 ========
                {
                    panel1.setPreferredSize(new Dimension(1000, 500));
                    panel1.setLayout(new BorderLayout());

                    //======== desktopPane1 ========
                    {
                        desktopPane1.setPreferredSize(new Dimension(1000, 450));
                        desktopPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
                        desktopPane1.addPropertyChangeListener(e -> desktopPane1PropertyChange(e));
                    }
                    panel1.add(desktopPane1, BorderLayout.NORTH);
                }
                dialogPane.add(panel1, BorderLayout.CENTER);
            }
            contentPane.add(dialogPane, BorderLayout.NORTH);
            pack();
            setLocationRelativeTo(getOwner());
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        // Generated using JFormDesigner Evaluation license - qwer
        private JPanel dialogPane;
        private JPanel contentPanel;
        private JMenuBar menuBar1;
        private JMenu menu1;
        private JMenuItem menuItem1;
        private JMenuItem menuItem2;
        private JMenuItem menuItem3;
        private JMenu menu2;
        private JMenuItem menuItem4;
        private JMenuItem menuItem5;
        private JMenuItem menuItem6;
        private JMenu menu3;
        private JMenuItem menuItem7;
        private JPanel panel1;
        private JDesktopPane desktopPane1;
        // JFormDesigner - End of variables declaration  //GEN-END:variables
    }
}

utils

创建连接、关闭连接、其他工具…

public class InputUtil {
    /**
     * 判断用户输入是否为空
     */

    //输入为空
    public static boolean isEmpty(String str){
        if(str==null || "".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }

    //输入不为空
    public static boolean isNotEmpty(String str){
        if(str!=null && !"".equals(str.trim())){
            return true;
        }else{
            return false;
        }
    }

}

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * 封装JDBCUtils
 * 通过Druid数据库连接池获取连接对象
 */
public class JDBCUtilsByDruid {
    static DataSource ds;
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("warehousemanagement\\src\\druid.properties"));

            //1.创建一个指定参数的数据库连接池
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //创建连接
    public static Connection getConnection() throws Exception{
        //2.从数据库连接池中获取可用的连接对象
//        Connection connection = ds.getConnection();
        return ds.getConnection();
    }

/**
 * 功能:释放资源
 *
 * @param set
 * @param statement
 * @param connection
 * @throws Exception
 */
    //关闭连接
    public static void close(ResultSet set, PreparedStatement statement, Connection connection) throws Exception {
        if (set != null) {
            set.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-28 09:38:55  更:2021-08-28 09:40:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/17 22:46:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码