1.把工具类写成一个jar包
1.选择你要打包的文件,右键找到EXport
2.如图所示
3.点击finish,就可以成功了,记得设置路径
2.JD-GUI
作用:可以查看jar包的内容,
使用方法:打开软件,把jar包拖进去即可。
3.究极mvc
最终升级版的MVC终于来袭
1.创建一个类,BaseDao
public class BaseDao<T> {
private static final String T = null;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
public List<T> executeQuery(String sql,Class clz,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
List<T> list;
try {
list = new ArrayList<>();
con = DBAccess.getConnection();
if (pagebean != null && pagebean.isPagination()) {
//该分页了
String countSql=getCountSql(sql);
ps=con.prepareStatement(countSql);
rs=ps.executeQuery();//结果集
if(rs.next()) {
pagebean.setTotal(rs.getLong(1)+"");
}
String pageSql=getPageSql(sql,pagebean);
ps=con.prepareStatement(pageSql);
rs=ps.executeQuery();
} else {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
}
while (rs.next()) {
T t = (T) clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(t, rs.getObject(field.getName()));
}
list.add(t);
}
} finally {
DBAccess.close(con);
}
return list;
}
private String getPageSql(String sql, PageBean pagebean) {
return sql + " limit "+pagebean.getStartIndex() +","+pagebean.getRows();
}
private String getCountSql(String sql) {
// TODO Auto-generated method stub
return "select count(1) from ("+sql+") t";
}
public int executeUpdate(String sql,String[] attrs,T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
for (int i = 0; i < attrs.length; i++) {
Field field = t.getClass().getDeclaredField(attrs[i]);
field.setAccessible(true);
ps.setObject(i+1, field.get(t));
}
return ps.executeUpdate();
}
2.创建一个类BookDao,继承BaseDao
public class BookDao extends BaseDao<Book>{
// 增加
public int add(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book);
}
// 删除
public int delete(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, new String[] {"bid"}, book);
}
//
带分页查询
public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_mvc_book where true";
if(util.StringUtils.isNotBlank(book.getBname())) {
sql+=" and bname like '%"+book.getBname()+"%'";
}
return super.executeQuery(sql, Book.class, pageBean);
}
// 修改
public int update(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="update t_mvc_book set bname=?,price=? where bid=?";
return super.executeUpdate(sql, new String[] {"bname","price","bid"}, book);
}
}
3.写子控制器BookAction
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private BookDao bookDao=new BookDao();
private Book book=new Book();
/**
* 查询分页数据
*
* @param req
* @param resp
* @return yinyi
*/
public String list(HttpServletRequest req,HttpServletResponse resp) {
try {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Book> list = this.bookDao.list(book, pageBean);
req.setAttribute("bookList", list);//存值
req.setAttribute("pageBean", pageBean);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return "list";
}
}
/**
* 增加书本
*
* @param req
* @param resp
* @return yinyi
*/
public String add(HttpServletRequest req,HttpServletResponse resp) {
try {
int n = this.bookDao.add(book);
} 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";
}
/**
* 删除书本
*
* @param req
* @param resp
* @return yinyi
*/
public String delete(HttpServletRequest req,HttpServletResponse resp) {
try {
int n = this.bookDao.delete(book);
} 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";
}
/**
* 需要进行更改的书本
* 修改方法
* @param req
* @param resp
* @return yinyi
*/
public String load(HttpServletRequest req,HttpServletResponse resp) {
try {
List<Book> list=this.bookDao.list(book, null);
req.setAttribute("book", list.get(0));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return "toEdit";
}
/**
* 开始修改书本信息
* @param req
* @param resp
* @return yinyi
*/
public String update(HttpServletRequest req,HttpServletResponse resp) {
try {
int n = this.bookDao.update(book);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
}
4.页面编写
4.1主界面
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主界面</title>
<script type="text/javascript">
function add(){
window.location.href="bookEdit.jsp";
}
function update(bid){
window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=load&&bid="+bid;
}
function del(bid){
window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=delete&&bid"+bid;
}
</script>
</head>
<body>
<h2>小说目录</h2>
<form action="${pageContext.request.contextPath}/bookAction.action?methodName=list"
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
</form>
<button onclick="add();">新增书籍</button>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
<c:forEach items="${bookList }" var="a">
<tr>
<td>${a.bid }</td>
<td>${a.bname }</td>
<td>${a.price }</td>
<td>
<button onclick="update(${a.bid });">修改</button>
<button onclick="del(${a.bid });">删除</button>
</td>
</tr>
</c:forEach>
</table>
</body>
4.2修改和增加
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加/修改界面</title>
<script type="text/javascript">
function doSubmit(bid){
var bookForm =document.getElementById("bookForm");
if(bid){
//修改
bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=update';
}else{
//增加
bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=add';
}
bookForm.submit();
}
</script>
</head>
<body>
<form id="bookForm" method="post" action="${pageContext.request.contextPath}/bookAction.action?methodName=list">
bid:<input name="bid" value="${book.bid }"><br>
bname:<input name="bname" value="${book.bname }"><br>
price:<input name="price" value="${book.price }"><br>
<input type="submit" value="提交" onclick="doSubmit();"><br
</form>
</body>
5.web配置
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>yinyi.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatherServlet</servlet-name>
<servlet-class>yinyi.com.framework.DispatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
6.MVC配置
<action path="/bookAction" type="web.BookAction">
<forward name="list" path="/book.jsp" redirect="false" />
<forward name="toList" path="bookAction.action?methodName=list" redirect="true" />
<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
</action>
4.总结
1.增删改用重定向,查询用转发
2. 自定义MVC框架的CRUD操作
3配置config.xml文件
4.导入PageTag自定义分页标签类
5.导入自定义分页标签的描述文件z.tld
今天的分享就到此结束,制作不易,记得点个赞哈!!!