目录
一、配置步骤
二、配置框架前三步
导入相应的jar
导入相应的Class
导入xml文件
三、优化基本操作(增删改)
1、基础优化
编写实体类
编写BookDao类
优化BookDao
JUnit测试
2、后台优化
3、前端优化
导入之后我们选中所有的jar包===》右键===》Build Path===》Add to Build Path
测试我们的数据库能否连接
package com.tgq.entity;
/**
* 实体类
*
* @author tgq
*
*/
public class Book {
private int bid;
private String bname;
private float price;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
// TODO Auto-generated constructor stub
}
}
编写增删改查的方法
package com.tgq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import com.tgq.entity.Book;
import com.tgq.util.BaseDao;
import com.tgq.util.DBAccess;
import com.tgq.util.PageBean;
import com.tgq.util.StringUtils;
public class BookDao extends BaseDao {
public List list(Book book, PageBean pageBean) throws Exception {
String sql = "select * from t_mvc_book where 1=1 ";
String bname = book.getBname();
int bid = book.getBid();
// 判断是否为空
if (StringUtils.isNotBlank(bname)) {
sql += " and bname like '%" + bname + "%'";
}
// 判断前台是否传bid
if (bid != 0) {
sql += " and bid=" + bid;
}
return super.executeQuery(sql, Book.class, pageBean);
}
/**
* 增加Book的方法
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int addBook(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBid());
ps.setObject(2, book.getBname());
ps.setObject(3, book.getPrice());
return ps.executeUpdate();
}
/**
* 删除Book的方法 ,根据id删除
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int delBook(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid=?";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBid());
return ps.executeUpdate();
}
/**
* 修改Book的方法
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int updateBook(Book book) throws Exception {
String sql = "update t_mvc_book set bname=?,price=? where bid=?";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBname());
ps.setObject(2, book.getPrice());
ps.setObject(3, book.getBid());
return ps.executeUpdate();
}
}
1、在增删改的方法里面我们这两行代码是一样的、重复的
Connection conn = DBAccess.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);
2、代码逻辑是重复的===》sql占位符赋值
ps.setObject(1, book.getBid()); ps.setObject(2, book.getBname()); ps.setObject(3, book.getPrice());
怎么解决?BaseDao
进行一个通用的增删改的方法/** * 通用的增删改方法 * * @param sql * 数据库sql语句 * @param t * 对应表的实体类 * @param attrs * 要修改的属性值 * @return * @throws Exception * 万能异常 */ public int executeUpdate(String sql, T t, String[] attrs) throws Exception { Connection conn = DBAccess.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); //利用fori 下标来进行?的赋值 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(); }
我们在BaseDao类里面增加了一个通用的增删改的方法,我们在BookDao里面利用起来
/**
* 增加Book的方法
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int addBook(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, book, new String[] { "bid", "bname", "price" });
}
/**
* 删除Book的方法 ,根据id删除
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int delBook(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, book, new String[] { "bid" });
}
/**
* 修改Book的方法
*
* @param book
* 实体类
* @return
* @throws Exception
* 万能异常
*/
public int updateBook(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" });
}
选中我们的类名===》ctrl+N===》JUnit Test Case===》选中顶部的New JUnit 4 test===》Next
勾选BookDao===》Finish
BookDaoTest测试
package com.tgq.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import com.tgq.entity.Book;
import com.tgq.util.PageBean;
public class BookDaoTest {
private BookDao bookDao = new BookDao();
@Test
public void testList() throws Exception {
Book book = new Book();
book.setBname("52");
PageBean pageBean = new PageBean();
// pageBean.setPage(2);
List list = bookDao.list(book, pageBean);
for (Book b : list) {
System.out.println(b);
}
}
@Test
public void testAddBook() throws Exception {
Book book = new Book();
book.setBid(52);
book.setBname("5252");
book.setPrice(12f);
bookDao.addBook(book);
}
@Test
public void testDelBook() throws Exception {
Book book = new Book();
book.setBid(52);
bookDao.delBook(book);
}
@Test
public void testUpdateBook() throws Exception {
Book book = new Book();
book.setBid(52);
book.setBname("5252");
book.setPrice(19f);
bookDao.updateBook(book);
}
}
测试结果:
我们新建一个web包,在里面新建一个BookAction类(class),继承ActionSupport,实现
ModelDriver接口,编写
package com.tgq.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tgq.dao.BookDao;
import com.tgq.entity.Book;
import com.tgq.util.PageBean;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
public class BookAction extends ActionSupport implements ModelDriver {
private Book book = new Book();
private BookDao bookDao = new BookDao();
// 查询 bookList.jsp
public String list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List list = bookDao.list(book, pageBean);
req.setAttribute("list", list);
req.setAttribute("pageBean", pageBean);
return "list";
}
// 编辑界面跳转 bookEdit.jsp
public String toEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
// 前提
if (book.getBid() != 0) {
List list = bookDao.list(book, null);// 不需要分页
req.setAttribute("b", list.get(0));
}
return "toEdit";
}
// 增加 重定向到book.action?methodName=list
public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bookDao.addBook(book);
return "toEdit";
}
// 删除
public String del(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bookDao.delBook(book);
return "toEdit";
}
// 修改
public String update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
bookDao.updateBook(book);
return "toEdit";
}
@Override
public Book getModel() {
return book;
}
}
我们编写jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
书籍列表
书籍ID
书籍名
价格
操作
${b.bid }
${b.bname }
${b.price }
修改
删除
增加,修改的jsp界面。
判断你点击的是否是增加还是修改
action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit'}"
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
书籍新增/修改
我们运行书籍列表
进行一个搜索,新增、修改、删除
我们删除ID:16、22
新增一个
ID:12
书籍名:圣墟12
价格:123
修改ID:12
书籍名:圣墟1234
价格:12345
希望对你们有用!!!