mybatis之动态sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--<1&#45;&#45;namespace与mapper接口的全类名保持一致,-->
<mapper namespace="com.sunyu.mapper.EmpMapper">
    <!--    Student selectStudent(@Param("studentName") String studentName);-->
    <resultMap id="getEmpInforResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="emp_age" property="empAge"></result>
    </resultMap>
<select id="getEmpInfor" resultMap="getEmpInforResultMap">
    select * from t_emp
    <where>
 <if test="empName != null and empName != ''">
   and  emp_name = #{empName}
 </if>
 <if test="empAge != null and empAge != ''">
   and   emp_age = #{empAge}
 </if>
    </where>
</select>


<!--    Emp selecet(@Param("empName") String empName);-->
    <select id="selecetemp" resultMap="getEmpInforResultMap">
        select * from t_emp where emp_name = #{empName}
    </select>
</mapper>

遇到的问题:找不到emp_name在Emp.class文件中的位置

原因是在if标签中吧empName写成数据库中的emp_name 

if标签中的内容是前端勾选之后传给后端需要查找什么,然后判断它如果不为空就执行sql从数据库中查询出来,所以if标签后应该写的是实体类的属性而不是数据库中的字段。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值