mybatis的paramType
MyBatis传多个参数: 1、采用#{0},#{1}获得参数: Dao层函数方法: public User selectUser(String name,String area); 对应的Mapper.xml select * from user where user user_name=#{0} and user_area=#{1} 2、采用Map传递参数: Dao层的函数方法: public User selectUser(Map paramMap); 对应的Mapper.xml select * from user where user_name=#{userName,jdbcType=VARCHAR} and user_area = #{userArea,jdbctType=VARCHAR} Service层调用: private User xxSelectUser(){ Map paramMap = new HashMap(); paramMap.put("userName","xxx"); paramMap.put("userArea","xxxx"); User user = xxx.selectUser(paramMap); } 3、采用@param("name") String name Dao层的函数方法: public User selectUser(@param("userName") String name,@param("userArea") String area); 对应的Mapper.xml select * from user where user_name=#{userName,jdbcType=VARCHAR} and user_area = #{userArea,jdbcType=VARCHAR} MyBatis传入参数与ParameterType: 1、简单数据类型 mapper接口方法: User selectByPrimaryKey(Integer id); sql映射: select from user where id = #{id,jdbcType=INTEGER} select from tb_user where id = #{id,jdbcType=INTEGER} 2、对象类型: mapper 接口方法: int insert(User user); sql映射: insert into tb_user (name, sex) values (#{name,jdbcType=CHAR}, #{sex,jdbcType=CHAR}) 3、map类型 传入map类型,直接通过#{keyname}就可以引用到键对应的值。使用@param注释的多个参数值也会组装成一个map数据结构,和直接传递map进来没有区别。 mapper接口: int updateByExample(@Param("user") User user, @Param("example") UserExample example); sql映射: update tb_user set id = #{user.id,jdbcType=INTEGER}, ... 4、集合类型: 集合类型通常用于构造IN条件,sql映射文件中使用foreach元素来遍历List或Array元素。 mapper接口: User selectUserInList(List idlist); sql映射文件: SELECT * FROM USER WHERE ID in #{item} 5、对象类型中的集合属性: 对于单独传递的List或Array,在SQL映射文件中映射时,只能通过list或array来引用。但是如果对象类型有属性的类型为List或Array,则在sql映射文件的foreach元素中,可以直接使用属性名字来引用。 mapper接口: List selectByExample(UserExample example); sql映射文件: 在这里,UserExample有一个属性叫oredCriteria,其类型为List,所以在foreach元素里直接用属性名oredCriteria引用这个List即可。item="criteria"表示使用criteria这个名字引用每一个集合中的每一个List或Array元素