1:当实体类中的属性名和表中的字段名不一致时?
使用MyBatis进行查询操作时无法查询出相应的结果的问题以及针对问题采用的两种办法:方法一: 通过在查询的sql语句中定义字段名的别名
让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上了,这种方式是通过在sql语句中定义别名来解决字段名和属性名的映射关系的。方法二: 通过<resultMap>来映射字段名和实体类属性名的一一对应关系
这种方式是使用MyBatis提供的解决方式来解决字段名和属性名的映射关系的
例:
<mapper namespace="me.mybat.mapping.orderMapper">
<select id="selectOrder" parameterType="int"
resultType="Order">
select order_id id, order_no orderNo,order_price price from orders where order_id=#{id}
</select>
<!--
根据id查询得到一个order对象,使用这个查询是可以正常查询到我们想要的结果的,
这是因为我们通过<resultMap>映射实体类属性名和表的字段名一一对应关系
-->
<select id="selectOrderResultMap" parameterType="int" resultMap="orderResultMap">
select * from orders where order_id=#{id}
</select>
<resultMap type="Order" id="orderResultMap">
<!-- 用id属性来映射主键字段 -->
<id property="id" column="order_id"/>
<!-- 用result属性来映射非主键字段 -->
<result property="orderNo" column="order_no"/>
<result property="price" column="order_price"/>
</resultMap>
</mapper>
2:为实体类定义别名,简化sql映射xml文件中的引用
之前,我们在sql映射xml文件中的引用实体类时,需要写上实体类的全类名(包名+类名),如下:<!-- 创建用户(Create) -->
<insert id="addUser" parameterType="me.mybat.entity.User">
insert into users(name,age) values(#{name},#{age})
</insert>
每次都写这么一长串内容挺麻烦的,而我们希望能够简写:
(1)单独为某一个实体类设置别名
在conf.xml文件中<configuration></configuration>标签中添加如下配置:
<typeAliases>
<typeAlias type="me.mybat.entity.User" alias="_User"/>
</typeAliases>
(2)批量为某个包下的所有实体类设置别名
<!-- 配置实体类的别名,配置实体类别名的目的是为了在引用实体类时可以使用实体类的别名来代替实体类,达到简写的目的 -->
<typeAliases>
<!-- 为me.mybat.entity包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名
比如me.mybat.entity.User这个实体类的别名就会被设置成User
-->
<package name="me.mybat.entity"/>
</typeAliases>
3:MyBatis的返回参数类型 resultMap和resultType 区别
返回值类型:
resultMap : 结果集[对象等];适合使用返回值是自定义实体类的情况
resultType : Integer,String ,Long ,class类;合使用返回值得数据类型是非自定义的
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
例:班级是一个类,班级类里添加学生类,老是类集合
方式一: 使用嵌套结果映射来处理重复的联合结果的子集
SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND c.c_id=1
<select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and c.c_id=#{id}
</select>
<resultMap type="me.mybat.entity.Classes" id="ClassResultMap3">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="me.mybat.entity.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<!-- ofType指定students集合中的对象类型 -->
<collection property="students" ofType="me.mybat.entity.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值
SELECT * FROM student WHERE class_id=1 //1是第一个查询得到的c_id字段的值
<select id="getClass4" parameterType="int" resultMap="ClassResultMap4">
select * from class where c_id=#{id}
</select>
<resultMap type="Classes" id="ClassResultMap4">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"></association>
<collection property="students" ofType="Student" column="c_id" select="getStudent"></collection>
</resultMap>
SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
</select>
<select id="getStudent" parameterType="int" resultType="Student">
SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
</select>
4:MyBatis的传入参数parameterType类型
MyBatis的传入参数parameterType类型分两种基本数据类型:int,string,long,Date;
复杂数据类型:类和Map
注:不同版本的MyBatis对基本类型传递过来的参数名称不能识别,要使用_parameter来代替。