1.dao包
(1.)ManagerDao接口
public interface ManagerDao {
public Integer addManager(Manager manager);
public Integer delById(Integer id);
public Integer updateManager(Manager manager);
public List<Manager>findAll(String uname);
public Manager findById(Integer id);
}
(2.)ManagerDaoImpl
public class ManagerDaoImpl extends BaseDao implements ManagerDao {
@Override
public Integer addManager(Manager manager) {
return super.executeUpdate("insert into t_manager(uname,utype,uing)values(?,?,?)"
,new Object[]{manager.getUname(),manager.getUtype(),manager.getUing()});
}
@Override
public Integer delById(Integer id) {
return super.executeUpdate("delete from t_manager where id=?"
,new Object[]{id});
}
@Override
public Integer updateManager(Manager manager) {
return super.executeUpdate("update t_manager set uname=?,utype=?,uing=? where id=?",
new Object[]{manager.getUname(),manager.getUtype(),manager.getUing(),manager.getId()});
}
@Override
public List<Manager> findAll(String uname) {
List<Manager>list=new ArrayList<>();
Manager manager=null;
rs=super.executeQuery("select * from t_manager where uname like concat('%',?,'%')",new Object[]{uname});
try {
while(rs.next()){
manager = new Manager();
manager.setId(rs.getInt("id"));
manager.setUname(rs.getString("uname"));
manager.setUtype(rs.getString("utype"));
manager.setUing(rs.getString("uing"));
list.add(manager);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeConn();
}
return list;
}
@Override
public Manager findById(Integer id) {
Manager manager = null;
rs=super.executeQuery("select * from t_manager where id=?",new Object[]{id});
try {
while(rs.next()){
manager=new Manager();
manager.setId(rs.getInt("id"));
manager.setUname(rs.getString("uname"));
manager.setUtype(rs.getString("utype"));
manager.setUing(rs.getString("uing"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeConn();
}
return manager;
}
}
2.entity包
(1.) Manager
public class Manager {
private Integer id;
private String uname;
private String utype;
private String uing;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUtype() {
return utype;
}
public void setUtype(String utype) {
this.utype = utype;
}
public String getUing() {
return uing;
}
public void setUing(String uing) {
this.uing = uing;
}
}
3.service包
(1.)ManagerService
public interface ManagerService {
public Integer addManager(Manager manager);
public Integer delById(Integer id);
public Integer updateManager(Manager manager);
public List<Manager> findAll(String uname);
public Manager findById(Integer id);
}
(2.)ManagerServiceImpl
public class ManagerServiceImpl implements ManagerService {
ManagerDaoImpl managerDao=new ManagerDaoImpl();
@Override
public Integer addManager(Manager manager) {
return managerDao.addManager(manager);
}
@Override
public Integer delById(Integer id) {
return managerDao.delById(id);
}
@Override
public Integer updateManager(Manager manager) {
return managerDao.updateManager(manager);
}
@Override
public List<Manager> findAll(String uname) {
return managerDao.findAll(uname);
}
@Override
public Manager findById(Integer id) {
return managerDao.findById(id);
}
}
4、servlet
ManagerServlet
@WebServlet(name = "ManagerServlet")
public class ManagerServlet extends HttpServlet {
protected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//中文集
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ManagerServiceImpl managerService = new ManagerServiceImpl();
String action=request.getParameter("action");
if(action.equals("list")){
String suname=request.getParameter("suname");
if(suname!=null){
}else{
suname="";
}
List<Manager>list =managerService.findAll(suname);
request.setAttribute("list",list);
request.getRequestDispatcher("list.jsp").forward(request,response);
}else if(action.equals("add")){
String uname=request.getParameter("uname");
String utype=request.getParameter("utype");
String uing=request.getParameter("uing");
Manager manager = new Manager();
manager.setUname(uname);
manager.setUtype(utype);
manager.setUing(uing);
Integer flag=managerService.addManager(manager);
if(flag>0){
request.getRequestDispatcher("ManagerServlet?action=list").forward(request,response);
}
}else if(action.equals("delete")){
String id=request.getParameter("id");
Integer flag= managerService.delById(Integer.parseInt(id));
if(flag>0) {
request.getRequestDispatcher("ManagerServlet?action=list").forward(request, response);
}
}else if(action.equals("toupdate")){
String id=request.getParameter("id");
Manager manager=managerService.findById(Integer.parseInt(id));
request.setAttribute("manager",manager);
request.getRequestDispatcher("update.jsp").forward(request,response);
}else if(action.equals("update")){
String id= request.getParameter("id");
String uname=request.getParameter("uname");
String utype= request.getParameter("utype");
String uing=request.getParameter("uing");
Manager manager = new Manager();
manager.setId(Integer.parseInt(id));
manager.setUname(uname);
manager.setUtype(utype);
manager.setUing(uing);
Integer flag=managerService.updateManager(manager);
if(flag>0){
request.getRequestDispatcher("ManagerServlet?action=list").forward(request, response);
}
}
}
}