zoukankan      html  css  js  c++  java
  • DAO模式

    JDBC封装

     为什么进行JDBC封装

      

      

     实现JDBC封装

      

      

      

      

      

      

      

     DAO模式的组成

      

      

    package cn.jbit.epet.entity;
    
    import java.util.Date;
    
    public class Pet {
        private int id; // 宠物id
        private int masterId; // 主人id
        private String name; // 昵称
        private int typeId; // 类型id
        private int health; // 健康值
        private int love; // 亲密度
        private Date adoptTime; // 领养时间
        private String status; // 状态
    
        public Pet() {
        }
    
        public Pet(int id, int masterId, String name, int typeId, int health,
                int love, Date adoptTime, String status) {
            super();
            this.id = id;
            this.masterId = masterId;
            this.name = name;
            this.typeId = typeId;
            this.health = health;
            this.love = love;
            this.adoptTime = adoptTime;
            this.status = status;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getMasterId() {
            return masterId;
        }
    
        public void setMasterId(int masterId) {
            this.masterId = masterId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getTypeId() {
            return typeId;
        }
    
        public void setTypeId(int typeId) {
            this.typeId = typeId;
        }
    
        public int getHealth() {
            return health;
        }
    
        public void setHealth(int health) {
            this.health = health;
        }
    
        public int getLove() {
            return love;
        }
    
        public void setLove(int love) {
            this.love = love;
        }
    
        public Date getAdoptTime() {
            return adoptTime;
        }
    
        public void setAdoptTime(Date adoptTime) {
            this.adoptTime = adoptTime;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    实体类
    package cn.jbit.epet.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class BaseDao {
        private String driver = "com.mysql.jdbc.Driver"; // 数据库驱动字符串
        private String url = "jdbc:mysql://localhost:3306/epet"; // 数据库连接字符串
        private String user = "epetadmin"; // 数据库用户名
        private String password = "0000"; // 数据库密码
        Connection conn = null;
    
        // 1.打开数据库
        public Connection getConnection() {
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                conn = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        // 2.关闭所有资源
        public void closeAll(Connection conn, PreparedStatement stmt, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        // 3.增、删、改通用方法
        public int executeUpdate(String sql, Object[] param) {
            int num = 0; // 影响行数
            conn = this.getConnection();
            PreparedStatement stmt = null;
            try {
                stmt = conn.prepareStatement(sql);
                // 为参数赋值
                if (param != null) {
                    for (int i = 0; i < param.length; i++) {
                        stmt.setObject(i + 1, param[i]);
                    }
                }
                // 执行sql语句
                num = stmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                this.closeAll(conn, stmt, null);
            }
            return num;
        }
    }
    数据库工具类
    package cn.jbit.epet.dao;
    
    import java.util.List;
    
    import cn.jbit.epet.entity.Pet;
    
    public interface PetDao {
        int save(Pet pet);
    
        int del(Pet pet);
    
        int update(Pet pet);
    
        Pet getByName(String name);
    
        List<Pet> findByName(String name);
    
        List<Pet> findByType(String type);
    }
    DAO接口
    package cn.jbit.epet.dao.impl;
    
    import java.util.List;
    
    import cn.jbit.epet.dao.BaseDao;
    import cn.jbit.epet.dao.PetDao;
    import cn.jbit.epet.entity.Pet;
    
    public class PetDaoMySQLImpl extends BaseDao implements PetDao {
    
        @Override
        public int save(Pet pet) {
            return 0;
        }
    
        @Override
        public int del(Pet pet) {
            return 0;
        }
    
        @Override
        public int update(Pet pet) {
            String sql = "update dog set status=0 where id=?";
            Object[] param = { pet.getId() };
            int result = this.executeUpdate(sql, param);
            return result;
        }
    
        @Override
        public Pet getByName(String name) {
            return null;
        }
    
        @Override
        public List<Pet> findByName(String name) {
            return null;
        }
    
        @Override
        public List<Pet> findByType(String type) {
            return null;
        }
    
    }
    DAO实现类
    package cn.jbit.epet.manager;
    
    import cn.jbit.epet.dao.PetDao;
    import cn.jbit.epet.dao.impl.PetDaoMySQLImpl;
    import cn.jbit.epet.entity.Pet;
    
    public class Test {
        public static void main(String[] args) {
            PetDao petDao = new PetDaoMySQLImpl();
            Pet pet = new Pet();
            pet.setId(1);
            int result = petDao.update(pet);
            if (result > 0) {
                System.out.println("狗狗信息更新成功!");
            } else {
                System.out.println("狗狗信息更新失败!");
            }
        }
    }
    测试类

    Properties类

      

      

      

  • 相关阅读:
    Java 概述
    vue组件事件
    小程序注册
    小程序基础知识梳理
    小程序简介
    公众号
    jeecg-boot
    小程序背景图
    bootstrap-select采坑
    存取cookie
  • 原文地址:https://www.cnblogs.com/xhddbky/p/9245498.html
Copyright © 2011-2022 走看看