Mybatis中Mapper.xml的参数接受方式

1.使用#{arg}接受

mapper接口

public interface FoodMapper {
    public int add(String foodName,Integer foodTypeId);
}

mapper.xml实现

<insert id="add">
    insert into foods(foodsName,foodsTypeId) values(#{arg0},#{arg1})
insert>

2.使用#{param}接受

mapper接口

public interface FoodMapper {
    public int add(String foodName,Integer foodTypeId);
}

mapper.xml实现

<insert id="add">
    insert into foods(foodsName,foodsTypeId) values(#{param1},#{param2})
insert>

3.使用@Param注解

mapper接口

public interface FoodMapper {
    public int add(@Param(foodName)String foodName,@Param(foodTypeId)Integer foodTypeId);
}

mapper.xml实现

<insert id="add">
    insert into foods(foodsName,foodsTypeId) values(#{foodName},#{foodTypeId})
insert>

4.使用Map

mapper接口

public interface FoodMapper {
    public int add(Map m);
}
public void test(){
	Map m = new HashMap<>();
	m.put("key1",foodName);
	m.put("key2",foodTypeId);
	FoodMapper mapper = new FoodMapper();
	mapper.add(m);
}

mapper.xml实现

<insert id="add" parameterType="java.util.Map">
    insert into foods(foodsName,foodsTypeId) values(#{key1},#{key2})
insert>

5.使用实体类对象

public interface FoodMapper {
    public int add(Food food);
}

mapper.xml实现

<insert id="add" parameterType="com.bean.Food">
	
    insert into foods(foodsName,foodsTypeId) values(#{foodName},#{foodTypeId})
insert>

你可能感兴趣的:(mybatis,xml,java)