今日内容
一、利用框架开发项目
二、解决配置文件可随意更改问题
一、
补充上次知识点:配置
1、在web.xml<servlert>里面加上以下代码
<init-param>
<param-name>configurationLocation</param-name>
<param-value>/mvc.xml</param-value>
</init-param>
2、新建一个文件夹用反射mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/book" type="com.zking.gwq.framework.BookAction">
<forward name="list" path="/booklist.jsp" redirect="false" />
增删改操作之后要再一次查询数据库的新的数据做展示
<forward name="toList" path="/book.action?methodName=list" redirect="true" />
</action>
</config>
然后在DispathServlet里面配置的文件
二、
自用jar包:
1、选中你要作为jar包用的包
2、搜索jar选中红色标记点 3、选要路径
4、然后finish,最后复制到自己想要用的项目里就可以了
三、
学完整个mvc就可以利用框架开发一个项目啦
Dao方法
package com.zking.Dao;
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.zking.entity.Book;
import com.zking.util.BaseDao;
import com.zking.util.DBAccess;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;
public class BookDao extends BaseDao<Book>{
/**
* 新增
* @param student
* @return
*/
public int add(Book student) {
String sql="insert into t_student(sname,sname_pinyin,age,remark) values('"+student.getSname()+"','"+student.getSnamePinyin()+"','"+student.getAge()+"','"+student.getRemark()+"')";
return this.executeUpdate(sql);
}
/**
* 修改
* @param student
* @return
*/
public int update(Book student) {
String sql="update t_student set sname='"+student.getSname()+"',sname_pinyin='"+student.getSnamePinyin()+"',age='"+student.getAge()+"',remark='"+student.getRemark()+"' where sid="+student.getSid();
return this.executeUpdate(sql);
}
/**
* 删除
* @param student
* @return
*/
public int del(Book student) {
String sql="delete from t_student where sid="+student.getSid();
return this.executeUpdate(sql);
}
/**
* 查询所有
* @param student
* @param pageIndex
* @param pageSize
* @return
* @throws Exception
*/
public List<Book> list(Book student,PageBean pagebean) throws Exception{
int sid=student.getSid();
String sql="select * from t_student where 1=1";
if(StringUtils.isNotBlank(student.getSname())) {
sql+=" and sname like '%"+student.getSname()+"%'";
}
if(sid!=0) {
sql+=" and sid= "+ sid;
}
return this.executeQuery(sql, pagebean, new CallBack<Book>() {
@Override
public List<Book> forEach(ResultSet rs) throws Exception{
List<Book> students=new ArrayList<>();
Book s = null;
while(rs.next()) {
s=new Book();
s.setSid(rs.getInt("sid"));
s.setSname(rs.getString("sname"));
s.setAge(rs.getInt("age"));
s.setRemark(rs.getString("remark"));
students.add(s);
}
return students;
}
});
}
public static void main(String[] args) throws Exception {
Book book = new Book();
book.setSid(123456);
/*book.setSname("张");
book.setSnamePinyin("123");
book.setAge(1222222);
book.setRemark("nb");*/
BookDao bookdao = new BookDao();
// bookdao.add(book);
// bookdao.update(book);
bookdao.del(book);
}
}
子控制器
package com.zking.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.Dao.BookDao;
import com.zking.entity.Book;
import com.zking.gwq.framework.ActionSupport;
import com.zking.gwq.framework.ModelDriver;
import com.zking.util.PageBean;
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book = new Book();
private BookDao bookDao = new BookDao();
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
public String add(HttpServletRequest req, HttpServletResponse resp) {
bookDao.add(book);
if(book!=null) {
System.out.println(book.getSname());
}
return "toList";
}
public String delete(HttpServletRequest req, HttpServletResponse resp) {
bookDao.del(book);
return "toList";
}
public String edit(HttpServletRequest req, HttpServletResponse resp) {
bookDao.update(book);
return "toList";
}
public String list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = bookDao.list(book, pageBean);
req.setAttribute("books", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
public String toEdit(HttpServletRequest req, HttpServletResponse resp) {
//如果跳转的是新增界面无需查询,如果跳转的是修改界面,需要查询sid对应的数据,回显界面
if(book.getSid()!=0) {
try {
//sid=16
List<Book> list = bookDao.list(book, null);
req.setAttribute("b", list.get(0));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "toEdit";
}
}
不要忘记jar包和tid
项目需要的包和文件夹