mybatis——连级查询、调用存储过程、返回为Map查询(三)

一.连级查询

当有引用其他类中属性的时候可以进行连级查询

<select id="selectAllByAddress" parameterType="Person" resultType="Person">
     select * from person where homeaddress = #{address.homeAddress} or studentaddress = '${address.studentAddress}'
</select>

二.调用存储过程

在mysql创建存储过程 然后可以直接调用 statementType要改成CALLABLE IN

select id="queryCountBySex" resultType="int" parameterType="HashMap" statementType="CALLABLE">
  {call selectPersonBySex3(#{p_sex,jdbcType=VARCHAR,mode=IN},#{p_out,jdbcType=INTEGER,mode=OUT})}
</select>

测试方法:

HashMap<String, Object> input = new HashMap<>();
input.put("p_sex","男");
mapper.queryCountBySex(input);
Object p_out = input.get("p_out");//返回的直接是p_out
System.out.println(p_out);

三.返回为Map查询

分装的key值就是查询""中的字符串

<!--返回单条记录-->
<select id="queryHashMap" resultType="HashMap" parameterType="String">
    select id "id", name "name" from person where name like '%${value}%'
</select>
<!--返回的map封装在list里面返回多条-->
<select id="queryHashMapAll" resultType="HashMap" parameterType="String">
    select id "id", name "name" from person
</select>
<!--查询单条记录可以直接封装 也可以直接分装在类里面-->
    <select id="queryHashMapAll" resultType="Person" parameterType="String">
    select id "id", name "name" from person where name = #{value}
</select>

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/87937579