先实现简单的对各个单表之间的增删改查的操作
就以增加数据为例,由于比较简单,就可以直接用注解的方式来实现
在mapper文件中,书写sql语句
@Insert("insert into houseinfo values (#{houseId},#{roomType},#{address},#{year},#{area},#{sales},#{status},#{agent},#{customer})")void add(House h);
然后在service层中实现函数
SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();@Overridepublic void add(House h) {SqlSession sqlSession = factory.openSession();HouseMapper mapper = sqlSession.getMapper(HouseMapper.class);mapper.add(h);sqlSession.commit();sqlSession.close();}
之后再servlet层接收前端表单的数据,实现添加操作
public void addHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");//接受数据String houseId = req.getParameter("houseId");String roomType = req.getParameter("roomType");String address = req.getParameter("address");String year = req.getParameter("year");String area = req.getParameter("area");String sales = req.getParameter("sales");String status = req.getParameter("status");House h = new House();h.setHouseId(houseId);h.setRoomType(roomType);h.setAddress(address);h.setYear(Integer.parseInt(year));h.setArea(Integer.parseInt(area));h.setSales(Integer.parseInt(sales));h.setStatus(status);houseService.add(h);req.getRequestDispatcher("/manager.html").forward(req, resp);}
这样就完成了单表的增加操作,简单的增删改跟上面大致相同。