三层架构完成增删改查篇章七
【摘要】 完成删除这不是有手就行?这个当时看的视频黑马没有说这个,于是自己补上了。先定义一个删除的方法 @Delete("delete from tb_brand where id = #{id}") void delete(int id);回顾一下工具类和Service层package com.jgdabc.util;import org.apache.ibatis.io.Resour...
完成删除
这不是有手就行?
这个当时看的视频黑马没有说这个,于是自己补上了。
先定义一个删除的方法
@Delete("delete from tb_brand where id = #{id}")
void delete(int id);
回顾一下工具类和Service层
package com.jgdabc.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
//静态代码块会随着类的加载而自动执行,且只执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
package com.jgdabc.service;
import com.jgdabc.mapper.BrandMapper;
import com.jgdabc.pojo.Brand;
import com.jgdabc.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class BrandService {
SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
public List<Brand>selectAll()
{
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
List<Brand> brands = mapper.selectAll();
sqlSession.close();
return brands;
}
public void add(Brand brand)
{
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.add(brand);
sqlSession.commit();
sqlSession.close();
}
// 根据id查询
public Brand selectById(int id)
{
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
Brand brand = mapper.selectById(id);
sqlSession.close();
return brand;
}
public void update(Brand brand)
{
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.update(brand);
sqlSession.commit();
sqlSession.close();
}
public void delete(Integer brand)
{
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.delete(brand);
sqlSession.commit();
sqlSession.close();
}
}
我们的service调用的delete方法
注意看我们展示界面的删除
好,转发到Servlet
package com.jgdabc.util.web;
import com.jgdabc.service.BrandService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/selectDeleteServlet")
public class SelectDeleteServlet extends HttpServlet {
BrandService service = new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BrandService brandService = new BrandService();
String id = request.getParameter("id");
Integer id_1 = Integer.parseInt(id);
brandService.delete(id_1);
// request.getRequestDispatcher("/brand.jsp").forward(request,response);
response.sendRedirect("selectAllServlet");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
这样就可以了。
我们需要再写一个删除的jsp吗?没有必要。前面的添加修改,都需要进行传参的,然后需要根据参数进行在Servlet进行操作。删除直接调用删除方法在主界面中传参,然后传到Servlet进行处理调用方法,再次回到主界面就可以了。
这样就删除成功了。
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)