目录
一、基础增删改查
代码:
book实体类
package com.zxw.entity;
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;
}
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
super();
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
}
dao方法
package com.zxw.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zxw.entity.Book;
import com.zxw.util.BaseDao;
import com.zxw.util.DBAccess;
import com.zxw.util.PageBean;
import com.zxw.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();
//前台jsp传递到后台、只要传了就有值、没传就是默认值默认值就是0
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) {
e.printStackTrace();
}
return list;
});
}
// //增
public int add(Book book) throws Exception {
Connection con = DBAccess.getConnection();
String sql="insert into t_mvc_book values(?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setObject(1, book.getBid());
pst.setObject(2, book.getBname());
pst.setObject(3, book.getPrice());
return pst.executeUpdate();
}
//
// 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 {
Connection con = DBAccess.getConnection();
String sql="delete from t_mvc_book where bid = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setObject(1, book.getBid());
return pst.executeUpdate();
}
// 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 {
Connection con = DBAccess.getConnection();
String sql="update t_mvc_book set bname=?,price=? where bid = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setObject(1, book.getBname());
pst.setObject(2, book.getPrice());
pst.setObject(3, book.getBid());
return pst.executeUpdate();
}
// 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"});
// }
}
book测试类
package com.zxw.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zxw.entity.Book;
import com.zxw.util.PageBean;
public class BookDaoTest {
private BookDao bookDao = new BookDao();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testList() {
try {
List<Book> list = bookDao.list(new Book(), new PageBean());
for (Book book : list) {
System.out.println(book);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testAdd() {
Book book = new Book(1234321, "1234321", 1234321);
try {
bookDao.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testDel() {
Book book = new Book(1234321, "1234321", 1234321);
try {
bookDao.del(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testEdit() {
Book book = new Book(1234321, "111", 1234321);
try {
bookDao.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
效果演示:
增加
public void testAdd() {
Book book = new Book(1234321, "1234321", 1234321);
try {
bookDao.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效果:
删除
@Test
public void testDel() {
Book book = new Book(1234321, "1234321", 1234321);
try {
bookDao.del(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效果
修改
public void testEdit() {
Book book = new Book(1234321, "111", 1234321);
try {
bookDao.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效果