DAO模式

目录

 一、DAO里的组成

二、实体类

三、DAO接口

四、DAO实现

测试方法

         使用DAO模式开发基于控制台的记账本系统

        实体类

        DAO接口

        DAO实现

        控制台界面


概念:DAO(Data  Access Object)数据访问对象,就是应用里专注于数据访问的那些对象,这是业界一个通用的规范。DAO也是分层里的一个层

 一、DAO里的组成

  • 数据访问工具类
  • 实体类
  • DAO接口:对这个表的增删改查
  • DAO实现:DAO接口的实现

二、实体类

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/27 15:34
 * @Version 1.0
 */
package com.wjk.entity;

public class Dept {
    private int deptno;
    private String dname;
    private String loc;

    public Dept() {
    }

    public Dept(int deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

三、DAO接口

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/27 15:35
 * @Version 1.0
 */
package com.wjk.dao;

import com.wjk.entity.Dept;

import java.util.List;

public interface DeptDao {
    /**
     * 添加
     * @param dept
     * @return
     */
    int insert(Dept dept);

    /**
     * 删除
     * @param deptno
     * @return
     */
    int delete(int deptno);

    /**
     * 修改
     * @param dept
     * @return
     */
    int update(Dept dept);

    /**
     * 查询单个
     * @param deptno
     * @return
     */
    Dept selectOne(int deptno) throws Exception;

    /**
     * 查询所有
     * @return
     */
    List<Dept> selectAll();
}

四、DAO实现

public class DeptDaoImpl implements DeptDao {
    @Override
    public int saveDept(Dept dept) {
        return JdbcUtil.update("insert into dept(deptno,loc,dname) values (?,?,?)", dept.getDeptno(),dept.getLoc(),dept.getDname());
    }

    @Override
    public int deleteByDeptno(int deptno) {
        return JdbcUtil.update("delete from dept where deptno=?", deptno);
    }

    @Override
    public int updateDept(Dept dept) {
        return JdbcUtil.update("update dept set dname=?,loc=? where deptno=?", dept.getDname(),dept.getLoc(),dept.getDeptno());
    }

    @Override
    public Dept selectByDeptno(int deptno) {
        Map<String, Object> map = JdbcUtil.queryOneForMap("select * from dept where deptno=?", deptno);
        Dept dept = new Dept();
        dept.setDeptno((Integer) map.get("deptno"));
        dept.setDname((String) map.get("dname"));
        dept.setLoc((String) map.get("loc"));
        return dept;
    }

     @Override
    public List<Dept> selectList() {
        List<Map<String, Object>> maps = JdbcUtil.queryForMapList("select * from dept");
        List<Dept> list = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Dept dept = new Dept();
            dept.setDeptno((Integer) map.get("deptno"));
            dept.setDname((String) map.get("dname"));
            dept.setLoc((String) map.get("loc"));
            list.add(dept);
        }
        return list;
    }
}

测试方法

/**
 * @author Andy
 * @create 2022/8/27
 */
public class DeptDaoTest {
    //创建DAO
    private DeptDao deptDao = new DeptDaoImpl();
    @Test
    public void testSaveDept(){
        //1.测试添加
        Dept dept = new Dept()  ;
        dept.setLoc("北京");
        dept.setDname("企划部");
        dept.setDeptno(30);

        int row = deptDao.saveDept(dept);
        System.out.println("row = " + row);
    }
    @Test
    public void testDeleteByDeptno(){
        int row = deptDao.deleteByDeptno(30);
        System.out.println("row = " + row);
    }

    @Test
    public void testUpdateDept(){
        Dept dept = new Dept()  ;
        dept.setLoc("郑州");
        dept.setDname("企划部");
        dept.setDeptno(10);
        int row = deptDao.updateDept(dept);
        System.out.println("row = " + row);
    }

    @Test
    public void testSelectByDeptno(){
        Dept dept = deptDao.selectByDeptno(20);
        System.out.println("dept = " + dept);
    }

    @Test
    public void testSelectList(){
        List<Dept> depts = deptDao.selectList();
        for (Dept dept : depts) {
            System.out.println("dept = " + dept);
        }
    }
}

         使用DAO模式开发基于控制台的记账本系统

        功能要求:注册、登录、账单查询、账单添加

        1.需求分析:公司有需求分析人员去做需求分析:可能去找客户的相关文件、找相关的人员了解实际工作用的什么?怎么用?需要达到一个什么结果。有了就可以写需求分析:分析有什么功能、每个功能里面的输入输出项。可能会出也给原型。

