当前位置:网站首页>Customize MVC 3.0
Customize MVC 3.0
2022-07-24 06:27:00 【zsm030616】
Catalog
- 1. Make the frame into jar package , Then import the new project , And the dependence of the framework jar Import the package
- 2. Paging label related files 、 And related helper classes
- 3. Add the configuration file of the framework 、 as well as web.xml Configuration of
- 4. complete Book Entity class and bookDao Compiling
- 5. Complete the general addition, deletion and modification methods
1. Make the frame into jar package , Then import the new project , And the dependence of the framework jar Import the package






2. Paging label related files 、 And related helper classes
3. Add the configuration file of the framework 、 as well as web.xml Configuration of
4. complete Book Entity class and bookDao Compiling
package com.zsm.entity;
public class Book {
private int bid;
private String bname;
private float price;
public Book() {
// TODO Auto-generated constructor stub
}
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;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
}
package com.zsm.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zsm.entity.Book;
import com.zsm.util.BaseDao;
import com.zsm.util.DBAccess;
import com.zsm.util.PageBean;
import com.zsm.util.StringUtils;
public class BookDao extends BaseDao<Book>{
// Inquire about
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();
// The front desk jsp Pass it to the background , As long as it's passed, it's worth it , Not wearing it is the default , The default value is 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 (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
});
}
// increase
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();
}
// Delete
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();
}
// Change
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(3, book.getBid());
pst.setObject(1, book.getBname());
pst.setObject(2, book.getPrice());
return pst.executeUpdate();
}
}
And then use junit4 To test
Then it will automatically generate a BookDaoTest:
package com.zsm.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zsm.entity.Book;
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(), null);
for (Book book : list) {
System.out.println(book);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testAdd() {
fail("Not yet implemented");
}
@Test
public void testDel() {
fail("Not yet implemented");
}
@Test
public void testEdit() {
fail("Not yet implemented");
}
}
5. Complete the general addition, deletion and modification methods
Paging query :
@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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
increase
@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();
}
}
Change
@Test
public void testEdit() {
Book book = new Book(1234321, "123432100", 1234321);
try {
bookDao.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Delete
@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();
}
}
边栏推荐
- 【219】app 测试和web测试的区别点?
- Ia note 1
- 三分钟记住20道性能测试经典面试题
- leetcode剑指offer JZ23:链表中环的入口节点
- leetcode剑指offer JZ3 数组中重复的数字
- leetcode剑指offer JZ25 合并两个排序的链表
- Jenkins automated unattended operation (up / down)
- Flink production environment configuration recommendations
- Remember to get the password of college student account once, from scratch
- Leetcode sword finger offer JZ9 dual stack implementation queue
猜你喜欢
随机推荐
Maximum value of jz47 gifts (dynamic planning ideas)
【251】常见的测试工具
进行挂载永久挂载后无法开机
MySQL from basic to entry to high availability
【218】CS架构和BS架构以及数据放在服务端和客户端的利与弊?
XML parsing
Ia class summary (1)
Using keras and LSTM to realize time series prediction of long-term trend memory -lstnet
IP lesson summary (3)
IP notes (9)
Hololens 2 development 101: create the first hololens 2 Application
Leetcode sword finger offer jz25 merges two sorted linked lists
leetcode剑指offer jz5 替换空格字符串
Remote connection to Qunhui NAS at home [no public IP, free intranet penetration]
Summary of common working methods (7S, SWOT analysis, PDCA cycle, smart principle, 6w2h, time management, WBS, 28 principles)
将内网映射到公网【无需公网IP】
Dameng database_ Small matters needing attention during use
General paging 2.0
【219】app 测试和web测试的区别点?
MySQL数据库—SQL汇总(记得关注我!中国加油!)




![Map the intranet to the public network [no public IP required]](/img/d0/b391bcfcaeb4c7ad439e241334361b.png)



