栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自定义MVC增删改查

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义MVC增删改查

 


目录

1.将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导入进去

2.将分页标签相关文件、及相关助手类导入

3.框架的配置文件添加、以及web.xml的配置-以后开源框架的使用从这一步开始

4.完成Book实体类及bookDao的编写

5.完成通用的增删改方法

6.完成BookAction

7.完成mvc.xml的配置

8.完成前台代码的编写

9.总结


1.将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导入进去。

                        

                 

                 

    

                 

  

                                

 2.将分页标签相关文件、及相关助手类导入  3.框架的配置文件添加、以及web.xml的配置-以后开源框架的使用从这一步开始

                                        

                                         

4.完成Book实体类及bookDao的编写
package com.ycx.entity;

public class Book {
	private int bid;
	private String bname;
	private float price;
	public Book() {
		// TODO Auto-generated constructor stub
	}
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
	
}
package com.ycx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ycx.entity.Book;
import com.ycx.util.BaseDao;
import com.ycx.util.DBAccess;
import com.ycx.util.PageBean;
import com.ycx.util.StringUtils;

public class BookDao extends BaseDao{
//	查询
	public List list(Book book,PageBean pageBean) throws Exception{
		String sql="select * from t_mvc_book where 1=1";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		int bid = book.getBid();
//		前台jsp传递到后台,只要传了就有值,没穿就是默认值,默认值就是0
		if(bid!=0) {
			sql+=" and bid = "+bid;
		}
		return super.executeQuery(sql, pageBean, rs ->{
			List list=new ArrayList<>();
			try {
				while(rs.next()) {
					list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		});
	}
	
//	 增
	public int add(Book book) throws Exception {
		Connection con = DBAccess.getConnection();
		String sql="insert into t_mvc_book values (?,?,?)";
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setObject(1, book.getBid());
		pst.setObject(2, book.getBname());
		pst.setObject(3, book.getPrice());
		return pst.executeUpdate();
	}
//	 删
	public int del(Book book) throws Exception {
		Connection con = DBAccess.getConnection();
		String sql="delete from t_mvc_book where bid= ?";
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setObject(1, book.getBid());
		return pst.executeUpdate();
	}
	
	
//	 改
	public int edit(Book book) throws Exception {
		Connection con = DBAccess.getConnection();
		String sql="update t_mvc_book set bname=?,price=?, where bid=?";
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setObject(3, book.getBid());
		pst.setObject(1, book.getBname());
		pst.setObject(2, book.getPrice());
		return pst.executeUpdate();
	}
	
	
}
然后用junit4去测试:

ctrl+n

 

 

 之后就自动生成了一个BookDaoTest:

package com.ycx.dao;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ycx.entity.Book;

public class BookDaoTest {
	private BookDao bookDao=new BookDao();
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testList() {
		try {
			List list = bookDao.list(new Book(), null);
			for (Book book : list) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void testAdd() {
		fail("Not yet implemented");
	}

	@Test
	public void testDel() {
		fail("Not yet implemented");
	}

	@Test
	public void testEdit() {
		fail("Not yet implemented");
	}

}

测试查询所有:

打印结果如下

 

 测试分页查询:

@Test
	public void testList() {
		try {
			List list = bookDao.list(new Book(), new PageBean());
			for (Book book : list) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

结果如下:

 测试新增数据:

@Test
	public void testAdd() {
		Book book = new Book(1234321, "1234321", 1234321);
		try {
			bookDao.add(book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

结果如下:

 测试修改:

@Test
	public void testEdit() {
		Book book = new Book(1234321, "123432100", 1234321);
		try {
			bookDao.edit(book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

结果如下:

 测试删除:

	@Test
	public void testDel() {
		Book book = new Book(1234321, "1234321", 1234321);
		try {
			bookDao.del(book);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

5.完成通用的增删改方法 BaseDao:
	public int executeUpdate(String sql,T t,String [] attrs) throws Exception {
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
//		将T的某一个属性对应的值加到 pst 对象中
		for (int i = 0; i < attrs.length; i++) {
			Field f = t.getClass().getDeclaredField(attrs[i]);
			f.setAccessible(true);
			pst.setObject(i+1, f.get(t));
		}
		return pst.executeUpdate();
	}
BookDao:
package com.ycx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ycx.entity.Book;
import com.ycx.util.BaseDao;
import com.ycx.util.DBAccess;
import com.ycx.util.PageBean;
import com.ycx.util.StringUtils;

public class BookDao extends BaseDao{
//	查询
	public List list(Book book,PageBean pageBean) throws Exception{
		String sql="select * from t_mvc_book where 1=1";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		int bid = book.getBid();
//		前台jsp传递到后台,只要传了就有值,没穿就是默认值,默认值就是0
		if(bid!=0) {
			sql+=" and bid = "+bid;
		}
		return super.executeQuery(sql, pageBean, rs ->{
			List list=new ArrayList<>();
			try {
				while(rs.next()) {
					list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		});
	}
	
//	 增
	
	public int add(Book book) throws Exception {
		String sql="insert into t_mvc_book values (?,?,?)";
		return super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
	}
//	 删
	
	public int del(Book book) throws Exception {
		String sql="delete from t_mvc_book where bid= ?";
		return super.executeUpdate(sql, book, new String[] {"bid"});
	}
	
//	 改
	
	public int edit(Book book) throws Exception {
		String sql="update t_mvc_book set bname=?,price=?, where bid=?";
		return super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
	}
	
	
}

6.完成BookAction
package com.ycx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ycx.dao.BookDao;
import com.ycx.entity.Book;
import com.ycx.framework.ActionSupport;
import com.ycx.framework.ModelDriven;
import com.ycx.util.PageBean;

public class BookAction  extends ActionSupport implements ModelDriven{
	private Book book=new Book();
	private BookDao bookDao=new BookDao();
	
	
	@Override
	public Book getModel() {
		return null;
	}
//	增
	public String add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bookDao.add(book);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		toList代表跳到查询界面
		return "toList";
	}
	
//	删
	public String del(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bookDao.del(book);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		toList代表跳到查询界面
		return "toList";
	}	
//	修改	
		public String edit(HttpServletRequest req, HttpServletResponse resp) {
			try {
				bookDao.edit(book);
			} catch (Exception e) {
				e.printStackTrace();
			}
//			toList代表跳到查询界面
			return "toList";
		}
//		查询所有
		public String list(HttpServletRequest req, HttpServletResponse resp) {
			try {
				PageBean pageBean=new PageBean();
				pageBean.setRequest(req);
				List list = bookDao.list(book, pageBean);
				req.setAttribute("list", list);
				req.setAttribute("pageBean", pageBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
//			执行查询展示
			return "list";
		}	
		
//		跳转到新增或者修改界面
		public String preEdit(HttpServletRequest req, HttpServletResponse resp) {
			try {
				int bid = book.getBid();
				if(bid != 0) {
//					传递bid到后台,有且只能查出一条数据,那也就意味着list集合中只有一条
					List list = bookDao.list(book, null);
					req.setAttribute("b", list.get(0));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
//			代表跳到编辑界面
			return "toEdit";
		}	
		
		
		
		
}

7.完成mvc.xml的配置



	
		
		
		
	
	

8.完成前台代码的编写
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@taglib uri="http://ycx.com" prefix="y" %>
	<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>






书籍列表



	
书籍ID 书籍名 价格
${b.bid } ${b.bname } ${b.price }

 

 

9.总结 自定义MVC框架应用中最为关键的是要把自定义MVC框架打造好,其中在应用是也要注意细节,比如:改造.xml文件时要跳转的目的类。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/998799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号