目录
功能一:查询所有展示
1.根据城市来进行条件查询
2.当进入首页后显示所有网点信息
3.根据城市进行条件查询
功能二:新增网点
功能三:修改网点
功能四:删除功能
功能一:查询所有展示
1.根据城市来进行条件查询
findAllAreaServlet
package com.itheima.controller;import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.Area;
import com.itheima.service.AreaService;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;@WebServlet("/findAllAreaServlet")
public class findAllAreaServlet extends HttpServlet {private ObjectMapper objectMapper = new ObjectMapper();private AreaService as = new AreaService();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("application/json;charset=utf-8");List<Area> areas = as.SelectAllArea();objectMapper.writeValue(response.getWriter(),areas);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
AreaService
package com.itheima.service;import com.itheima.mapper.AreaMapper;
import com.itheima.pojo.Area;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;import java.util.List;/*** @Author zhouxiangyang* @Date 2022/9/23 13:58*/
public class AreaService {private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();public List<Area> SelectAllArea(){SqlSession sqlSession = sqlSessionFactory.openSession();AreaMapper mapper = sqlSession.getMapper(AreaMapper.class);List<Area> areas = mapper.selectAllArea();sqlSession.close();return areas;}
}
AreaMapper
@Select("select * from area")List<Area> selectAllArea();
工具类
package com.itheima.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();}}
2.当进入首页后显示所有网点信息
(和上面代码差不多,自行写)
3.根据城市进行条件查询
findAllBankServlet
package com.itheima.controller;import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.Bank;
import com.itheima.service.AreaService;
import com.itheima.service.BankService;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;@WebServlet("/findAllBankServlet")
public class findAllBankServlet extends HttpServlet {private ObjectMapper objectMapper = new ObjectMapper();private BankService bs = new BankService();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("application/json;charset=utf-8");String areaid = request.getParameter("areaid");List<Bank> banks = bs.SelectAllBank(areaid);System.out.println(banks);objectMapper.writeValue(response.getWriter(),banks);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
BandService
BrandMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.BankMapper"><!--根据城市查询id查询所有银行网点List<Bank> findAll(String areaid);--><select id="selectAllBank" resultType="com.itheima.pojo.Bank">select bank.*,area.name as areaName from bank,area<where>bank.areaid=area.id<if test="areaid!=-1">and bank.areaid=#{areaid}</if></where></select>
</mapper>
工具类
package com.itheima.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();}}
BrandMapper
List<Bank> selectAllBank(int areaid);
功能二:新增网点
功能三:修改网点
功能四:删除功能
(太简单了,就不在这边写了)