- 单个参数查询
在mapper.xml配置文件中配置
<select id= "selectByNu" parameterType ="java.lang.String"
resultMap="BaseResultMap" >
select
<include refid= "Base_Column_List" />
from consume_number
where consume = #{consume,jdbcType=VARCHAR}
</select >
在接口方法中
ConsumeNumber selectByNu(String consume);
- 直接传递多参数 查询
在mapper.xml配置文件中配置 ,其中传递参数在mybatis的配置文件中可以直接使用角标来获取<select id= "selectByUserNameAndPwd" resultMap="BaseResultMap" >select<include refid= "Base_Column_List" />from member_managerWHEREusername = #{0}ANDpassword = #{1}</select>在dao中代码MemberManager selectByUserNameAndPwd(String username ,String password);如果想要在在mybatis中#{}中不使用角标获取也可以是在传递的时候在参数上加上注解这样的话就可以在mapper.xml中直接添加使用名字在xml配置文件中
- <select id="selectTeacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
在接口方法中public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
- 使用query对象封装查询
在mapper.xml配置文件中配置
<!-- 自定义sql -->
<select id="selectByQuery" parameterType="com.laiwifi.query.FollowerQuery"
resultMap="BaseResultMap" >
select
<include refid= "Base_Column_List" />
from pgy_follower
where 1=1
<if test= "openid != null and openid != '' " >
and openid = #{openid ,jdbcType=VARCHAR}
</if>
<if test= "next != null and next != '' " >
and next = #{next,jdbcType=VARCHAR}
</if>
<if test= "pre != null and pre != '' " >
and pre = #{pre ,jdbcType=VARCHAR}
</if>
</select >
在接口方法中
PgyFollower selectByQuery(FollowerQuery query);
封装的对象
public class FollowerQuery {
private String openid;
private String next;
private String pre;
public String getOpenid() {
return openid ;
}
public void setOpenid(String openid ) {
this.openid = openid ;
}
public String getNext() {
return next ;
}
public void setNext(String next ) {
this.next = next ;
}
public String getPre() {
return pre ;
}
public void setPre(String pre ) {
this.pre = pre ;
}
}
- Map封装多参数
在mapper.xml中
public list<XXBean> getXXXBeanList(HashMap map);<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">select 字段... from XXX where id=#{xxId} code = #{xxCode}</select>其中hashmap是mybaits默认存在的,可直接使用。map中key的名字是在#{}中使用的在接口方法中
- list封装参数
在mapper.xml中配置
在接口方法中<select id="getXXXBeanList" resultType="XXBean">select 字段... from XXX where id in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach></select>foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
public List<XXBean> getXXBeanList(list(String) list);