动态数据库查询
<?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="CustomerMapper">
<resultMap id="BaseResultMap"
type="com.hlq.ch8.dao.Customer">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR"
property="username" />
<result column="jobs" jdbcType="VARCHAR" property="jobs" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
</resultMap>
<sql id="Base_Column_List">
id, username, jobs, phone
</sql>
<select id="findCustomersByNameAndJobs" parameterType="com.hlq.ch8.dao.Customer" resultMap="BaseResultMap">
SELECT * FROM customer
<where>
<if test="username!=null and username!=''">
and username like concat('%', #{username}, '%')
</if>
<if test="jobs!=null and jobs!=''">
and jobs = #{jobs}
</if>
</where>
</select>
<select id="findCustomersByNameOrJobs" parameterType="com.hlq.ch8.dao.Customer" resultMap="BaseResultMap">
SELECT * FROM customer
<where>
<choose>
<when test="username!=null and username!=''">
and username like concat('%', #{username}, '%')
</when>
<when test="jobs!=null and jobs!=''">
and jobs = #{jobs}
</when>
<otherwise>
and phone IS NOT NULL
</otherwise>
</choose>
</where>
</select>
<select id="findCustomersByIds" parameterType="Integer" resultMap="BaseResultMap">
SELECT * FROM customer
WHERE id in
<foreach item="id" index="index" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<select id="findCustomerByName" parameterType="String" resultMap="BaseResultMap">
<bind name="pattern_username" value="'%'+username+'%'"></bind>
SELECT * FROM customer WHERE username LIKE #{pattern_username}
</select>
<insert id="insertSelective" parameterType="com.hlq.ch8.dao.Customer">
insert into customer
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="jobs != null">
jobs,
</if>
<if test="phone != null">
phone,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="jobs != null">
#{jobs,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.hlq.ch8.dao.Customer">
update customer
<set>
<if test="username != null">
username=#{username},
</if>
<if test="jobs!=null">
jobs = #{jobs}
</if>
<if test="phone!=null">
phone = #{phone}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>