自定义mvc框架应用

前言

自定义链接

自定义mvc框架/xml建模https://blog.csdn.net/licmi/article/details/106305815
认识自定义mvchttps://blog.csdn.net/licmi/article/details/106498785.
自定义mvc框架https://blog.csdn.net/licmi/article/details/106225291.

mvc流程图
在这里插入图片描述

应用案例

实体类

package com.liuchunming.entity;

public class Solr {
	private String id;
	private String job;
	private String company;
	private String address;
	private String salary;
	private String url;
	private String limito;
	private String time;
	private String descc;
	private String jobHandle;
	private String addressHandle;
	
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getSalary() {
		return salary;
	}
	public void setSalary(String salary) {
		this.salary = salary;
	}
	public String getLimito() {
		return limito;
	}
	public void setLimit(String limito) {
		this.limito = limito;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public String getDescc() {
		return descc;
	}
	public void setDescc(String descc) {
		this.descc = descc;
	}
	public String getJobHandle() {
		return jobHandle;
	}
	public void setJobHandle(String jobHandle) {
		this.jobHandle = jobHandle;
	}
	public String getAddressHandle() {
		return addressHandle;
	}
	public void setAddressHandle(String addressHandle) {
		this.addressHandle = addressHandle;
	}
	
	public Solr(String id, String job, String company, String address, String salary, String url, String limito,
			String time, String descc, String jobHandle, String addressHandle) {
		super();
		this.id = id;
		this.job = job;
		this.company = company;
		this.address = address;
		this.salary = salary;
		this.url = url;
		this.limito = limito;
		this.time = time;
		this.descc = descc;
		this.jobHandle = jobHandle;
		this.addressHandle = addressHandle;
	}
	public Solr() {
		super();
	}
	@Override
	public String toString() {
		return "Solr [id=" + id + ", job=" + job + ", company=" + company + ", address=" + address + ", salary="
				+ salary + ", url=" + url + ", limito=" + limito + ", time=" + time + "]";
	}

	
}

相信大家写增删改方法总是有很多重复的代码,降低编码效率。所有想个办法写个通用的方法

	/**
	 * 通用的增删改
	 * @throws SQLException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public int executeUpdate(String sql,T t,String [] attrs) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		Connection con = DBAccess.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		int lop=1;
		Field f=null;
		for (String attr : attrs) {
			 f = t.getClass().getDeclaredField(attr);
			f.setAccessible(true);
			ps.setObject(lop++, f.get(t));
		}
		int code = ps.executeUpdate();
		DBAccess.close(con, ps, null);
		return code;
	}

实现Dao方法类

package com.liuchunming.dao;

import java.sql.SQLException;
import java.util.List;

import com.liuchunming.entity.Solr;
import com.liuchunming.util.BaseDao;
import com.liuchunming.util.PageBean;
import com.liuchunming.util.StringUtils;

public class SolrDao extends BaseDao<Solr> {
	public List<Solr> list(Solr solr,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_solr_job where true";
		String company = solr.getCompany();
		if(StringUtils.isNotBlank(company)) {
			sql+=" and company like '%"+company+"%'";
		}
		return super.executeQuery(sql, Solr.class, pageBean);
	}
	
	public List<Solr> toEdit(Solr solr,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_solr_job where true";
		String id = solr.getId();
		if(StringUtils.isNotBlank(id)) {
			sql+=" and id="+id;
		}
		return super.executeQuery(sql, Solr.class, pageBean);
	}
	public int add(Solr solr) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="insert into t_solr_job values(?,?,?,?,?,?,?,?,?,?,?)";
		return super.executeUpdate(sql, solr, new String[]{"id","job","company","address","salary","url","limito","time","descc","jobHandle","addressHandle"});
	}
	public int del(Solr solr) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="delete from t_solr_job where id=?";
		return super.executeUpdate(sql, solr, new String[] {"id"});
	}
	public int edit(Solr solr) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="update  t_solr_job set job=?,company=?,address=?,salary=?,url=?,limito=?,time=?,descc=?,jobHandle=?,addressHandle=? where id=?";
		return super.executeUpdate(sql, solr, new String[] {"job","company","address","salary","url","limito","time","descc","jobHandle","addressHandle","id"});
		
	}
}

web层

package com.liuchunming.web;

import java.sql.SQLException;
import java.util.List;

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

import com.liuchunming.dao.SolrDao;
import com.liuchunming.entity.Solr;
import com.liuchunming.framework.ActionSupport;
import com.liuchunming.framework.ModelDriven;
import com.liuchunming.util.PageBean;

public class SolrAction extends ActionSupport implements ModelDriven<Solr> {
	private SolrDao solrDao =new SolrDao();
	private Solr solr=new Solr();
	@Override
	public Solr getModel() {
		// TODO Auto-generated method stub
		return solr;
	}
	//查询业务逻辑
	private String list(HttpServletRequest req,HttpServletResponse resp) {
		solr.setCompany(req.getParameter("company"));
		PageBean pageBean =new PageBean();
		pageBean.setRequest(req);
		try {
			List<Solr> list = solrDao.list(solr, pageBean);
			req.setAttribute("solrList", list);
			req.setAttribute("pageBean", pageBean);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}
	//跳转增加页面
	private String toAdd(HttpServletRequest req,HttpServletResponse resp) {
		return "toAdd";
	}
	//跳转修改
	private String toEdit(HttpServletRequest req,HttpServletResponse resp) {
		solr.setCompany(req.getParameter("id"));
		try {
			Solr s = this.solrDao.toEdit(solr, null).get(0);
			req.setAttribute("s", s);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toEdit";
	}
	//增加业务逻辑
	private String add(HttpServletRequest req,HttpServletResponse resp) {
		try {
			solr.setDescc("");
			solr.setJobHandle("");
			solr.setAddressHandle("");
			this.solrDao.add(solr);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	//修改业务逻辑
	private String edit(HttpServletRequest req,HttpServletResponse resp) {
		try {
			solr.setDescc("");
			solr.setJobHandle("");
			solr.setAddressHandle("");
			this.solrDao.edit(solr);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	//删除业务逻辑
	private String del(HttpServletRequest req,HttpServletResponse resp) {
		try {
			this.solrDao.del(solr);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
}

mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/solr" type="com.liuchunming.web.SolrAction">
		<forward name="list" path="/solrList.jsp" redirect="false" />
		<forward name="toAdd" path="/solrAdd.jsp" redirect="" />
		<forward name="toEdit" path="/solrEdit.jsp" redirect="false" />
		<forward name="toList" path="/solr.action?methodName=list" redirect="" />
	</action>
</config>

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结

今天就到这里了如果有什么不对的地方欢迎大家在评论区留言交流改进!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值