欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot新闻管理条件查询分页展示+新增+编辑

程序员文章站 2022-04-01 10:49:58
commoditytypeDaoImpl实现类,实现接口方法@Overridepublic Commoditytype getCommoditytypeById(String ct_id) {Commoditytype ct = null;try {Connection conn = DBHelper.getConnection();String sql = "select * from commoditytype where ct_id=?";List param...

commoditytypeDaoImpl实现类,实现接口方法

@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

CommoditytypeController控制层传递信息

public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
}

CommodityDaoImpl实现类实现接口的方法

@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

CommodityController控制层

public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
}

CommoditytypeDaoImpl实现类

public class CommoditytypeDaoImpl implements CommoditytypeDao{

	@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

	@Override
	public List<Commoditytype> getCommoditytypeList() {
		List<Commoditytype> cts = new ArrayList<>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commoditytype ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
				cts.add(ct);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return cts;
	}
}

CommoditytypeController 控制层

public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
	
	public List<Commoditytype> getCommoditytypeList(){
		return commoditytypeDao.getCommoditytypeList();
	}
}

CommodityDaoImpl实现类

public class CommodityDaoImpl implements CommodityDao {

	@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

	@Override
	public int istCommodity(Commodity c) {
		int line = 0;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "insert into commodity(c_id,c_name,c_madein,c_type,c_inprice,c_outprice,c_num) values (?,?,?,?,?,?,?)";
			List param = new ArrayList();
			param.add(c.getC_id());
			param.add(c.getC_name());
			param.add(c.getC_madein());
			param.add(c.getC_type());
			param.add(c.getC_inprice());
			param.add(c.getC_outprice());
			param.add(c.getC_num());
			
			line = DBHelper.executeUpdate(conn, sql, param);
			
			DBHelper.closeConnection(conn);
		}catch(Exception  e) {
			e.printStackTrace();
		}
		return line;
	}

	@Override
	public int delCommodityById(String c_id) {
		int line = 0;
		Connection conn = DBHelper.getConnection();
		String sql = "delete from commodity where c_id=?";
		List param = new ArrayList();
		param.add(c_id);
		line = DBHelper.executeUpdate(conn, sql, param);
		DBHelper.closeConnection(conn);
		return line;
	}
	
}

CommodityController 控制层

public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
	
	public int istCommodity(String c_name,String c_madein,String c_type,Integer c_inprice,Integer c_outprice,Integer c_num) {
		Commodity c = new Commodity(System.currentTimeMillis()+"",c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		return commodityDao.istCommodity(c);
	}
	
	public int delCommodityById(String c_id) {
		return commodityDao.delCommodityById(c_id);
	}
}

Servlet(ist\del)

public class IstCommodityServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_name = req.getParameter("c_name");
		String c_madein = req.getParameter("c_madein");
		String c_type = req.getParameter("c_type");
		Integer c_inprice = new Integer(req.getParameter("c_inprice"));
		Integer c_outprice = new Integer(req.getParameter("c_outprice"));
		Integer c_num = new Integer(req.getParameter("c_num"));
		
		int line = new CommodityController().istCommodity(c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
public class DeleteCommodityById extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_id = req.getParameter("c_id");
		
		int line = new CommodityController().delCommodityById(c_id);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

效果展示
spring boot新闻管理条件查询分页展示+新增+编辑
spring boot新闻管理条件查询分页展示+新增+编辑
spring boot新闻管理条件查询分页展示+新增+编辑

本文地址:https://blog.csdn.net/jigsaw6213/article/details/107675919