MVC案例一

这篇博客详细介绍了如何运用MVC模式,通过jsp页面实现数据库的增删改查功能。首先创建Emp实体类,接着实现DAO层,利用DBUtils和数据库连接池进行数据交互。然后是Service层,主要负责调用DAO层方法并处理业务逻辑。前端交互部分,通过Servlet将数据传递给jsp页面,实现数据展示和操作。添加、修改和删除操作都有明确的步骤说明。
摘要由CSDN通过智能技术生成

通过jsp页面,进行对数据库的增删改查

表结构:
在这里插入图片描述

1.根据表结构创建实体类Emp:

public class Emp {
   
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private BigDecimal sal;
    private BigDecimal comm;
    private Integer deptno;

    public Emp() {
   
    }

    public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, BigDecimal sal, BigDecimal comm, Integer deptno) {
   
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }

    public Integer getEmpno() {
   
        return empno;
    }

    public void setEmpno(Integer empno) {
   
        this.empno = empno;
    }

    public String getEname() {
   
        return ename;
    }

    public void setEname(String ename) {
   
        this.ename = ename;
    }

    public String getJob() {
   
        return job;
    }

    public void setJob(String job) {
   
        this.job = job;
    }

    public Integer getMgr() {
   
        return mgr;
    }

    public void setMgr(Integer mgr) {
   
        this.mgr = mgr;
    }

    public Date getHiredate() {
   
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
   
        this.hiredate = hiredate;
    }

    public BigDecimal getSal() {
   
        return sal;
    }

    public void setSal(BigDecimal sal) {
   
        this.sal = sal;
    }

    public BigDecimal getComm() {
   
        return comm;
    }

    public void setComm(BigDecimal comm) {
   
        this.comm = comm;
    }

    public Integer getDeptno() {
   
        return deptno;
    }

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

    @Override
    public String toString() {
   
        return "Emp{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate=" + hiredate +
                ", sal=" + sal +
                ", comm=" + comm +
                ", deptno=" + deptno +
                '}';
    }
}

2.写完实体类,我们开始写dao层(与数据库进行交互)

接口

public interface EmpDao {
   
    List<Emp> findAll();
    Emp findByEmpno(Integer empno);
    void add(Emp emp);
    void update(Emp emp);
    void delete(Integer empno);
}

在完成增删改查功能之前,需要先获取到连接(从连接池中)
如果不懂,可以点开以下链接
DBUtils工具类的使用
数据库连接池

public class DataSourceUtils {
   

    private static DruidDataSource dataSource;

    static {
   
        try {
   
            Properties properties = new Properties();
            InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("database.properties");
            properties.load(is);

            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
    }

    public static DataSource getDataSourse(){
   
        return dataSource;
    }
}

然后创建接口的实现类:完成增删改查功能

public class EmpDaoImpl implements EmpDao {
   
    @Override
    public List<Emp> findAll() {
   
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSourse());
        try {
   
            return qr.query("select * from emp", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException e) {
   
            e.printStackTrace();
            throw new RuntimeException("查找失败", e);
        }
    }

    @Override
    public Emp findByEmpno(Integer empno) {
   
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSourse());
        try {
   
            return qr.query("select * from emp where empno=?", new BeanHandler<Emp>(Emp.class), empno);
        } catch (SQLException e) {
   
            e.printStackTrace();
            throw new RuntimeException("查询失败", e);
        }
    }

    @Override</
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值