mybatis的学习(三)
1.sql标签
这个标签可以用来定义可重复使用的sql语句,可以通过include标签引用到其他sql语句里。
例:
<mapper namespace="lesson03.sql.FoodMapper">
<sql id="selectSql">
select * from
</sql>
<select id="queryFood" resultType="java.util.Map">
<include refid="selectSql"></include>
food
</select>
</mapper>
2.结果集映射
结果集映射:数据库的列名和实体类的属性名如果一致,需要建立列名和属性名的映射关系,建立后就不能使用resultType了,需要使用resultMap。
建立方法:mapper.xml(映射文件)添加resultMap标签
<!-- type类 类型 idresultmap的唯一标识符 automapping true代表属性名同列名一样则自动映射,反则相反 -->
<resultMap type="grade" id="gradeMap" autoMapping="false">
<!-- 映射主键列 -->
<id column="gid" property="gid"/>
<!-- 映射普通列 -->
<result column="gname" property="gname1"/>
</resultMap>
3.关系型结果映射
当一个类中有一个或多个属性,数据库不存在相应的列名,并且这些属性可以关联到另一个对象或者对象集合,那么就可以使用关系型结果映射。
建立方法:大体同结果集相同,但是还要加一个标签association(映射对象)或者是collection(映射集合),取决类的属性,然后在这些标签中加入select属性,用来指向映射的方法。
主要代码:
<resultMap type="grade" id="gradeTwo" autoMapping="true">
<!-- 映射主键列 -->
<id column="gid" property="gid"/> <result column="gname" property="gname1"/>
<collection column="gid" property="stu" select="lesson03.resultMap.StudentMapper.queryStudentByGid"></collection>
</resultMap>
<resultMap type="student" id="studentMap" autoMapping="true">
<!-- 映射主键列 -->
<id column="sid" property="sid"/>
<association column="gid" property="g" select="lesson03.resultMap.GradeMapper.queryGradeById"></association>
</resultMap>
4.动态sql
If:用来判断条件是否满足,如果满足就把if中的sql拼接到主sql,如果有条件where标签自动加where。
<select id="queryStudent" resultType="student">
select * from student
<where>
<if test="sanme!=null">
and sname like '%${sname}%'
</if>
</where>
</select>
Choose、when、otherwise:类似于switch case othe,当满足when中的条件是满足时将sql语句拼接到主sql,否则拼接otherwise中的sql。
<select id="queryStudentById" resultType="student">
select * from student wehre 1=1
<choose>
<when test="sid!=null">
and sex=#{0}
</when>
<otherwise>
and sex=1
</otherwise>
</choose>
</select>
Trim:替换前缀、后缀
Prefix 要替换的内容 prefixOverrides被替换的内容(前缀)
SuffixOverrides要替换的内容(后缀) suffix被替换的内容
<select id="queryStudent" resultType="student">
select * from student
<trim prefix="where" prefixOverrides="and"> <if test="sanme!=null">
and sname like '%${sname}%'
</if>
<if test="sanme!=null">
and sid=#{sid}
</if>
</trim>
</where>
</select>
Set:添加set,清除多余的逗号,自动加where
<update id="updateStudent">
update student
<set>
<if test="sanme!=null">
sname=#{sname},
</if>
</set>
</update>
Foreach:
Item定义临时变量
Collection接收要遍历的数据
Open 第一次遍历前拼接
Close 遍历完后拼接
Separator 每次遍历完添加的值,最后一次不添加
<select id="queryAll" resultType="student">
select * from student
<foreach item="i" collection="studentList" open=" gid in(" close=")" separator=",">
${i}
</foreach>
</select>
补充:
注解使用动态sql
1.在注解中使用script标签将sql包裹,使用动态sql就会被解析
2.使用select(update、insert、delet)Provider指定类,type指定类method指定方法,在方法中使用java代码实现sql拼接(方法的形参是Map类型,${}不适用,取值:别名.属性名)
3.在2的基础上创建SQL 对象,追加sql,再追加过程中可以使用动态sql(以方法的形式呈现,例如:AND()、WHERE())
class UpdatStudent{
public String updateStudent(Map map){
SQL sql = new SQL();
Student stu = (Student) map.get("student");
sql.UPDATE("student").SET("sname=#{student.sname}");
if(stu.getSid()!=0){
sql.WHERE("sid=#{student.sid}");
}
return sql.toString();
}
}
@UpdateProvider(type=UpdatStudent.class,method="updateStudent")
public void updateStudent(@Param("student") Student student);