增删改查
环境准备
- 创建一个emp表
 - 创建一个新的springboot工程,选择mysql、lombok、mybatis依赖
 - application.properties中引入数据库连接信息
 - 创建对应的实体类Emp
 - 准备Mapper接口EmpMapper,@mapper代表程序运行时自动创建接口的代理对象,并放入ioc容器之中
 
如果一个文件夹是springboot项目,就从maven窗口引入它的pom文件
删除
@delete现在里面的value语句没有提示,不知道怎么回事
@Mapper
public interface EmpMapper {
//    delete data by id
    @Delete("delete from emp where id = # {id}")
    public void delete(Integer id);
//返回影响的元组数量
    public int delete(Integer id);
} 
日志输出
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl 
预编译sql
性能更高:从缓存当中直接拿到编译的sql语句
更安全:防止sql注入

#{...}:预编译,被?取代
${...}:直接拼接sql语句
新增
可以用实体类封装
主键返回:
@Options(keyProperty = "id", UsedGeneratedKeys = True)
更新
根据主键修改员工信息
查询
如果实体类属性名和数据库表查询返回的字段名不一致,不能自动封装
@Select("select id, username, password, name, gender, image, job, entrydate, " +
            "dept_id deptID, create_time createTime, update_time updateTime from emp where id = #{id}")
 
@Results({
     @Result(column = "dept_id", property = "deptId"),
     @Result(column = "create_time", property = "createTime"),
     @Result(column = "update_time", property = "updateTime")
}) 
#开启驼峰命名自动映射
mybatis.configuration.map-underscore-to-camel-case=true 
查询(条件查询)
模糊匹配里面要把'%张%'本来改成'%#{name}%',但是''里面不能装#,所以改成%${name}%',但是性能低,容易被注入
concat函数,改成concat('%',#{name},'%'),这样引号里面就没有问号了
xml映射文件
- XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名)
 - XML映射文件的namespace属性为Mapper接口全限定名一致
 - XML映射文件中sql语句的id与Mapper接口中的方法名一致,并保持返回类型一致
 
动态sql
if
查询select
<!--    resultType : 单条记录所封装的类型。-->
    <select id="list" resultType="com.itheima.pojo.Emp">
        select * from emp
        <where>
              <if test="name != null">
                  name like concat('%',#{name},'%')
              </if>
              <if test="gender != null">
                  and gender = #{gender}
              </if>
              <if test="begin != null and end != null">
                  and entrydate between #{begin} and #{end}
              </if>
        </where>
        order by update_time desc
    </select> 
更新update
注意!!!test里面的属性名不要写成数据库里面的了
<update id="update2">
        update emp
        set
            <if test = "username != null">
            username=#{username},
            </if>
            <if test = "name != null">
            name=#{name},
            </if>
            <if test = "gender != null">
            gender=#{gender},
            </if>
            <if test = "image != null">
            image=#{image},
            </if>
            <if test = "job != null">
            job=#{job},
            </if>
            <if test = "entrydate != null">
            entrydate=#{entrydate},
            </if>
            <if test = "deptId != null">
            dept_id=#{deptId},
            </if>
            <if test = "updateTime != null">
            update_time=#{updateTime}
            </if>
        where id = #{id}
    </update> 
<set>标签可以自动忽略逗号
foreach
    <delete id="deleteIds">
        delete from emp where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete> 
sql,include
sql定义可重用语句片段
include是一个单标签,里面设置refid值
















![[leetcode] 33. 搜索旋转排序数组](https://img-blog.csdnimg.cn/direct/a36c1b59f2eb48eebd27dc7ba26116ca.png)


