目录
一.XML映射文件
通过配置xml文件来配置sql语句 项目开发--一份接口对应一份xml文件
resources--存放配置文件
xml配置文件--需要有约束
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
获取约束--浏览器--mybatis中文网--
根标签--mapper
此插件提高开发效率
二.mybatis动态sql
1.if标签
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.mapper.EmpMapper"> <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> </mapper>
案例:
<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} </set> </update>
@Test public void testUpdate2(){ Emp emp=new Emp(); emp.setId(22); emp.setUsername("Tom2222"); emp.setName("汤姆2222"); emp.setGender((short)2); emp.setUpdateTime(LocalDateTime.now()); empMapper.update2(emp); }
2.foreach标签
3. sql include标签
include自己自闭合即可