        2.项目设计:设计数据库,设计基础类库

        3.开发实现

        4.测试      

        5.上线

        6.维护

        实体类

        User的实体类

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/29 15:14
 * @Version 1.0
 */
package com.wjk.entity;

public class User {
    private int uid;
    private String email;
    private String nikeName;
    private String password;

    public User() {
    }

    public User(int uid, String email, String nikeName, String password) {
        this.uid = uid;
        this.email = email;
        this.nikeName = nikeName;
        this.password = password;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNikeName() {
        return nikeName;
    }

    public void setNikeName(String nikeName) {
        this.nikeName = nikeName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", email='" + email + '\'' +
                ", nikeName='" + nikeName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

         Catgory的实体类

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/29 15:15
 * @Version 1.0
 */
package com.wjk.entity;

public class Category {
    private int cid;
    private String name;
    private String pic;
    private int type;

    public Category() {
    }

    public Category(int cid, String name, String pic, int type) {
        this.cid = cid;
        this.name = name;
        this.pic = pic;
        this.type = type;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Category{" +
                "cid=" + cid +
                ", name='" + name + '\'' +
                ", pic='" + pic + '\'' +
                ", type=" + type +
                '}';
    }
}

         AccountBook的实体类

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/29 15:15
 * @Version 1.0
 */
package com.wjk.entity;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

public class AccountBook {
    private int abid;
    private int categoryId;
    private String details;
    private Date createDatetime;
    private BigDecimal money;
    private int userid;

    //关联
    private User user;
    private Category category;

    public AccountBook() {
    }

    public AccountBook(int abid, int categoryId, String details, Date createDatetime, BigDecimal money, int userid, User user, Category category) {
        this.abid = abid;
        this.categoryId = categoryId;
        this.details = details;
        this.createDatetime = createDatetime;
        this.money = money;
        this.userid = userid;
        this.user = user;
        this.category = category;
    }

    public int getAbid() {
        return abid;
    }

    public void setAbid(int abid) {
        this.abid = abid;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public Date getCreateDatetime() {
        return createDatetime;
    }

    public void setCreateDatetime(Date createDatetime) {
        this.createDatetime = createDatetime;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "AccountBook{" +
                "abid=" + abid +
                ", categoryId=" + categoryId +
                ", details='" + details + '\'' +
                ", createDatetime=" + createDatetime +
                ", money=" + money +
                ", userid=" + userid +
                ", user=" + user +
                ", category=" + category +
                '}';
    }
}

        DAO接口

用户DAO的接口

public interface UserDao {

    //注册
    int saveUser(User user);

    //登录
    /**
     * 根据账号和密码查询一个用户信息
     * @param email 账号
     * @param password 密码
     * @return 查询到对象,如果账号密码不匹配返回null
     */
    User queryByEmailAndPassword(String email,String password);

}

 类别DAO的接口

public interface CategoryDao {
    List<Category> queryAll();
}

 账单DAO的接口

public interface AccountBookDao {

    /**
     * 查询指定用户的帐单
     * @param uid 用户id
     * @return 帐单集合
     */
    List<AccountBook> queryAccountBook(int uid);

    int saveAccountBook(AccountBook accountBook);
}

        DAO实现

         userDAO的实现

public class UserDaoImpl implements UserDao {
    @Override
    public int saveUser(User user) {
        return JdbcUtil.update("insert into t_user values(?,?,?,?)", null,user.getEmail(),user.getNikename(),user.getPassword());
    }

    @Override
    public User queryByEmailAndPassword(String email, String password) {
        Map<String, Object> map = JdbcUtil.queryOneForMap("select * from t_user where email=? and password=?", email, password);
        //账号密码是否匹配
        User user = null;
        //有一行记录,账号和密码匹配
        if(map.size() > 0){
            user = new User();
            //放什么属性值,根据业务来,因为后面功能要使用这个登录用户的uid
            user.setUid((Integer) map.get("uid"));
            user.setNikename((String) map.get("nikename"));
        }
        return user;
    }
}

        测试

public class UserDaoTest {
    private UserDao userDao = new UserDaoImpl();

    @Test
    public void testSaveUser(){
        User user = new User();
        user.setNikename("冰冰");
        user.setEmail("bb123@qq.com");
        user.setPassword("000000");

        int row = userDao.saveUser(user);
        System.out.println("row = " + row);
    }

    @Test
    public void testQueryByEmailAndPassword(){
        User user = userDao.queryByEmailAndPassword("bb123@qq.com", "00000011");
        if(user == null){
            System.out.println("账号或密码错误");
        } else {
            System.out.println("欢迎:"+user.getNikename());
        }
    }
}

        categoryDAO的实现

public class CategoryDaoImpl implements CategoryDao {
    @Override
    public List<Category> queryAll() {
        List<Category> list = new ArrayList<>();
        List<Map<String, Object>> maps = JdbcUtil.queryForMapList("select * from t_category");
        for (Map<String, Object> map : maps) {
            Category c = new Category();
            c.setCid((Integer) map.get("cid"));
            c.setName((String) map.get("name"));
            list.add(c);
        }
        return list;
    }
}

        AccountBookDAO的实现

public class AccountBookDaoImpl implements AccountBookDao {
    @Override
    public List<AccountBook> queryAccountBook(int uid) {
        List<AccountBook> list = new ArrayList<>();
        List<Map<String, Object>> maps = JdbcUtil.queryForMapList("select * from t_account_book ab join t_category c on ab.category_id=c.cid where user_id=?", uid);
        for (Map<String, Object> map : maps) {
            AccountBook ab = new AccountBook();
            ab.setCreateDatetime((Date) map.get("create_datetime"));
            ab.setDetails((String) map.get("details"));
            ab.setMoney((BigDecimal) map.get("money"));

            Category c = new Category();
            c.setName((String) map.get("name"));
            c.setType((Integer) map.get("type"));
            ab.setCategory(c);

            list.add(ab);
        }
        return list;
    }

    @Override
    public int saveAccountBook(AccountBook accountBook) {
        return JdbcUtil.update("insert into t_account_book(details,money,create_datetime,user_id,category_id) values (?,?,?,?,?)", accountBook.getDetails(),accountBook.getMoney(),accountBook.getCreateDatetime(),accountBook.getUser().getUid(),accountBook.getCategory().getCid());
    }
}

        控制台界面

/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2022/8/29 18:53
 * @Version 1.0
 */
package com.wjk;

import com.wjk.dao.AccountBookDao;
import com.wjk.dao.Impl.AccountBookDaoImpl;
import com.wjk.dao.Impl.UserDaoImpl;
import com.wjk.dao.UserDao;
import com.wjk.entity.AccountBook;
import com.wjk.entity.User;

import java.util.ArrayList;
import java.util.Scanner;

public class Client {
    static UserDao userDao=new UserDaoImpl();
    static AccountBookDao accountBookDao=new AccountBookDaoImpl();
    static Scanner sc = new Scanner(System.in);
    static User loginUser = null;

    public static void main(String[] args) {
        System.out.println("*************************欢迎来到王老板收银台**********************************");
        System.out.println("请选择: 1.注册  2.登录");
        //用户选择功能
        int aop =sc.nextInt();
        switch (aop){
            case 1:
                int rows=reg();
                if (rows>0){
                    System.out.println("注册成功,请登录");
                    //登陆页面
                    loginUser=login();
                    if (loginUser==null){
                        System.err.println("登陆失败,账号或者密码错误");
                        loginUser=login();
                    }else {
                        //到主界面
                        System.out.println("主界面:欢迎"+loginUser.getNikeName());
                        index();
                    }
                }else {
                    reg();
                }
                break;
            case 2:
                loginUser=login();
                if (loginUser==null){
                    System.out.println("登陆失败,账号或密码错误");
                    loginUser=login();
                }else {
                    //到了主界面
                    System.out.println("欢迎:"+loginUser.getNikeName());
                    index();
                }
                break;
        }
    }


    public static void index(){
        ArrayList<AccountBook> accountBooks  = accountBookDao.queryAccountBook(loginUser.getUid());
        System.out.println("金额\t\t详情\t\t发生时间\t\t\t\t类别");
        for (AccountBook accountBook : accountBooks) {
            System.out.println(accountBook.getMoney()+"\t");
            System.out.println(accountBook.getDetails()+"\t");
            System.out.println(accountBook.getCreateDatetime()+"\t");
            System.out.println(accountBook.getCategory().getName());
            System.out.println();
        }
        System.out.println("请选择添加账单: 1.支出 2.收入");
        int aop=sc.nextInt();
    }




    public static int reg(){
        System.out.println("请输入邮箱:");
        String email = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        System.out.println("请输入昵称");
        String nikename=sc.next();

        User user = new User();
        user.setPassword(password);
        user.setNikeName(nikename);
        user.setEmail(email);
        int rows = userDao.savaUser(user);
        return rows;
    }

    public static User login(){
        System.out.println("请输入邮箱:");
        String email=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        return userDao.queryByEmailAndPassword(email,password);
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值