新建学生表和学生证表
--学生表 CREATE TABLE student( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'id', `name` VARCHAR(20) NOT NULL COMMENT '姓名', `age` INT NOT NULL COMMENT '年龄', sex INT NOT NULL COMMENT '性别 1 男 0 女', cid INT NOT NULL COMMENT '班级id', cardId INT COMMENT '学生证id' ) --学生证表 CREATE TABLE Card ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'id', number INT NOT NULL COMMENT '学生证id', studentId INT NOT NULL COMMENT '学生id' )
动态标签
include&sql
实现sql复用
<sql id="columns"> id,name,age </sql> <!--useCache 表示使用缓存 --> <select id="selectOne" parameterType="string" flushCache="false" useCache="true" resultMap="studentAndClassMap"> select <include refid="columns"/> from student <!--模拟if标签 --> <if test="id !=null and id!=''"> where id=#{id} </if> </select>
where&if
<select id="selectOne" parameterType="string" flushCache="false" useCache="true" resultMap="studentAndClassMap"> select <include refid="columns"/> from student <where> <if test="id !=null and id!=''"> and id=#{id} </if>
<if test="name !=null and name!=''">
and name=#{name}
</if> </where> </select>
where标签里面有if标签成立 则自动在sql后面拼接where 同时去除多余的and
choose, when, otherwise
<select id="find" parameterType="map" resultType="student"> select * from student <!-- if else if else --> <where> <choose> <when test="sex != null"> and sex=#{sex} </when> <when test="name != null and name != ''"> and name like concat('%',#{name},'%') </when> <!-- <otherwise> ... </otherwise> --> </choose> </where> </select>
trim, set
select * from user <trim prefix="WHERE" prefixoverride="AND |OR"> <if test="name != null and name.length()>0"> AND name=#{name}</if> <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if> </trim>
满足条件拼接 where 同时去掉前面多余的and或者or
update user <trim prefix="set" suffixoverride="," suffix=" where id = #{id} "> <if test="name != null and name.length()>0"> name=#{name} , </if> <if test="gender != null and gender.length()>0"> gender=#{gender} , </if> </trim>
满足条件拼接set 同时去掉后面多余的, 再拼接 suffix
update user <set> <if test="name != null and name.length()>0"> name=#{name} , </if> <if test="gender != null and gender.length()>0"> gender=#{gender} , </if> </set>
满足条件拼接set 同时去掉多余的,
一对多级联
1.学生mapper增加一个根据班级获取学生
<select id="findByClassesId" parameterType="int" resultType="student"> select * from student where cid=#{id} </select>
public List<Student> findByStudent(Student params);
2.在classes类增加一个学生集合
public class Classes implements Serializable{
....
private List<Student> students;
}
2.classesMapper增加自定义resultMap
<resultMap id="classVoMap" type="com.liqiang.vo.ClassesVo"> <id property="id" column="id" /> <result property="name" column="name" /> <collection property="students" column="id" select="com.liqiang.mapper.StudentMapper.findByClassesId"></collection> </resultMap>
select为调用指定mapperStatement(可以理解是指定key的标签namespace+id)
column 为将哪个列作为参数传递
students 返回结果赋值的属性
可以设置 fetchType="eager(默认值 不延迟加载)|lazy" 是否延迟加载的意思
3.在需要使用resultMap的地方指定resultMap 如根据id获得class
<select id="findById" parameterType="Integer" resultMap="classVoMap"> select * from classes where id=#{id} </select>
多对一, 一对一级联
1.学生表增加classes
public class Student implements Serializable{
.... private Classes classes;
}
2.学生表增加自定义resultMap
<resultMap id="studentAndClassMap" type="student" >
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="cid" column="cid" />
<association property="classes" column="cid"
select="com.liqiang.mapper.ClassesMapper.findById"></association>
</resultMap>
3.在需要级联的地方resultMap改为这个
<!--useCache 表示使用缓存 --> <select id="selectOne" parameterType="string" flushCache="false" useCache="true" resultMap="studentAndClassMap"> select * from student <!--模拟if标签 --> <if test="id !=null and id!=''"> where id=#{id} </if> </select>
跟多对一一样 只是标签改为association
resultMap或者ParameterMap继承
<resultMap id="studentMap" type="studentVo"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="cid" column="cid" /> <association property="card" column="id" select="com.liqiang.mapper.CardMapper.findByStudentId"></association> </resultMap> <resultMap id="studentAndClassMap" type="student" > <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="cid" column="cid" /> <association property="classes" column="cid" select="com.liqiang.mapper.ClassesMapper.findById"></association> </resultMap>
如我定义了2个Resultmap 一个需要class级联 一个只需要学生证级联,是否发现前面几个属性几乎是copy的
我们可以这样
<resultMap id="simpleType" type="student"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="cid" column="cid" /> </resultMap> <resultMap id="studentMap" type="student" extends="simpleType"> <association property="card" column="id" select="com.liqiang.mapper.CardMapper.findByStudentId"></association> </resultMap> <resultMap id="studentAndClassMap" type="student" extends="simpleType" > <association property="classes" column="cid" select="com.liqiang.mapper.ClassesMapper.findById"></association> </resultMap>
使用extends="simpleType" 继承