目录
1.搭建自定义mvc框架环境

2.基础的增删改
package com.hz.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hz.entity.Book;
import com.hz.util.BaseDao;
import com.hz.util.PageBean;
import com.hz.util.StringUtils;
public class BookDao extends BaseDao<Book>{
// 查看
public List<Book> list(Book book,PageBean pageBean) throws Exception{
String sql= "select*from t_mvc_book where 1=1";
String bname = book.getBname();
if(StringUtils.isNotBlank(bname)) {
sql +=" and bname like '%"+bname+"%'";
}
int bid = book.getBid();
if(bid!=0) {
sql+=" and bid = "+bid;
}
return super.executeQuery(sql, pageBean, rs ->{
List<Book> list =new ArrayList<>();
try {
while(rs.next()) {
list.add(new Book(rs.getInt("bid"),rs.getString("bname"),rs.getFloat("price")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
});
}
// 增加
public int add(Book book) throws Exception {
String sql="insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
}
// 删除
public int del (Book book) throws Exception {
String sql="delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, book, new String[] {"bid"});
}
// 修改
public int edit (Book book) throws Exception {
String sql="update t_mvc_book set bname=?,price=? where bid=?";
return super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
}
}
3.通用的增删改
package com.hz.util;
import java.lang.reflect.Field;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hz.entity.Book;
/**
* T代表的是实体类 可以是Book/User/Goods。。
* @author Administrator
*
* @param <T>
*/
public class BaseDao<T> {
// public List<T> executeQuery(String sql,PageBean pageBean,CallBack<T> callBack) throws Exception{
//// sql="SELECT * FROM `t_mvc_book` where bname like '%圣墟%'\r\n" +
//// "";
//// 从上面得到SELECT * FROM `t_mvc_book` where bname like '%圣墟%'
//// 目的是为了得到总记录数-》得到总页数
//// SELECT * FROM `t_mvc_book` where bname like '%圣墟%' limit 10,10
//
// /**
// *1.拿到数据库连接
// *2.拿到preparestatement
// *3.执行sql语句
// */
// Connection con=DBAccess.getConnection();//重复代码1
//
// PreparedStatement ps=con.prepareStatement(sql);//重复代码2
// ResultSet rs=ps.executeQuery();//重复代码3
// /*while(rs.next()) {
// list.add(new Book(rs.getInt("bid"),rs.getString("bname"), rs.getFloat("price")));
// }*/
//// 查询不同的表,必然要处理不同的结果集
//// 接口是调用方来实现
// return callBack.foreach(rs);
//
// }
public List<T> executeQuery(String sql,PageBean pageBean,CallBack<T> callBack) throws Exception{
// sql="SELECT * FROM `t_mvc_book` where bname like '%圣墟%'\r\n" +
// "";
// 从上面得到SELECT * FROM `t_mvc_book` where bname like '%圣墟%'
// 目的是为了得到总记录数-》得到总页数
// SELECT * FROM `t_mvc_book` where bname like '%圣墟%' limit 10,10
/**
*1.拿到数据库连接
*2.拿到preparestatement
*3.执行sql语句
*/
Connection con=null;//重复代码1
PreparedStatement ps=null;//重复代码2
ResultSet rs=null;//重复代码3
if(pageBean!=null&&pageBean.isPagination()) {
String countSQL=getcOuntSQL(sql);
con=DBAccess.getConnection();//重复代码1
ps=con.prepareStatement(countSQL);//重复代码2
rs=ps.executeQuery();//重复代码3
if(rs.next()) {
// 当前实体类就包含了总纪录数
pageBean.setTotal(rs.getString("n"));
}
String pageSQL=getPageSQL(sql,pageBean);
con=DBAccess.getConnection();//重复代码1
ps=con.prepareStatement(pageSQL);//重复代码2
rs=ps.executeQuery();//重复代码3
}
else {
con=DBAccess.getConnection();//重复代码1
ps=con.prepareStatement(sql);//重复代码2
rs=ps.executeQuery();//重复代码3
}
return callBack.foreach(rs);
}
/**
* 拼装第N页的数据SQL
* @param sql
* @param pageBean
* @return
*/
private String getPageSQL(String sql, PageBean pageBean) {
// TODO Auto-generated method stub
return sql+" limit " + pageBean.getStartIndex() +"," + pageBean.getRows();
}
/**
* 拼装符合条件总记录数的SQL
* @param sql
* @return
*/
private String getcOuntSQL(String sql) {
// sql="SELECT * FROM `t_mvc_book` where bname like '%圣墟%'\r\n" +
// "";
// select count(1) as n from (SELECT * from 't_mvc_book' where bname like '%圣墟%') t;
return "select count(1) as n from ("+sql+") t";
}
public int executeUpdate(String sql,T t,String[] sttrs ) throws Exception {
Connection con=DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
// 将t的摸一个属性对应的值加到pst对象中
for (int i = 0; i < sttrs.length; i++) {
Field f = t.getClass().getDeclaredField(sttrs[i]);
f.setAccessible(true);
ps.setObject(i+1, f.get(t));
}
// ps.setObject(1, book.getBid());
// ps.setObject(2, book.getBname());
// ps.setObj ect(3, book.getPrice());
return ps.executeUpdate();
}
}
4.查询删除及重复表单提交问题
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<action path ="/book" type="com.hz.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
<forward name="tolist" path="/book.action?methodName=list" redirect="true" />
</action>
</config>
5.新增修改的前端实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="h" uri="http://jsp.veryedu.cn" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
rel="stylesheet">
<script
src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍列表</title>
<style type="text/css">
.page-item input {
padding: 0;
width: 40px;
height: 100%;
text-align: center;
margin: 0 6px;
}
.page-item input, .page-item b {
line-height: 38px;
float: left;
font-weight: 400;
}
.page-item.go-input {
margin: 0 10px;
}
</style>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/book.action?methodName=list" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="bname"
placeholder="请输入书籍名称">
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=preEdit" >增加</a>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">书籍ID</th>
<th scope="col">书籍名</th>
<th scope="col">价格</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="b">
<tr>
<td>${b.bid}</td>
<td>${b.bname}</td>
<td>${b.price}</td>
<td>
<a href="${pageContext.request.contextPath }/book.action?methodName=preEdit&bid=${b.bid}" >修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}" >删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- http://localhost:8080/Y1_mvc_cru/book.action?methodName=list
-->
<h:page pageBean="${pageBean }"></h:page>
</body>
</html>
界面展示

2145

被折叠的 条评论
为什么被折叠?



