- Single parameter: directly use #{parameter name} to obtain the value, mybatis does not do special processing, and the parameter name can be written casually.
- Multiple parameters: use #{param1}, #{param2} value, or #{args1} #{args2}
- Named parameters: explicitly specify the key in the encapsulation map through @param("key"), and you can retrieve the parameter value through #{key}
- POJO: If multiple parameters happen to be business models, the business model can be passed in at this time, and the value can be obtained through #{attribute name}
- Map: If multiple parameters are not business models and are not frequently used, you can customize the Map to pass in
- TO: If multiple parameters are not business models and are often used, you can customize a TO to transfer objects
1. A single parameter
In the configuration sql mapping file, when a parameter is passed in the sql statement, how should it be passed?
Earlier we tested that mybatis does not do special processing, just use #{parameter name} to take out the parameter value.
Note: In fact, here #{id}, the parameter name id can write any value, because only one parameter is passed. .
<?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" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
<!-- public Hotel getHotel(Integer i); -->
<select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
select id,hotel_name as
hotelName, hotel_address as hotelAddress, price from hotel
where
<!-- 这里#{id}可以写成任何值如 #{abc}-->
id=#{id}
</select>
</mapper>
2. Multiple parameters
First of all, let’s take an example. Of course, it’s easy to think that this is the way to get parameter values and write #{id} and #{price} directly.
<?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" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
<!-- public Hotel getHotel(Integer i,Integer j); -->
<select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
select id,hotel_name as
hotelName, hotel_address as hotelAddress, price from hotel
where
id=#{id} and price =#{price}
</select>
</mapper>
This is wrong, after running it, I found an error
Caused by: org.apache.ibatis.binding.BindingException:
Parameter 'id' not found.
Available parameters are [arg1, arg0, param1, param2]
In fact, when there are multiple parameters, mybatis will do special processing, and multiple parameters will be encapsulated as map
key is param1, param2.....or 0, 1, 2 index value
value is the parameter value
You can experiment again and change the parameter name to #{param1},#{ param2}
<?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" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
<!-- public Hotel getHotel(Integer i,Integer j); -->
<select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
select id,hotel_name as
hotelName, hotel_address as hotelAddress, price from hotel
where
id=#{param1} and price =#{param2}
</select>
</mapper>
It can be found that the result is correct at this time
3. Named parameters
In fact, people are still used to using #{id} and #{price} instead of #{param1} and #{param2}, of course mybatis also provides a solution
By explicitly specifying the key in the encapsulated map by @param("key"), the parameter value can be retrieved by #{key}
@param("key") is written on the hotelMapper interface
package com.pjf.mybatis.dao;
import org.apache.ibatis.annotations.Param;
import com.pjf.mybatis.po.Hotel;
public interface HotelMapper {
public Hotel getHotel(@Param("id")Integer i,@Param("price")Integer j);
}
hotelMapper.xml
<?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" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
<!-- public Hotel getHotel(@Param("id")Integer i,@Param("price")Integer j); -->
<select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
select id,hotel_name as
hotelName, hotel_address as hotelAddress, price from hotel
where
id=#{id} and price =#{price}
</select>
</mapper>
At this time, you can get the parameter value through #{id} and #{price}.
4、map
The above is @param("key") to specify a key, then of course we can also pass in a Map ourselves.
实例:
hotelMapper接口
package com.pjf.mybatis.dao;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.pjf.mybatis.po.Hotel;
public interface HotelMapper {
public Hotel getHotel(Map<String,Integer> map);
}
hotelMapper.xml和上面一样
<?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" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
<!-- public Hotel getHotel(@Param("id")Integer i,@Param("price")Integer j); -->
<select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
select id,hotel_name as
hotelName, hotel_address as hotelAddress, price from hotel
where
id=#{id} and price =#{price}
</select>
</mapper>
public class TestHotel {
public SqlSessionFactory sqlSessionFactory() throws IOException {
// mybatis的配置文件
String resource = "mybatis_config.xml";
// 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)TestHotel.class.getClassLoader()
InputStream is = Resources.getResourceAsStream(resource);
// 构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
return sessionFactory;
}
// 查
@Test
public void getHotel() throws IOException {
SqlSessionFactory sessionFactory = sqlSessionFactory();
SqlSession session = sessionFactory.openSession();
HotelMapper hotelMapper = session.getMapper(HotelMapper.class);
System.out.println(hotelMapper.getClass());
//手动封装一个map传入
Map map =new HashMap<>();
map.put("id", 1001);
map.put("price", 997);
Hotel hotel = hotelMapper.getHotel(map);
System.out.println(hotel);
session.close();
}
}
5. Object parameters (POJO)
If multiple parameters happen to be business models, then you can directly pass them as objects, and you can get the attribute values of the object through #{attribute value}
hotelMapper接口
package com.pjf.mybatis.dao;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.pjf.mybatis.po.Hotel;
public interface HotelMapper {
public Hotel getHotel(Map<String,Integer> map);
//直接传入hotel业务模型
public void insertHotel(Hotel hotel);
}
6. Object parameters (TO)
If multiple parameters are not business models, but are useful more frequently, we can write a TO object specifically to pass parameters
“`
package com.pjf.mybatis.po;
public class HotelTo {
private int id;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package com.pjf.mybatis.dao;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.pjf.mybatis.po.Hotel;
public interface HotelMapper {
public Hotel getHotel(Map