租房子的例题后端写法
如下图的目录结构:
HouseMapper.xml
<resultMap id="BaseResultMap" type="com.hanqi.model.House"> <id column="ID" jdbcType="DECIMAL" property="id" /> <result column="KEYWORD" jdbcType="VARCHAR" property="keyword" /> <result column="AREA" jdbcType="VARCHAR" property="area" /> <result column="SQUAREMETER" jdbcType="DECIMAL" property="squaremeter" /> <result column="RENT" jdbcType="DECIMAL" property="rent" /> <result column="RENTTYPE" jdbcType="VARCHAR" property="renttype" /> <result column="HOUSETYPE" jdbcType="VARCHAR" property="housetype" /> </resultMap> <sql id="Base_Column_List"> ID, KEYWORD, AREA, SQUAREMETER, RENT, RENTTYPE, HOUSETYPE </sql> <!-- 根据id查数据 --> <select id="selectByPrimaryKey" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from HAHA.HOUSE where ID = #{id} </select> <!-- 查询所有数据 --> <select id="selectAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from HAHA.HOUSE </select> <!-- 根据复选框选择的内容进行查询 --> <select id="selectByCheck" resultMap="BaseResultMap" parameterType="Map"> select <include refid="Base_Column_List" /> from house h <where> <if test="p1 != null"> and h.area in <foreach collection="p1" item="a" close=")" open="(" separator=","> #{a} </foreach> </if> <if test="p2 != null"> and h.renttype in <foreach collection="p2" item="r" close=")" open="(" separator=","> #{r} </foreach> </if> <if test="p3 != null"> and h.housetype in <foreach collection="p3" item="h" close=")" open="(" separator=","> #{h} </foreach> </if> <if test="p4 != null"> and h.keyword like '%'||#{p4}||'%' </if> </where> </select> <!-- 删除数据 --> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from HAHA.HOUSE where ID = #{id,jdbcType=DECIMAL} </delete> <!-- 插入数据 --> <insert id="insert" parameterType="com.hanqi.model.House"> insert into HAHA.HOUSE (ID, KEYWORD, AREA, SQUAREMETER, RENT, RENTTYPE, HOUSETYPE) values (sq_test.nextval, #{keyword,jdbcType=VARCHAR}, #{area,jdbcType=VARCHAR}, #{squaremeter,jdbcType=DECIMAL}, #{rent,jdbcType=DECIMAL}, #{renttype,jdbcType=VARCHAR}, #{housetype,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKeySelective" parameterType="com.hanqi.model.House"> <!-- 更新数据 --> update HAHA.HOUSE <set> <if test="keyword != null"> KEYWORD = #{keyword,jdbcType=VARCHAR}, </if> <if test="area != null"> AREA = #{area,jdbcType=VARCHAR}, </if> <if test="squaremeter != null"> SQUAREMETER = #{squaremeter,jdbcType=DECIMAL}, </if> <if test="rent != null"> RENT = #{rent,jdbcType=DECIMAL}, </if> <if test="renttype != null"> RENTTYPE = #{renttype,jdbcType=VARCHAR}, </if> <if test="housetype != null"> HOUSETYPE = #{housetype,jdbcType=VARCHAR}, </if> </set> where ID = #{id,jdbcType=DECIMAL} <!-- where ID = #{id} --> </update>
HouseMapper.java
public interface HouseMapper { int deleteByPrimaryKey(Integer id);//删除数据 int insert(House record); //用来插入数据的 int insertSelective(House record); List<House> selectAll(); //查询全部数据的 House selectByPrimaryKey(Integer id); //根据id查询信息 List<House> selectByCheck(Map<String, Object> map); //根据复选框选择的内容进行查询 int updateByPrimaryKeySelective(House record); //更新数据 int updateByPrimaryKey(House record); }
HouserService.java
public interface HouseService { public int insertHouse(House house); //插入数据 public List<House> selectAll(); //查询所有数据 public House selectByID(Integer id); //根据id查数据 public int updateHouse(House house); //更新数据 public int deleteHouse(Integer id); //删除数据 public List<House> selectByCheck(Map<String, Object> map); //根据复选框选择查询信息 }
HouserServiceImpl.java
@Repository public class HouseServiceImpl implements HouseService { @Autowired private HouseMapper houseMapper; @Override public int insertHouse(House house) { //插入数据 int h = houseMapper.insert(house); return h; } @Override public List<House> selectAll() { //查询全部信息 List<House> list = houseMapper.selectAll(); return list; } @Override public House selectByID(Integer id) { //根据id进行信息的查询 House h2 = houseMapper.selectByPrimaryKey(id); return h2; } @Override public int updateHouse(House house) { //数据更新 int h3 = houseMapper.updateByPrimaryKeySelective(house); return h3; } @Override public int deleteHouse(Integer id) { //删除数据 int h4 = houseMapper.deleteByPrimaryKey(id); return h4; } @Override public List<House> selectByCheck(Map<String, Object> map) { //根据复选框选择的内容进行查询 List<House> list2 = houseMapper.selectByCheck(map); return list2; } }
HouseController.java
@Controller @RequestMapping("/house2") public class HouseController { @Autowired private HouseService houseService; @RequestMapping("/toXin") public String toInsert() { //在搜索页面上点击添加新数据跳到insert.jsp页面上 return "insert"; } @RequestMapping("/insert") public String insertHouse(House house,Model md) { //插入数据 int h = houseService.insertHouse(house); if (h > 0) { md.addAttribute("msg", "插入成功!"); }else { md.addAttribute("msg", "插入失败!"); } return "success"; } @RequestMapping("/select") public ModelAndView selectAll(ModelAndView mav){ //查询全部数据 List<House> list = houseService.selectAll(); if(list.size() > 0){ mav.addObject("infolist", list); // mav.setViewName("forward:/manage.jsp"); //不走视图解析器了,直接来配置一下地址 mav.setViewName("manage"); }else { mav.setViewName("fail"); } return mav; } @RequestMapping("/selectbyid") public ModelAndView selectByID(ModelAndView mav,Integer id) { //根据id查询信息 House h = houseService.selectByID(id); mav.addObject("list", h); mav.setViewName("update"); return mav; } @RequestMapping("/update") public String updateinfo(Model md,House house) { //更新数据 int h2 = houseService.updateHouse(house); System.out.println(h2); if (h2 > 0) { md.addAttribute("msg", "更新成功!"); //mav.setViewName("redirect:/house2/select.do"); //重定向,目的是为了能够进行更新操作后,再次发送请求,看一下自己更新的结果 }else { md.addAttribute("msg", "更新失败!"); } return "success"; } @RequestMapping("/delete") public String deleteHouse(Model md,Integer id) { //删除信息 int h3 = houseService.deleteHouse(id); if (h3 > 0) { md.addAttribute("msg","删除成功!"); }else { md.addAttribute("msg","删除失败!"); } return "success"; } //根据复选框里面选择的选项进行信息的查询 @RequestMapping("/selectByCheck") public String selectByCheck( String[] area, String[] ht, String[] rt,String keyword, Model md) { Map<String,Object> map = MapUtil.mp(area,ht,rt,keyword); List<House> list2 = houseService.selectByCheck(map); md.addAttribute("houselist",list2); return "forward:/seareach.jsp" ; } }
在com.hanqi.util 包下面的MapUtil.java
public class MapUtil { //往map集合里面添加对象,直接在控制层里面进行调用 public static Map<String, Object> mp(Object... objs) { //静态的方法,直接调用 Map<String, Object> map = new HashMap<String, Object>(); int i = 1; for(Object o : objs) { map.put("p" + i, o); i++; } return map; } }