动态sql介绍
由于在开发过程不同的业务中会用到不同的操作条件,如果每个业务都拼接不同sql语句的话会是一个庞大的工作量;此时动态sql就能解决这个问题,可以针对不确定的操作条件动态拼接sql语句,根据提交的条件来完成业务sql的执行!
sql根标签
<insert>,<update>,<select>,<delete>
动态sql标签
<if>,<choose>,<when>,<otherwise>,<trim>,<foreach>,<where>,<set>,<bind>
关联关系标签
<collection>,<association>
sql根标签介绍
01<select>
<select id="getById" resultMap="orderMap"/>
02<insert>
<update id="update" parameterType="employee"/>
03<update>
<insert id="insert" puseGeneratedKeys="true" keyProperty="id"/>
04<delete>
<delete id="delete" parameterType="employee"/>
注意哦
parameterType与resultMap为查询返回结果,且不能同时存在,id不能重复,插入时候使用puseGeneratedKeys、keyProperty来保证主键生成
常用动态sql标签介绍
01<foreach>
//循环插入等场景使用foreach
<select id="selectIDInEids" parameterType="java.util.List" resultType="com.atguigu.ssm.pojo.Employee">
select * from employee where id in
<foreach collection="eids" item="eid" open="(" separator="," close=")">
${eid}
</foreach>
</select>
02<if>
//通常用来处理条件语句的判断
<select id="queryEmployeeIf" parameterType="employee" resultType="employee">
select * from employee where 1=1
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="gender != null and gender != '' ">
and gender like '%${gender}%'
</if>
</select>
03<choose><when><otherwise>
<select id="queryEmployeeChoose" parameterType="employee" resultType="employee">
select * from employee where 1=1
<choose>
<when test="empName != null and empName != ''">
and emp_name = #{empName}
</when>
<when test="gender != null and gender != '' ">
and gender like '%${gender}%'
</when>
<otherwise>
and id = 18
</otherwise>
</choose>
</select>
04<where>
// where标签标明,有符合条件则主动拼接条件;不需要额外拼接where 1=1
<select id="queryEmployeeWhere" parameterType="employee" resultType="employee">
select * from employee
<where>
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
</where>
</select>
05<trim>
// 效果与 where类似,前缀添加where,如果符合条件则提出默认and。
<select id="queryEmployeeTrim" parameterType="employee" resultType="employee">
select * from employee
<trim prefix="where" prefixOverrides="and" >
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
</trim>
</select>
06<set>
// set标签可以用来进行更新操作,mybatis操作更新只需要指定对应元素即可进行局部更新,显的更加灵活
<update id="updateEmployeeInfo" parameterType="employee" >
update employee
<set>
<if test="empName != null and empName != ''">
emp_name=#{empName},
</if>
<if test="gender != null">
gender=#{gender},
</if>
</set>
where id=#{id}
</update>
07<bind>
// 该标签主要为了给传递的参数添加特色标签而完成特殊的需求;
// 如模糊查询可以通过该标签添加%name%
<select id="queryEmployeeInfoByBind" parameterType="employee" resultType="employee">
<bind name="pattern" value="'%' + empName + '%'"/>
select * from student where empName like #{pattern}
</select>
注意哦
01字符串空值问题
字符串变量需要针对null值和空字符串判断哦;
02基本类型转换问题
针对基本的类型的变量如果实体与前台字段中存在空字符串会报错哦,所以实例变量最好使用其包装类作为基本类型的替换类型;
03xml解析&&问题
动态sql时解析时xml无法失败&&,需要替换成为and,表示并且。
关联关系标签介绍
01<association>
// 该标签专门用来映射一对一类型关联关系,如每个人都拥有自己的身份标识,且仅仅为一对一,不会存在多种的可能
<resultMap id="orderMap" type="com.atguigu.entity.Order">
<id column="order_id" property="orderId"></id>
<result column="order_name" property="orderName"></result>
<result column="customer_id" property="customerId"></result>
<association property="customer" javaType="com.atguigu.entity.Customer">
<id column="customer_id" property="customerId"></id>
<result column="customer_name" property="customerName"></result>
</association>
</resultMap>
02<collection>
//该标签则能完成一对多的关联关系,多对多则可以理解为俩次处理一对多即可
<resultMap id="customerMap" type="com.atguigu.entity.Customer">
<id property="customerId" column="customer_id"></id>
<result property="customerName" column="customer_name"></result>
<collection property="orderList" ofType="com.atguigu.entity.Order">
<id property="orderId" column="order_id"></id>
<result property="orderName" column="order_name"></result>
</collection>
</resultMap>
小结
总之,在您学完MyBatis中动态拼接sql技能后,针对不同业务场景,灵活运用动态标签即可达到您想要的结果!