(javaweb)mybatis2

目录

一.XML映射文件

二.mybatis动态sql

1.if标签

 案例:

2.foreach标签

3. sql include标签


一.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自己自闭合即可

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值