<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
2.1.动态SQL
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace:当前xml文件对应的dao/mapper目录下的接口的全限定名--><mappernamespace="com.mydemo.mapper.UserMapper"><!--
resultType="com.mydemo.domain.User",引用了SqlMapConfig.xml中的别名配置
--><!--SQL片段(相当于将需要重复使用的SQL语句起个“别名”)--><sqlid="selectTable">
SELECT * from user
</sql><!--2.5修改用户信息--><updateid="updateUserSet"parameterType="User">
UPDATE user
<set><iftest="username != null and username !=''">
username = #{username},
</if><iftest="sex != null and sex != ''">
sex = #{sex},
</if><iftest="address !=null and address != ''">
address = #{address},
</if><iftest="birthday != null">
birthday = #{birthday},
</if></set>
WHERE id = #{id}
</update><!--2.4条件查询findUserByForEach--><selectid="findUserByForEach"parameterType="int"resultType="User"><!-- select * from user--><!-- 引用SQL片段
< foreach>标签用于遍历集合,它的属性:
collection:代表要遍历的集合元素,注意编写时不要写#{}
open:代表语句的开始部分
close:代表结束部分
item:代表遍历集合的每个元素,生成的变量名
sperator:代表分隔符
--><includerefid="selectTable"/><foreachcollection="list"item="item"separator=","open="where id in ("close=")">
#{item}
</foreach></select><!-- 2.3条件查询(where)=======√====== --><selectid="findUserByWhere"parameterType="User"resultType="User">
select * from user
<where><choose><whentest="id !=null">
and id = #{id}
</when><otherwise><iftest="username != null and username !=''">
and username like #{username}
</if><iftest="sex != null and sex != ''">
and sex = #{sex}
</if><iftest="address !=null and address != ''">
and address = #{address}
</if></otherwise></choose></where></select><!--2.2条件查询(choose)--><selectid="findUserByChoose"parameterType="User"resultType="User">
select * from user where 1=1
<choose><whentest="id !=null">
and id = #{id}
</when><otherwise><iftest="username != null and username !=''">
and username like #{username}
</if><iftest="sex != null and sex != ''">
and sex = #{sex}
</if><iftest="address !=null and address != ''">
and address = #{address}
</if></otherwise></choose></select><!--2.1条件查询(if)--><selectid="findUserByIf"parameterType="User"resultType="User">
select * from user where 1=1
<iftest="username != null and username !=''">
and username like #{username}
</if><iftest="sex != null and sex != ''">
and sex = #{sex}
</if><iftest="address !=null and address != ''">
and address = #{address}
</if></select><!--3.增--><insertid="addUser"parameterType="User">
INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})
<selectKeykeyColumn="id"keyProperty="id"resultType="java.lang.Integer"order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey></insert></mapper>