深圳做生鲜的网站叫什么,公司文件页面设计,网站架构设计师面试技巧,多语言 网站最近在用Mybatis做项目的时候遇到了不少问题#xff0c;今天我就在这和大家分享一下#xff0c;稀稀拉拉的研究了两天#xff0c;终于搞好了#xff01;开发人员#xff1a;1111开发软件#xff1a;Myeclipse用到的框架技术#xff1a;Mybatis数据库#xff1a;MySql主… 最近在用Mybatis做项目的时候遇到了不少问题今天我就在这和大家分享一下稀稀拉拉的研究了两天终于搞好了 开发人员1111 开发软件Myeclipse 用到的框架技术Mybatis 数据库MySql 主要内容动态分页查询数据 好了现在开始演示我先把代码贴上来以便大家的理解mybatis-config.xml的主要配置内容[html] view plain copy print??xml version1.0 encodingUTF-8? !DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd configuration typeAliases !-- 动态查询房屋信息的条件类 -- typeAlias typecn.bdqn.mhouse.entity.HouseCondition aliashouseC/ typeAlias typecn.bdqn.mhouse.util.Page aliaspage/ !-- 区县别名 -- typeAlias typecn.bdqn.mhouse.entity.District aliasdistrict/ typeAlias typecn.bdqn.mhouse.dao.IDistrictDao aliasdistrictDao/ !-- 房屋信息的别名 -- typeAlias typecn.bdqn.mhouse.entity.House aliashouse/ typeAlias typecn.bdqn.mhouse.dao.IHouseDao aliashouseDao/ !-- 街道信息的别名 -- typeAlias typecn.bdqn.mhouse.entity.Street aliasstreet/ typeAlias typecn.bdqn.mhouse.dao.IStreetDao aliasstreetDao/ !-- 房屋类型的别名 -- typeAlias typecn.bdqn.mhouse.entity.Types aliastypes/ typeAlias typecn.bdqn.mhouse.dao.ITypesDao aliastypesDao/ !-- 用户信息的别名 -- typeAlias typecn.bdqn.mhouse.entity.Users aliasusers/ typeAlias typecn.bdqn.mhouse.dao.IUsersDao aliasusersDao/ /typeAliases environments defaultMysqldevelopment !-- oracle的数据库配置 -- environment idOracledevelopment transactionManager typeJDBC/ dataSource typePOOLED property namedriver valueoracle.jdbc.OracleDriver/ property nameurl valuejdbc:oracle:thin:127.0.0.1:1521:orcl/ property nameusername valuescott/ property namepassword value123/ /dataSource /environment !-- mysql的数据库配置 -- environment idMysqldevelopment transactionManager typeJDBC/ dataSource typePOOLED property namedriver valuecom.mysql.jdbc.Driver/ property nameurl valuejdbc:mysql://192.168.1.128:3306/house/ property nameusername valueroot/ property namepassword value171268/ /dataSource /environment /environments mappers mapper resourcecn/bdqn/mhouse/dao/DistrictDaoMapper.xml/ mapper resourcecn/bdqn/mhouse/dao/HouseDaoMapper.xml/ mapper resourcecn/bdqn/mhouse/dao/StreetDaoMapper.xml/ mapper resourcecn/bdqn/mhouse/dao/TypesDaoMapper.xml/ mapper resourcecn/bdqn/mhouse/dao/UsersDaoMapper.xml/ /mappers /configuration 由于分页查询得用到总记录数所以我写了两个方法来实现的第一个是动态查询数据总记录数接下来大家看看impl类和相对应的Mapper.xml配置信息吧由于dao接口是由impl实现类来实现的所以在这我就不给大家放dao层接口的代码了 动态查询数据的impl代码[java] view plain copy print?/** * (非 Javadoc) * pTitle: reCount/p * pDescription(描述):动态查询总计录数/p * param housec * return * see cn.bdqn.mhouse.dao.IHouseDao#reCount(cn.bdqn.mhouse.entity.HouseCondition) */ Override public int reCount(HouseCondition housec) { SqlSession sessionMybatisUtil.getSession(); Integer count(Integer)session.selectOne(houseDao.reCount,housec); return count; } 代码中的MybatisUtils是mybatis的工具类动态查询数据方法在Mapper.xml里面的配置详情代码[html] view plain copy print?!-- 动态查询房屋信息的总记录数 -- select idreCount parameterTypehouseC resultTypeInteger select count(0) from house h where if testpriceBegin!null and h.price #{priceBegin} /if if testpriceEnd!null and h.price ![CDATA[]] #{priceEnd} /if !-- h.street_id是数据库的字段名 -- if teststreet!null and h.street_id #{street.id} /if !-- h.type_id是数据库的字段名 -- if testtypes!null and h.type_id #{types.id} /if if testfloorageBegin!null and h.floorage #{floorageBegin} /if if testfloorageEnd!null and h.floorage ![CDATA[]] #{floorageEnd} /if /where /select 然后我把表与表之间的关联映射在放上来供大家看看[html] view plain copy print?resultMap idBaseResultMap typehouse id columnID propertyid jdbcTypeINTEGER / result columnTITLE propertytitle jdbcTypeVARCHAR / result columnDESCRIPTION propertydescription jdbcTypeVARCHAR / result columnPRICE propertyprice jdbcTypeREAL / result columnPUBDATE propertypubdate jdbcTypeDATE / result columnFLOORAGE propertyfloorage jdbcTypeINTEGER / result columnCONTACT propertycontact jdbcTypeVARCHAR / !-- 开始映射外键 -- !-- 映射用户表 -- association propertyusers columnuser_id selectselectUsers/ !-- 映射类型表 -- association propertytypes columntype_id selectselectTypes/ !-- 映射街道表 -- association propertystreet columnstreet_id selectselectStreet/ /resultMap !-- 关联用户表 -- resultMap idusersMapper typeusers id columnID propertyid jdbcTypeINTEGER / result columnNAME propertyname jdbcTypeVARCHAR / result columnPASSWORD propertypassword jdbcTypeVARCHAR / result columnTELEPHONE propertytelephone jdbcTypeVARCHAR / result columnUSERNAME propertyusername jdbcTypeVARCHAR / result columnISADMIN propertyisadmin jdbcTypeVARCHAR / /resultMap !-- 关联街道表 -- resultMap idstreetMapper typestreet id columnID propertyid / result columnNAME propertyname jdbcTypeVARCHAR / association propertydistrict columndistrict_id select selectDirstrict/ /resultMap !-- 关联区县表 -- resultMap iddistrictDaoMapper typedistrict id columnID propertyid/ result columnNAME propertyname/ /resultMap !-- 在根据区县id查询一遍区县表 -- select idselectDirstrict resultMapdistrictDaoMapper select * form district where id#{district_id} /select !--关联类型表 -- resultMap idtypeMapper typetypes id columnID propertyid/ result columnNAME propertyname jdbcTypeVARCHAR / /resultMap !-- 用户表 -- select idselectUsers resultMapusersMapper select * from users where id#{user_id} /select !-- 街道表 -- select idselectStreet resultMapstreetMapper select * from street where id#{street_id} /select !-- 类型表 -- select idselectTypes resultMaptypeMapper select * from types where id#{type_id} /select sql idBase_Column_List ID, USER_ID, TYPE_ID, TITLE, DESCRIPTION, PRICE, PUBDATE, FLOORAGE, CONTACT, STREET_ID /sql 上面都有相对应的注释在这就不多做解释了 总记录数现在查询出来了就开始动态分页查询数据了首先得用到一个分页类Page分页类的代码[java] view plain copy print?package cn.bdqn.mhouse.util; import java.util.ArrayList; import java.util.List; import cn.bdqn.mhouse.entity.House; /** * * * 项目名称mhouse * 类名称Page * 类描述 分页的工具类 * 创建人Mu Xiongxiong * 创建时间2017-3-17 下午1:04:02 * 修改人Mu Xiongxiong * 修改时间2017-3-17 下午1:04:02 * 修改备注 * version * */ public class Page { private int pageSize3; //页大小 private int pageIndex0; //当前页号 private int totalPageCount0; //总页数 private int record0; //记录总数 private Integer nextPage; //下一页 private Integer prePage; //上一页 private ListHouse houseListnew ArrayListHouse(); //房屋信息的集合 /** * author Mu Xiongxiong * created 2017-3-17 下午10:04:41 * return type */ public ListHouse getHouseList() { return houseList; } /** * author Mu Xiongxiong * created 2017-3-17 下午10:04:41 * param houseList */ public void setHouseList(ListHouse houseList) { this.houseList houseList; } //得到开始记录数 public int getSartRow(){ return (pageIndex-1)*pageSize; } //得到结束记录数 public int getEndRow(){ return pageSize; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize pageSize; } public int getPageIndex() { return pageIndex; } //得到当前页 public void setPageIndex(int pageIndex) { this.pageIndex pageIndex; //下一页 setNextPage(); //上一页 setPrePage(); } public int getTotalPageCount() { return totalPageCount; } //总页数 public void setTotalPageCount() { int totalP record % getPageSize() 0 ? record / getPageSize() : record/ getPageSize() 1; this.totalPageCount totalP; } public int getRecord() { return record; } //总记录数 public void setRecord(int record) { this.record record; //设置总页数 setTotalPageCount(); } public Integer getNextPage() { return nextPage; } //设置下一页 public void setNextPage() { this.nextPage this.pageIndex1; } public Integer getPrePage() { return prePage; } //设置上一页 public void setPrePage() { this.prePage this.pageIndex-1; if(this.prePage1){ this.prePage1; } } } 现在分页的工具类也出来了就差分页查询数据了先看impl里面的方法[java] view plain copy print?/** * (非 Javadoc) * pTitle: getHouseInfoByDymanic/p * pDescription:‘动态分页查询房屋信息/p * param housec * param pageIndex * return * see cn.bdqn.mhouse.dao.IHouseDao#getHouseInfoByDymanic(cn.bdqn.mhouse.entity.HouseCondition, int) */ Override public Page getHouseInfoByDymanic(HouseCondition housec,int pageIndex) { Page pagenew Page(); page.setPageIndex(pageIndex); //当前页 int reCountreCount(housec); page.setRecord(reCount); //总记录数 ListHouse houseListnew ArrayListHouse(); HashMap parMapnew HashMap(); parMap.put(priceBegin,housec.getPriceBegin()); parMap.put(priceEnd,housec.getPriceEnd()); if(housec.getStreet()!null){ parMap.put(street,housec.getStreet()); } if(housec.getTypes()!null){ parMap.put(types,housec.getTypes()); } parMap.put(floorageBegin, housec.getFloorageBegin()); parMap.put(floorageEnd,housec.getFloorageEnd()); parMap.put(stratRow,page.getSartRow()); parMap.put(endRow,page.getEndRow()); SqlSession sessionMybatisUtil.getSession(); try { houseListsession.selectList(houseDao.getHouseInfoByDymanic,parMap); page.setHouseList(houseList); } catch (Exception e) { e.printStackTrace(); }finally{ MybatisUtil.closeSession(); } return page; } 对应的Mapper.xml配置信息[html] view plain copy print?!-- 分页动态查询房屋信息 -- select idgetHouseInfoByDymanic parameterTypehashmap resultMapBaseResultMap select * from house h where if testpriceBegin!null and h.price #{priceBegin} /if if testpriceEnd!null and h.price ![CDATA[]] #{priceEnd} /if if teststreet!null and h.street_id #{street.id} /if if testtypes!null||!typesnull and h.type_id #{types.id} /if if testfloorageBegin!null and h.floorage #{floorageBegin} /if if testfloorageEnd!null and h.floorage ![CDATA[]] #{floorageEnd} /if /where limit #{stratRow},#{endRow} /select 最后我写了test测试看看能不能正常执行test测试类的方法如下[java] view plain copy print? /** * * Title: reCount * Description: 该方法的主要作用分页查询房屋信息 * param 设定文件 * return 返回类型void * throws */ Test public void getHouseInfoByDymanic(){ Page pagenew Page(); // houseC.setPriceBegin(50); //起始价格 // houseC.setPriceEnd(4000); //结束价格 // houseC.setFloorageBegin(10); //起始面积 // houseC.setFloorageEnd(6000); //最终面积 types.setId(1003); //房屋类型 houseC.setTypes(types); street.setId(1003); //所在的街道 // //street.setDistrict(district); houseC.setStreet(street); int pageIndex3; pagehouseDao.getHouseInfoByDymanic(houseC, pageIndex); System.out.println(当前页是page.getPageIndex()); System.out.println(下一页是page.getNextPage()); System.out.println(上一页是page.getPrePage()); System.out.println(总记录数page.getRecord()); System.out.println(总页数是page.getTotalPageCount()); System.out.println(页大小是page.getPageSize()); ListHouse houselistpage.getHouseList(); for (House house : houselist) { System.out.println(房屋标题house.getTitle()); } } 执行完成之后分页的信息上一页下一页总页数等等都可以正常显示出来但是只有数据库里面的数据没有显示出来调试如图所示 集合里面是空的 出现错误之后我就开始解决于是写了个测试查询全部的房屋信息的方法测试了一遍之后,果然不出所料查出来的都是null,更为奇怪的是数据库中有34条记录而程序运行后查询出来的是34个null对没错就是34个null,一个也不多一个也不少 但是还很纳闷于是各种假设各种调试还是不行当我吧问题发在csdn上面问的时候忽然想起来了原来是映射的resultType错了我查询的是house,应该映射的是house实体类的权限定名我写的是Page的全限定名我写成page也不足为奇因为我的返回类型就是page 哎 就这个错我弄了两小时才解决掉实现是累的不行了 这个问题解决了之后我就继续测试其他的功能现在是数据数来了,但是正儿八经最主要的测试还没进行呢汗于是我就开始测试动态查询数据一步一步的测试的时候错又来了我把错误信息贴上来大家看看org.apache.ibatis.exceptions.PersistenceException:### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException:There is no getter for property named id in class java.lang.Integer### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named id in class java.lang.Integer嘴里骂了一句之后拿起水杯干了一杯普利斯挽起袖子就开始调试我就不信还解决不了你了 慢慢的。。。慢慢的时间过来20分钟之后终于找出来了原来是类型错了我给大家看看错误代码和正确代码 错误代码 大家注意红色框中的代码我取的是对象而后面赋值的却是id,怪不得报错 正确的代码如下 还是老规矩看红框中的代码去掉getId()就可以正常运行了 还有其他的好多细节错误来着这里就不具体详细说了要想查看完整的分页动态查询代码请移步到 1111的博客 这里去看希望对大家有帮助