映射器
- 半自动化的体现:配置 SQL 语句,体现了半自动化和灵活性
- ORM的体现:对象关系映射的实现,数据库表和 POJO 类的映射关系
映射器的引入、接口
映射器的组成
select元素(多参传递)
转义字符代替
-
Mapper.java
// 找到学生编号小于某个sid的学生 public List<Student> selectStuBiggerSid(int sid);
-
Mapper.xml
<select id="selectStuBiggerSid" resultType="student" parameterType="int"> select * from student where sid < #{v} </select>
select语句中的小于号(<)会被当成是select标签的<,所以select中小于号必须要用转义字符"<;"代替,其余不影响
多个参数传递
-
javabean
推荐使用JavaBean的方式进行多参传递
-
Map
缺点:耦合度高,对其他层有影响,不推荐使用
-
param1 param2 param3 …(新版本):从1开始
Mapper.java
// 找到某性别的学生分页 // param1 param2 param3 ... public List<Student> selectStuPageBySex(String sex,int curpage,int sizepage);
Mapper.xml
<select id="selectStuPageBySex" resultType="student"> select * from student where ssex=#{param1} limit #{param2},#{param3} </select>
-
arg0 arg1 arg2 …(新版本):从0开始
Mapper.java
// 找到某性别的学生分页 // arg0 arg1 arg2 ... public List<Student> selectStuPageBySsex(String sex,int curpage,int sizepage);
Mapper.xml
<select id="selectStuPageBySsex" resultType="student"> select * from student where ssex=#{arg0} limit #{arg1},#{arg2} </select>
注意:方式3和方式4不能混合使用
insert元素(主键回填)
-
主键回填
当主键在数据库中为自增字段时,新增成功后,回填主键
insert插入数据时,当事务没提交,主键也被占用掉了,这样会造成根本不能查到本次插入主键,所以必须用主键回填的方式获取获取主键
<insert id="insertStu" parameterType="Student" keyProperty="sid" useGeneratedKeys="true" > insert into student(sname,birthday,ssex,classid) values(#{sname},#{birthday},#{ssex},#{classid}) </insert>
以上案例将新增后的将主键回填给student的sid属性
update和delete元素
-
update
-
delete
resultMap元素
resultMap的作用
当字段和属性名字不相同时,映射不上,导致查询拿不到值
解决方法:1:字段起别名,保持跟属性名一致
2:resultMap
单表可以只写映射不上的
对于主键字段来说可以使用id 也可以使用 result
-
定义映射规则
ORM 的特性,POJO 类和数据库的映射关系
-
级联操作
多表存在主外键关系时,主表和从表之间的关联操作
-
类型转换
数据库字段的类型和 POJO 类属性的类型转换
resultMap元素结构
<resultMap type="SMaster" id="aaa">
<id column="smid" property="smid" />
<result column="sm_name" property="smname" />
<result column="smsex" property="smsex" />
</resultMap>
<select id="selectAll" resultMap="aaa" >
select * from schoolmaster
</select>
MySQL中的字段名是sm_name,实体类中的属性名是smname
对于主键字段来说可以使用id 也可以使用 result
单表中可以只写映射不上的
多表联查
级联
级联(cascade),是指多个对象之间的映射关系,建立数据之间的级联关系提高管理效率
-
一对一
一个对象对应唯一的对象,举例:中国公民和身份证
-
一对多
一个对象对应多个对象,举例:班级和学生;
-
多对多
多个对象对应多个对象,举例:公司角色和公司员工
一对一
Mapper.xml
<resultMap type="Student" id="stu_class_Map">
<result column="sid" property="sid" />
<result column="sname" property="sname" />
<result column="birthday" property="birthday" />
<result column="ssex" property="ssex" />
<result column="classid" property="classid" />
<association property="bj">
<result column="classid" property="classid" />
<result column="classname" property="classname"/>
</association>
</resultMap>
<select id="findAllStudentAndClass" resultMap="stu_class_Map">
select * from student left join class on student.classid =class.classid
</select>
一对一使用association
一对多
<resultMap type="Banji" id="banji_class_Map">
<result column="classid" property="classid" />
<result column="classname" property="classname" />
<collection property="slist" ofType="Student">
<result column="sid" property="sid" />
<result column="sname" property="sname" />
<result column="birthday" property="birthday" />
<result column="ssex" property="ssex" />
<result column="classid" property="classid" />
</collection>
</resultMap>
<select id="findBanjiAndStu" resultMap="banji_class_Map">
select * from student left join class on student.classid =class.classid
</select>
一对多使用collection
级联的缺陷
-
性能缺陷
级联操作会降低性能,增加程序的执行时间
-
复杂度缺陷
关联较多造成复杂度的增加,不利于他人的理解和维护
使用建议:
- 根据实际情况增加级联关系
- 多层关联式,建议超过三层关联时尽量少用级联