自己写的一个简易开发框架,很简单,只包括如下内容:
1、简单数据CURD
2、分页、排序功能
3、错误提示功能
4、条件查询功能
5、分组查询功能
至于缓存功能、多表连接处理、客户端javascript验证等,日后在慢慢添加。
假设数据库存在如下数据库表:
CREATE TABLE `bbs_file` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`filepath` varchar(50) DEFAULT NULL,
`topicId` int(10) DEFAULT NULL,
`logtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
开发步骤:
1、新建与表对应的POJO,
package com.wj.mode;
public class File {
private int id;
private String filepath;
private int topicId;
private String logtime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
public int getTopicId() {
return topicId;
}
public void setTopicId(int topicId) {
this.topicId = topicId;
}
public String getLogtime() {
return logtime;
}
public void setLogtime(String logtime) {
this.logtime = logtime;
}
@Override
public String toString() {
return "File [id=" + id + ", filepath=" + filepath + ", topicId="
+ topicId + ", logtime=" + logtime + "]";
}
}
2、新建该POJO对应的CURD dao操作:
package com.wj.db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.common.BaseDaoSupport;
import com.common.PageInfo;
import com.common.RowMapper;
import com.wj.mode.File;
public class FileDao extends BaseDaoSupport<File> {
@Override
public int insert(File t) throws SQLException {
String sql = "insert into bbs_file (filepath,topicid,logtime) values (?,?,now())";
Object[] args = { t.getFilepath(), t.getTopicId() };
return jdbcTemplate.update(sql, args);
}
@Override
public int update(File t) throws SQLException {
String sql = "update bbs_file set filepath = ?,topicid = ? ,logtime=? where id = ?";
Object[] args = { t.getFilepath(), t.getTopicId(), t.getLogtime(),
t.getId() };
return jdbcTemplate.update(sql, args);
}
@Override
public int delete(Object... args) throws SQLException {
String sql = "delete from bbs_file where id = ?";
return jdbcTemplate.update(sql, args);
}
@Override
public File find(Object... args) throws SQLException {
String sql = "select * from bbs_file where id = ?";
return (File) jdbcTemplate.find(sql, args, new FileRowMapper());
}
@Override
public List<File> list(Object... args) throws SQLException {
String sql = "select * from bbs_file";
return jdbcTemplate.list(sql, args, new FileRowMapper());
}
@Override
public void page(PageInfo pageinfo, Object... args) throws SQLException {
String sql = "select * from bbs_file";
jdbcTemplate.listByPage(sql, args, pageinfo, new FileRowMapper());
}
class FileRowMapper implements RowMapper<File> {
@Override
public File rowMapper(ResultSet rs) throws SQLException {
File obj = new File();
obj.setFilepath(rs.getString("filepath"));
obj.setId(rs.getInt("id"));
obj.setLogtime(rs.getString("logtime"));
obj.setTopicId(rs.getInt("topicId"));
return obj;
}
}
}
注意该类继承了自定义的BaseDaoSupport类,该类涵盖了基本的数据库操作。
3、开发该类对应的FormBean:
package com.wj.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import com.wj.mode.File;
public class FileForm extends BaseForm<File> {
@Override
public Object getKey() {
return this.getData().getId();
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.data = new File();
super.reset(mapping, request);
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return super.validate(mapping, request);
}
}
BaseForm含有对File的基本存、取操作,该FileForm有一个基本的获取数据库主键操作getKey()
4、开发基本的FileAction类,对应数据的CRUD操作、
package com.wj.struts.action;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wj.db.FileDao;
import com.wj.mode.File;
public class FileAction extends BaseAction<File> {
private static Logger logger = LoggerFactory.getLogger(FileAction.class);
{
super.destObject = "File";
super.dao = new FileDao();
}
@Override
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return super.defalutList(mapping, form, request, response);
}
@Override
protected void initData(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws SQLException {
}
@Override
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return super.defaultDelete(mapping, form, request, response);
}
@Override
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return super.defaultUpdate(mapping, form, request, response);
}
@Override
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return super.defaultAdd(mapping, form, request, response);
}
@Override
public ActionForward load(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return this.defaultLoad(mapping, form, request, response);
}
}
该类的所有操作都是基于BaseAction默认的处理,这样编码量大大减少。
5、配置struts-config.xml 添加file对应的action配置:
<action path="/fileAction" name="fileForm" parameter="method"
scope="request" type="com.wj.struts.action.FileAction" input="/projsp/fileUpdate.jsp">
<forward name="list" path="/projsp/fileList.jsp" />
<forward name="update" path="/projsp/fileUpdate.jsp" />
</action>
6、新建fileList.jsp列表显示页,用table方式显示数据库中的数据:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="common"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="projsp/table.css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<a href="fileAction.do?method=load&action=add">添加 </a>
</div>
<html:errors/>
<html:form action="fileAction.do?method=list">
<div>
<html:hidden property="pageInfo.queryCondition1" value="topicid=?"/>
帖子ID:<html:text property="pageInfo.queryValue1" /> <html:submit>提交</html:submit>
</div>
<table>
<tr>
<td>
<common:sort value="id" field="id"/>
</td>
<td>
<common:sort value="路径" field="filepath"/>
</td>
<td>
<common:sort value="帖子标题" field="topicid"/>
</td>
<td>
<common:sort value="时间" field="logtime"/>
</td>
<td>
操作
</td>
</tr>
<logic:present property="pageInfo.resultList" name="fileForm" scope="request">
<logic:iterate id="elem" property="pageInfo.resultList" name="fileForm" scope="request">
<tr>
<td>
${elem.id }
</td>
<td>
${elem.filepath }
</td>
<td>
${elem.topicId }
</td>
<td>
${elem.logtime }
</td>
<td>
<a href="fileAction.do?method=load&action=update&data.id=${elem.id }&${fileForm.pageInfo.pageInfor }">修改</a>
<a href="fileAction.do?method=delete&data.id=${elem.id }&${fileForm.pageInfo.pageInfor }">删除</a>
</td>
</tr>
</logic:iterate>
</logic:present>
<common:pagination form="fileForm" num="5"/>
</table>
</html:form>
</body>
</html>
7、新建fileUpdatte.jsp用于添加和修改file操作:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="common"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="projsp/table.css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
<html:form action="fileAction.do?method=${param.action }&action=${param.action }">
<html:hidden property="pageInfo.recSizePerPage"/>
<html:hidden property="pageInfo.totalRecordSize"/>
<html:hidden property="pageInfo.totalPageSize"/>
<html:hidden property="pageInfo.currentPageNum"/>
<html:hidden property="pageInfo.sort"/>
<html:hidden property="pageInfo.orderBy"/>
<table>
<tr>
<td>id</td>
<logic:equal value="update" parameter="action">
<td>
<bean:write property="data.id" name="fileForm"/>
<html:hidden property="data.id" name="fileForm" />
</td>
</logic:equal>
<logic:equal value="add" parameter="action">
<td>
<html:text property="data.id" name="fileForm" />
</td>
</logic:equal>
</tr>
<tr>
<td>文章ID</td>
<td><html:text property="data.topicId" name="fileForm"></html:text></td>
</tr>
<tr>
<td>文章路径</td>
<td><html:text property="data.filepath" name="fileForm"></html:text></td>
</tr>
<tr>
<td>时间</td>
<td><html:text property="data.logtime" name="fileForm"></html:text></td>
</tr>
<tr>
<td colspan="2">
<html:submit>提交</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html>
至此一个基本表的CRUD、排序、分页等功能就完成了。
此框架优点和适应范围:
1、数据操作的基本功能已经全部实现。复杂的就需要自己去处理了
2、对于简单的数据操作,此框架还是非常适用,对于大数据量的操作,就必须实际情况实际处理了
3、采用此框架需要编码的地方特别少,基本操作,排序,条件查询都被封装了。