resultMap可以实现2个功能:
1.类型转换
2.属性-字段的映射关系
<select id="queryStudentByStuno" parameterType="int" resultMap="studentMapping" >
select * from student where stuno = #{stuno}
</select>
<resultMap type="student" id="studentMapping">
<!-- 分为主键id 和非主键 result-->
<id property="id" column="stuno" />
<result property="stuName" column="stuname" />
<result property="stuAge" column="stuage" />
<result property="graName" column="graname" />
<result property="stuSex" column="stusex" javaType="boolean" jdbcType="INTEGER"/>
</resultMap>
输入参数之parameterType
.类型为 简单类型(8个基本类型+String)#{}、${}的区别
1
#{任意值}
${value} ,其中的标识符只能是value
2
#{}自动给String类型加上'' (自动类型转换)
${} 原样输出,但是适合于 动态排序(动态字段)
例
select stuno,stuname,stuage from student where stuname = #{value}
select stuno,stuname,stuage from student where stuname = '${value}'
动态排序:
select stuno,stuname,stuage from student order by ${value} asc
3
#{}可以防止SQL注入
${}不防止
${}、#{}相同之处:
a.都可以 获取对象的值 (嵌套类型对象)
i.获取对象值:
模糊查询,方式一:
select stuno,stuname,stuage from student where stuage= #{stuAge} or stuname like #{stuName}
Student student = new Student();
student.setStuAge(24);
student.setStuName("%w%");
List<Student> students = studentMapper.queryStudentBystuageOrstuName(student) ;//接口的方法->SQL
模糊查询,方式二:
student.setStuName("w");
select stuno,stuname,stuage from student where stuage= #{stuAge} or stuname like '%${stuName}%'
ii.嵌套类型对象
2.对象类型
#{属性名}
${属性名}
****
也可以级联调用
输入参数之parameterType:“HashMap” 取值时用键取value 用法与传对象一致
**parameterMap:方法已被官网放弃
输出参数resultType
1.简单类型(8个基本+String)
2.输出参数为实体对象类型
3.输出参数为实体对象类型的集合 :虽然输出类型为集合,但是resultType依然写 集合的元素类型(resyltType="Student")
4.输出参数类型为HashMap
--HashMap本身是一个集合,可以存放多个元素,
但是根据提示发现 返回值为HashMap时 ,查询的结果只能是1个学生(no,name);
结论:一个HashMap 对应一个学生的多个元素(多个属性) 【一个map,一个学生】
二维数组
{
{1,zs,23,xa}, -一个HashMap对象
{2,ls,24,bj},
{3,ww,25,tj}
}
resultType
resultMap:实体类的属性、数据表的字段: 类型、名字不同时(stuno,id)
注意:当属性名 和字段名 不一致时,除了使用resultMap以外,还可以使用resultType+HashMap:
resultMap
<resultMap type="student" id="queryStudentByIdMap">
<!-- 指定类中的属性 和 表中的字段 对应关系 -->
<id property="stuNo" column="id" />
<result property="stuName" column="name" />
</resultMap>
resultType+HashMap:返回的是一个属性对应的hashMap(可定义别名 as)如果有多个用List包装 否者 返回0个或1个 其余情况报错
select 表的字段名 "类的属性名" from... 来制定字段名 和属性名的对应关系
<select id="queryStudentByIdWithHashMap" parameterType="int" resultType="student" >
select id "stuNo",name "stuName" from student where id = #{id}
</select>
注意: 如果如果某个字段,但发现 某一个字段结果始终为默认值(0,0.0,null),则可能是 表的字段 和 类的属性名字写错。
条件标签
<where>会自动处理第一个标签中的 and,但不会处理之后中的and
<select id="queryPerByNoOrAwithSQLTag" parameterType="person" resultType="person">
select stuno,stuname,stuage from tb_student where
<where>
<if test="stuName != null and stuName !=''">
and stuname = #{stuName}
</if>
<if test="stuAge != null and stuAge!=''">
and stuage = #{stuAge}
</if>
</where>
</select>
<foreach>迭代的类型:数组、对象数组、集合、属性(Grade类: List ids)
<select id="testForech" resultType="person" parameterType="org.zq.entiey.Gread">
select * from tb_student
<where>
<if test="stuNos != null and stuNos.size > 0">
<foreach collection="stuNos" open="and stuno in (" close=")" item="ss" separator=",">
#{ss}
</foreach>
</if>
</where>
</select>
注意
简单数组 的关键字是array 代替 如果是集合 统一用 list 用法一致
<select id="testForechWithArry" resultType="Person" parameterType="int[]">
select * from tb_student
<where>
<if test=" array != null and array.length > 0">
<foreach collection="array" open="and stuno in (" close=")" item="ss" separator=",">
#{ss}
</foreach>
</if>
</where>
</select>
如果是对象数组 则统一使用object[]
<select id="testForechWithObjectArry" resultType="person" parameterType="Object[]">
select *
from tb_student
<where>
<if test="array != null and array.length >0">
<foreach collection="array" open=" and stuno in (" close=")" separator="," item="stu">
#{stu.stuNo}
</foreach>
</if>
</where>
</select>
Sql片段
java:方法
数据库:存储过程、存储函数
Mybatis :SQL片段
a.提取相似代码
b.引用
提取相同的代码 减少代码 如果不在同一个 mapper 则需要加上 namespace
<sql id="objectArray">
<where>
<if test=" array != null and array.length > 0">
<foreach collection="array" open="and stuno in (" close=")" item="ss" separator=",">
#{ss}
</foreach>
</if>
</where>
</sql>
<!--使用-->
<select id="mode" resultType="Person" parameterType="String[]">
select *
from tb_student
<include refid="objectArray"></include>
</select>