if 判定标签:
<select id="queryUser" resultType="User">
select * from t_user /*where 1 = 1 恒等式效率较低*/
/*
if 判定标签
test 属性中: 定义判定条件
可以直接根据@Param() 中定义的参数名匹配方法的实参
*/
/*判断用户名是否不为null并且也不为空串 */
/*
where:
如果标签where标签对中存在条件,拼接一个where关键字
如果标签对中没有条件,就不拼接where关键字
如果存在条件,会帮你把第一个条件前面的and关键字去掉
*/
<where>
<if test="username!=null and username!=''">
and username = #{username}
</if>
<if test="password!=null and password!=0">
and password = #{password}
</if>
</where>
</select>
set标签:
<!--修改-->
<!--
set标签
1.如果存在条件,就拼接set关键字
2.如果不存在条件,就不拼接set关键字
3.如果存在条件,帮你删除最后一个条件的,
-->
<update id="updateUser">
update t_user
<set>
id = #{id}
<if test="username != '' and username != null">
username = #{username},
</if>
<if test="password != 0 and password != null">
password = #{password},
</if>
</set>
where id = #{id}
</update>
choose...when...otherwise:
<update id="updateUser2">
update t_user
<set>
<choose> <!--switch-->
<when test="username != '' and username != null"> <!--case-->
username = #{username},
</when>
<when test="password != 0 and password != null">
password = #{password},
</when>
<otherwise> <!--default-->
id = #{id}
</otherwise>
</choose>
</set>
where id = #{id}
</update>
trim代替set:
<update id="updateUser3">
update t_user
<trim prefix="set" suffixOverrides=",">
<choose> <!--switch-->
<when test="username != '' and username != null"> <!--case-->
username = #{username},
</when>
<when test="password != 0 and password != null">
password = #{password},
</when>
<otherwise> <!--default-->
id = #{id}
</otherwise>
</choose>
</trim>
where id = #{id}
</update>
模糊查询:
<!--根据用户名模糊查询用户信息-->
<select id="queryUserByNameLike" parameterType="string" resultType="user">
<!--select * from t_user where username like '%'|| #{str} ||'%'-->
<bind name="str" value="'%' + str + '%'" />
select * from t_user where username like #{str}
</select>
自定义映射关系:
<!--自定义映射关系-->
<resultMap id="myResultMap" type="Emp">
<!--主键字段-->
<id column="empno" property="no"></id>
<!--非主键字段-->
<result column="ename" property="name"></result>
</resultMap>
<select id="queryAll" resultType="emp" resultMap="myResultMap">
<!-- select empno no,ename name,job,mgr,sal,comm,hiredate,deptno from emp-->
select empno,ename,job,mgr,sal,comm,hiredate,deptno from emp
</select>
/*
查询用户信息
查询所有得到用户信息
根据用户名查询
根据用户密码
根据用户名和用户密码同时查询用户信息
*/
public List<User> queryUser(@Param("username") String username,@Param("password") Integer password);
/*
修改数据根据id修改用户数据
根据id修改用户名
修改用密码
修改用户名和用户密码
*/
int updateUser(@Param("id")Integer id,@Param("username") String username,@Param("password") Integer password);
/*
修改数据
根据id修改用户数据
如果存在用户名就根据修改用户名
用户名不存在,就修改用户密码
两个都不存在就不修改
*/
int updateUser2(@Param("id")Integer id,@Param("username") String username,@Param("password") Integer password);
int updateUser3(@Param("id")Integer id,@Param("username") String username,@Param("password") Integer password);
// 根据用户名模糊查询用户信息
public List<User> queryUserByNameLike(@Param("str") String str);
多表联查:
关系映射查询
多表联查: 1) 中间类 2) resultMap
一对一|多对一 : 查询所有的学生信息以及所在的班级信息
代码: 一端类中存在另一个类型的成员 学生类中存在班级类型的成员 | 员工类型中存在部门类型的成员 多的一方存在一的一方的成员
resultMap
一对多 : 查询所有的班级以及班级中的学生信息
代码: 在一的一方存在多的一方的List集合 班级类中存在一个成员List<学生>
resultMap
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
<!--自定义映射关系 : resultMap手动映射 所有的属性与字段都需要手动设置关联关系-->
<resultMap id="myResultMap" type="Emp">
<!--主键字段-->
<id column="empno" property="empno"/>
<!--非主键字段-->
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<result column="sal" property="sal"/>
<result column="comm" property="comm"/>
<result column="mgr" property="mgr"/>
<result column="hiredate" property="hiredate"/>
<result column="deptno" property="deptno"/>
<!--属性为自定义引用数据类型的关联设置-->
<association property="dept" javaType="Dept">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
</association>
</resultMap>
<select id="queryAll" resultType="emp" resultMap="myResultMap">
select empno,ename,job,mgr,sal,comm,hiredate,emp.deptno,dname,loc from emp left join dept on emp.deptno = dept.deptno
</select>