MyBatis的动态SQL语句
文章目录 1. 动态SQL之<if>标签 2. 动态SQL之<where>标签 3. 动态SQL之<foreach>标签 4. MyBatis中的SQL片段 1. 动态SQL之<if>标签 我们根据实体类的不同取值,使用不同的SQL语句来进行查询。比如在id如果不为空时可以根据id查询,如果username不为空时还要加入用户名作为条件,这种情况在我们的多条件组合查询中经常会碰到。 <?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"> < mapper namespace = " com.joker.dao.IUserDao " > < select id = " findByUser " resultType = " user " parameterType = " user " > select * from user where 1=1 < if test = " username!=null and username != ' ' " > and username like #{username} </ if > < if test =