mybatis 传递基本参数和模型参数到mapper.xml中

1.第一种 :基本参数的传递

mapper.java

public interface Some_ModelMapper extends Mapper<Some_Model> {
    public List<Some_Model> selectByMobileAndType(String phone,Integer ordertype);
}

在xml 中获取从mapper接口传过来的参数的值(假设table_name表(Some_Model模型)中有phone和ordertype两个字段)

xml文件

select FROM table_name WHERE  table_name.id=#{0})
    <if test="param1!=null">
     table_name.phone=#{0}
    </if>
    <if test="param2!=null and param2==2">
    table_name.ordertype=#{1}
    </if>

if判断中param1代表第一个参数,在if以外的地方使用#{0}获取第一个参数

2.第二种:mapper接口中直接传模型(model)
mapper.java

public interface Some_Model extends Mapper<Some_Model> {
    public List<Some_Model> select_by_condition(Some_Model some_Model );
}

xml中获取参数值时,在if中直接根据model的属性获取 比如model中有个属性是name,则在if中直接写name即可获取。在if以为则在 #{ } 中获取 ,比如#{name}

xml文件

<select id="select_by_condition" parameterType="model_url.Some_Model " resultMap="BaseResultMap">  
 select * from table_name where table_name.area=#{area}  
 <if test="name !=null and name!=''">  
    and table_name.name like  CONCAT('%',#{name},'%')   
 </if>  

xml中不要使用${} , 存在sql注入漏洞。

猜你喜欢

转载自blog.csdn.net/weixin_38570967/article/details/80474785
今日推荐