mybatis使用过程中问题集
多个参数时需要参数绑定
Dao接口进行参数绑定
异常信息:
HTTP Status 500 - Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'acceptPersonId' not found. Available parameters are [1, 0, param1, param2]解决方案:
DAO层中传递参数时加@Param("acceptPersonId"),如下: List<Message> selectByUser(@Param("acceptPersonId") String acceptPersonId, @Param("status") String status); 多个参数时加@Param("acceptPersonId"),单个参数可以直接selectByUser(String acceptPersonId);
mybatis与in查询
- mybatis中如何使用in查询
<!--String[] getddUserIds(Integer[] userId); --> <select id="getddUserIds" parameterType="integer" resultType="string"> select ddUserId from user where userId in <foreach collection="array" open="(" close=")" separator="," item="userId" index="index"> #{userId} </foreach> </select>
mybatis查询结果与SQL查询字段不一致
SQL中查询的字段在经过mybatis处理后的查询结果中没有
例如属性名为pId,但是编辑器生成getter/setter方法时生成的是setpId和setpId
这就导致在某些版本(比如3.1.0版本)的mybatis中无法将pId的值与SQL中的值匹配,将getter/setter改为setPId和setPId即可:private String pId; public String getPId() { return pId; } public void setPId(String pId) { this.pId = pId; } <!--查询组织机构数据--> <select id="selectOrg" resultType="com.union.Management.bean.Org"> select a.DepartID as id,IFNULL(a.ParentID,0) as pid,a.DepartName as name from sys_domain as a order by a.ruleid </select>
字段为空时不更新该字段
使用条件判断
<!--基本信息-更新--> <update id="updateBasicInfoById" parameterType="com.union.Management.bean.PersonInfoDetailBasic"> UPDATE user_info AS a SET a.PersonName = #{personName}, a.Gender = #{gender},a.DepartID=#{departID} ,a.Weight=#{weight},a.IdentityCard=#{identityCard},a.Nation=#{nation},a.MobileNo=#{mobileNo} ,a.LastModifyUserID=#{lastModifyUserID} <if test="height != null and height != ''" > <!--SAMPLE_BATCH_NO = #{sampleBatchNo,jdbcType=VARCHAR},--> ,a.Height=#{height} </if> WHERE a.PersonSID =#{personSID} </update>
用c标签遍历实现select下拉框
jsp
jsp需要引入jstl: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 和开启EL表达式: <%@ page isELIgnored="false" %><%--开启EL表达式--%> <select class="form-control" id = "nationType" > <c:forEach items="${nations}" var="data"> <option value="${data.nationName}">${data.nationName}</option> </c:forEach> </select>下拉框数据反写
//民族下拉框值反写 var selectCode2 = document.getElementById("nationType"); for (var i = 0; i < selectCode2.options.length; i++) { if (selectCode2.options[i].value == "${BasicInfo.nation}") { selectCode2.options[i].selected = true; break; } }参考链接:
nations在Java代码中设置
List<Nation> nations = this.personInfoDetailBasicService.queryNations(); req.setAttribute("nations", nations);
数据库中日期 0000-00-00 异常处理方式
在jdbc连接中添加参数解决
jdbc.url=jdbc:mysql://192.168.1.1:3306/mydb222?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true zeroDateTimeBehavior=round是为了指定MySql中的DateTime字段默认值查询时的处理方式;默认是抛出异常, 对于值为0000-00-00 00:00:00(默认值)的纪录,如下两种配置,会返回不同的结果: zeroDateTimeBehavior=round 0001-01-01 00:00:00.0 zeroDateTimeBehavior=convertToNull null
SSM
controller中接收参数的另一种形式
接收参数的另一种方式
@RequestMapping("/readMessage/{id}") public String ReadMessage(@PathVariable Integer id, Message message, HttpServletRequest request) {
mybatis 分页
mybatis-config.xml文件中配置插件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> //配置PageInterceptor位置 以及数据库类型 <plugins> <plugin interceptor="com.xjd.mybatis.page.PageInterceptor"> <property name="databaseType" value="mysql"/> </plugin> </plugins> </configuration>Dao中的修改
由原来的单参数变为多参数,要用@Param绑定
原:
List<Message> selectAll(Integer loginId);
修改后:
List<Message> selectAll(@Param("loginId") Integer loginId, @Param("page") Pagination page);
controller中对应的修改
//分页 Page<Message> page = new Page<Message>(pageIndex, 6); // 第1页取20条 List<Message> message = null; try { //传入page对象 message = this.messageService.queryAll(loginId, page); page.setResultList(message); // 将结果集传入 //总记录条数 Integer totalCount = page.getTotalCount(); //总页数-totalPage需要在Page实体中加入getter/setter方法 Integer totalPage = page.getTotalPage(); } catch (Exception e) { e.printStackTrace(); }原理;
MyBatis3.2 使用Interceptor进行分页原理: 当Mybatis查询数据的时候,利用Mybatis提供的Interceptor对查询方法进行拦截,动态修改SQL,把不带分页的SQL改造成带分页的SQL 比如在MySql数据库下面 正常查询的SQL 为 selelct id, name from user where name = ? 那么改造后的SQL 为 selelct id, name from user where name = ? limit 5 , 10
参考示例代码以及插件包
百度云
链接:https://pan.baidu.com/s/1IK_gf85Sz_x851n2hNJewA 密码:v78x参考源: