当我们查询时,传入的参数是一个对象时(parameterType="POJO具体类"),mybatis存在一个类型处理器(typeHandlers),会自动将数据库的字段和具体类中属性进行匹配,当数据库表格的字段和具体类的属性不一致时,如下图,查询出来的结果,pwd会对应为null,这时候就需要解决了。
解决办法一,直接在sql语句中使用别名查询,如下:该方法粗暴简单
<select id="getUserById" resultType="user" parameterType="int" >
select id,`name`,passward as pwd from school.user where id = #{id}
</select>
解决方法二,使用ResultMap 结果集映照,即将查询的结果先进行映射,在返回对应的类型,对应代码如下,其中select的标签中,mark要对应resultMap中id的mark,表示使用上面的映射关系,type类型为返回对象,
<resultMap id="mark" type="user">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="passward" property="pwd"/>
</resultMap>
<select id="getUserById" resultMap="mark" parameterType="int" >
select id,`name`,passward as pwd from school.user where id = #{id}
</select>
column 表示从数据库中查询到的字段或者别名
property 表示具体类的对应的属性值
上面是Result的简单实用,他的子属性还包含以下几种,需要掌握如下几种:
collection 表示一种集合,常用在一对多场景,表示查询放回的具体类中,有一个属性是集合或者数组,下面会有例子
result 属性是基本数据类型或者string类型时可以使用
association 属性是一个引用类时使用
id 常用在主键上
,
一对多,collection例子
创建以下pojo具体类,同时在数据库创建对应的表,分别时学生表,老师表
编写对应mapper.xml文件以及对应接口调用
//接口调用代码
public interface TeacherMapper {
public Teacher getTeacher(@Param("id") int id);
public Teacher getTeacher2(@Param("id") int id);
}
//mapper.xml
//第一种查询方式,一对多,联表查询,特点resultmap容易书写,sql语句复杂
<resultMap id="gett" type="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
//conllection 里的property对应的也是类中属性名
//oftype对应的集合中的类,ArrayLIst<E>,即对应的E
<collection property="students" ofType="student">
//student类中有三个属性值,一一对应
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
//sql语句比较复杂,需要有别名来区分
<select id="getTeacher" resultMap="gett">
select t.id tid,t.name tname,s.name sname,s.id sid from student s,teacher t
where s.tid=t.id and t.id=#{id}
</select>
//第二种查询方式,子查询,sql语句简单,mapper书写复杂
<select id="getTeacher2" resultMap="gett2">
select * from teacher where id=#{id}
</select>
<resultMap id="gett2" type="teacher">
//javatype对应的属性类型,当不是基本类型或者string类型,就要使用这个
//seletec表示连接的子查询语句,column表示子查询里的参数
<collection property="students" javaType="ArrayList" ofType="Student" select="teacherGetStudent" column="id"/>
</resultMap>
<select id="teacherGetStudent" resultType="student">
select * from student where tid=#{id}
</select>
多对一,例子,对应mapper.xml
<!--联表查询,sql复杂,mapper简单-->
<select id="getStudent2" resultMap="Student2">
select s.id sid, s.name sname, t.name tname from
student s, teacher t
where s.tid=t.id
</select>
<resultMap id="Student2" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--javaType对应的类,属性中如果是类,就要用这个-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
<!--子查询,sql语句简单,mapper复杂-->
<resultMap id="get" type="student">
<!--select表示对应的子查询目标,colum会作为参数传过去-->
<association property="teacher" column="tid" select="getTeacher" javaType="Teacher"/>
</resultMap>
<select id="getStudent" resultMap="get">
select * from student
</select>
<select id="getTeacher" resultType="teacher">
select * from teacher where id=#{id}
</select>