你的在映射文件中需要传递参数
eg:
<insert id="add">
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
里面的#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}就是我们需要传递的值。
对于这种传参的方式有三种。分别需要在Mapper接口处,Mapper核心配置文件和Mapper的映射文件中进行修改
1.使用一个类来封装
如使用
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
private Integer status;
}
这个Brand类来讲需要传递的参数进行实体化。
然后在Mapper接口处的方法这样写
List<Brand> insert(Brand brand);
2.使用Map来进行分装。
需要在mybatis核心配置文件中建立一个Map集合,然后将需要传递的值分别一一加入其中。
Map map=new HashMap();
map.put("status",status);
map.put("companyName",companyName);
map.put("brandName",brandName);
再在Mapper接口的方法,将上述方法一的接口方法传递的参数Brand brand改为Map map即可。
3.使用@Pararm("")
将Mapper核心配置文件传递的参数一一传递过来即可。
但是需注意在Mapper的接口的方法传参地方需要修改。
List<Brand> selectByCondiction(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName );
其中@Pararm("")中的字符得
<select id="selectByCondiction" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName !=null and companyName !=''">
and company_name like #{companyName}
</if>
<if test="brandName !=null and brandName !=''">
and brand_name like #{brandName};
</if>
</where>
</select>
和Mapper的映射文件xml里面所写的SQL语句中的#{brandName}, #{companyName}, #{status}内的变量名一样,否则会出现内传,从而在数据库里面出现null。或者报错。
因为我这里用的是动态的查询方式,所以漏传后也不会进行报错。