一.Mybatis 極簡入門
二.Mybatis 級聯查詢
三.Mybatis 延遲加載
四.Mybatis 緩存策略
五.Mybatis 動態SQL
本篇:Mybatis 多參處理
- 一個參數怎麼處理?
<select id="findClassedByID" parameterType="long" resultType="com.ibuyi.mybatis.entity.Classes">
select * from classes where id=#{hello}
</select>
當只有一個參數時,#{}裏面可以任意填寫都能夠取到傳遞進來的參數。
- 兩個或多個參數怎麼處理?
import com.ibuyi.mybatis.entity.User;
public interface UserDAO {
User findUserByCityAndType(String city,int Type);
}
第一種使用param1,param2…或arg0,arg1…
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
select * from user where location=#{param1} and type=#{param2}
</select>
第二種使用Map,傳入一個Map即可
public interface UserDAO {
User findUserByCityAndType(Map<String,Object> map);
}
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
select * from user where location=#{location} and type=#{type}
</select>
第三種使用註解:
public interface UserDAO {
User findUserByCityAndType(@Param("location") String location,@Param("type") int type);
}
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
select * from user where location=#{location} and type=#{type}
</select>
如果我們使用Collection或者List或者array傳遞參數,應該怎麼取出來呢?
public interface UserDAO {
User findUserByCityAndType(List<Integer> list);
}
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
select * from user where id=#{list[0]}
</select>
public interface UserDAO {
User findUserByCityAndType(int[] ids);
}
<select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
select * from user where id=#{array[0]}
</select>