规范:
XML映射文件的名称和Mapper接口名称一样(同包同名)注意:不能直接用.创建文件夹,用/分层
xml映射文件的namespace属性为mapper接口全限定名一致
xml映射文件的sql语句和mapper接口的方法名一致,并保持返回类型一致
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.EmpMapper">
<select id="select" resultType="com.example.demo.pojo.Emp">
-- resultType 单条记录封装的类型
select * from emp where username like concat('%',#{name},'%') and gender = #{gender} order by create_time desc
select>
mapper>
快速定位 方便
查找的时候没输入条件则全部查找
select * from emp where username like concat('%',#{name},'%') and gender = #{gender} order by create_time desc
where标签可以自动去除sql中的 and where
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.EmpMapper">
<select id="select" resultType="com.example.demo.pojo.Emp">
-- resultType 单条记录封装的类型
select * from emp
<where>
<if test="name != null and name!= ''">
username like concat('%',#{name},'%')
if>
<if test="gender != null">
and gender = #{gender}
if>
where>
order by create_time desc
select>
mapper>
更新时候如果没传值会变成null -->有值更新无值不更新
set标签 能去掉sql里面的,
update emp set username =#{username},image = #{image},update_time =#{updateTime} where id =#{id}
<update id="update">
update emp
<set>
<if test="username != null">
username=#{username},
if>
<if test="image != null">
image = #{image},
if>
<if test="updateTime != null">
update_time =#{updateTime}
if>
set>
where id = #{id}
update>
批量删除
delete from emp where id in(9,10,11);
<delete id="deleteByIds">
delete
from emp
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
foreach>
delete>
<sql id="xxx">
sql>
<include refid="xxx">
include>