简单mvc+三层架构实现增删改查功能

简单mvc+三层架构–>实现增删改查功能@数据库设计副产物

我的数据库系统做的是简单的外卖订餐平台~~~
耗时两天,快速实现的增删改查功能,话不多说,先上图,再贴代码

简单mvc+三层架构实现增删改查功能_第1张图片
简单mvc+三层架构实现增删改查功能_第2张图片
简单mvc+三层架构实现增删改查功能_第3张图片
简单mvc+三层架构实现增删改查功能_第4张图片
还有删除功能和修改功能截图就不好展示啦,详情b站欢迎您!!!

代码实现,太多了贴不完呀,这里只放删除的主要程序吧!修改也行O(∩_∩)O

数据库的连接

这里我使用阿里的druid连接池,后台查询等语句使用JdbcTemplate的queryForObject
写好工具包连接ConUtil,dao层实现类如下:
提示:代码中中包含复杂条件查询的处理哟,findbycondition!

package com.lcx.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.lcx.entity.Manager;
import com.lcx.entity.Menu;
import com.lcx.entity.Restaurant;
import com.lcx.utils.ConUtils;

/**
 * @author 作者 E-mail:
 * @version 创建时间:2019年12月14日 上午9:58:50 类说明
 */
public class ManagerDaoImpl implements ManagerDao {

	// 创建jdbcTemplate对象
	private JdbcTemplate template = new JdbcTemplate(ConUtils.getDataSource());

	@Override
	public List findAll() {
		String sql = "select * from restaurant";
		List restaurants = template.query(sql, new BeanPropertyRowMapper(Restaurant.class));
		return restaurants;
	}

	@Override
	public void add(Restaurant restaurant) {
		String sql = "insert into restaurant values(?,?,?,?,?,?,?,?,?,null)";
		template.update(sql, restaurant.getRest_adds(), restaurant.getRest_manager(), restaurant.getRest_tel(),
				restaurant.getFee(), restaurant.getDiscount(), restaurant.getRest_name(), restaurant.getDishid(),
				restaurant.getComments(), restaurant.getRest_range());

	}

	@Override
	public List findManager() {
		String sql = "select * from manager";
		List managers = template.query(sql, new BeanPropertyRowMapper(Manager.class));
		return managers;
	}

	@Override
	public Manager logincheck(String manager_count, String manager_psw) {
		try {
			System.out.println("执行登陆验证方法了!!!");
			String sql = "select * from manager where manager_count = ? and manager_psw = ?";
			Manager manager = template.queryForObject(sql, new BeanPropertyRowMapper(Manager.class),
					manager_count, manager_psw);
			if (manager == null) {
				System.out.println("没找到manager对象");
			}
			return manager;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void del(int id) {
		String sql = "delete from restaurant where restid = ?";
		template.update(sql, id);

	}

	@Override
	public void updateRest(Restaurant restaurant) {
		String sql = "update restaurant set rest_adds = ? , rest_manager = ? , rest_tel = ? , fee = ? , discount = ? , rest_name = ? , dishid = ? , comments = ? , rest_range = ? where restid = ?";
		template.update(sql, restaurant.getRest_adds(), restaurant.getRest_manager(), restaurant.getRest_tel(),
				restaurant.getFee(), restaurant.getDiscount(), restaurant.getRest_name(), restaurant.getDishid(),
				restaurant.getComments(), restaurant.getRest_range(), restaurant.getRestid());

	}

	@Override
	public Restaurant findById(String id) {
		String sql = "select * from restaurant where restid = ?";
		return template.queryForObject(sql, new BeanPropertyRowMapper(Restaurant.class), id);

	}

	@Override
	public List findbycondition(Map condition) {
		String sql = "select * from restaurant  where 1 = 1 ";

        StringBuilder sb = new StringBuilder(sql);
        //2.遍历map
        Set keySet = condition.keySet();
        //定义参数的集合
        List params = new ArrayList();
        for (String key : keySet) {

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//?条件的值
            }
        }

        sql = sb.toString();
        System.out.println(sql);
        System.out.println(params);

        return template.query(sql,new BeanPropertyRowMapper(Restaurant.class),params.toArray());
    }

	@Override
	public List findMenu() {
		String sql = "select * from menu";
		List menu = template.query(sql, new BeanPropertyRowMapper(Menu.class));
		return menu;
	}

	@Override
	public void delete(int id) {
		String sql = "delete from manager where managerid = ?";
		template.update(sql, id);
		
	}

	@Override
	public void delOne(int id) {
		// TODO Auto-generated method stub
		String sql = "delete from restaurant where restid = ?";
		template.update(sql, id);
		
	}

}

 
  
Servlet业务处理

我也不知道要贴那些了,真的太多了~~~
感觉身边的同学对验证码比较好奇,那我就贴验证码的吧

验证码的书写
package com.lcx.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CheckCodeServlet
 */
@WebServlet("/checkCode")
public class CheckCodeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// 服务器通知浏览器不要缓存
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setHeader("expires", "0");

		// 在内存中创建一个长80,宽30的图片,默认黑色背景
		// 参数一:长
		// 参数二:宽
		// 参数三:颜色
		int width = 80;
		int height = 30;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// 获取画笔
		Graphics g = image.getGraphics();
		// 设置画笔颜色为灰色
		g.setColor(Color.GRAY);
		// 填充图片
		g.fillRect(0, 0, width, height);

		// 产生4个随机验证码,12Ey
		String checkCode = getCheckCode();
		// 将验证码放入HttpSession中
		request.getSession().setAttribute("CHECKCODE_SERVER", checkCode);

		// 设置画笔颜色为黄色
		g.setColor(Color.YELLOW);
		// 设置字体的小大
		g.setFont(new Font("黑体", Font.BOLD, 24));
		// 向图片上写入验证码
		g.drawString(checkCode, 15, 25);

		// 将内存中的图片输出到浏览器
		// 参数一:图片对象
		// 参数二:图片的格式,如PNG,JPG,GIF
		// 参数三:图片输出到哪里去
		ImageIO.write(image, "PNG", response.getOutputStream());
	}

	/**
	 * 产生4位随机字符串
	 */
	private String getCheckCode() {
		String base = "0123456789ABCDEFGabcdefg";
		int size = base.length();
		Random r = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i <= 4; i++) {
			// 产生0到size-1的随机值
			int index = r.nextInt(size);
			// 在base字符串中获取下标为index的字符
			char c = base.charAt(index);
			// 将c放入到StringBuffer中去
			sb.append(c);
		}
		return sb.toString();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}

页面jsp显示

生成的验证码当然要放到页面上呀,验证码这东西一般都是登录的时候需要的,那当然只能是从登录界面上入手啦

 
        
登录验证也要验证输入的验证码是否正确

这我就放在了loginServlet中处理

//验证登录信息
		if (manager_count == null || "".equals(manager_count.trim())) {
			session.setAttribute("message", "账号输入有误");
			response.sendRedirect(request.getContextPath() + "/login.jsp");
			return;
		}
		
		if (manager_psw == null || "".equals(manager_psw.trim())) {
			session.setAttribute("message", "密码输入有误");
			response.sendRedirect(request.getContextPath() + "/login.jsp");
			return;
		}

到这里,验证码的功能就基本实现了,在数据库的实现中我使用的是以上的验证码操作,在我现在做的简单购物车系统中,使用了另一种方式生成验证码,欢迎持续关注我!

欢迎前往b站看我演绎视频,有意见可以给我留言哟!

本人B站链接: https://space.bilibili.com/385874624.

你可能感兴趣的:(javaWeb,java,mysql,javascript,jquery)