1. 程式人生 > >MyBatis常用sql語句編寫(基於MySQL資料庫)

MyBatis常用sql語句編寫(基於MySQL資料庫)

建立一張表t_user用於作示例:

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
  `name` varchar(20) DEFAULT NULL COMMENT '姓名',
  `sex` char(1) DEFAULT NULL COMMENT '性別:1.男 2.女 3.保密',
  `birthday` date DEFAULT NULL COMMENT '生日',
  `del_flag` char(1) DEFAULT '0' COMMENT '刪除標識:0.正常 1.刪除 2.稽核',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

資料庫對應實體類:

package cn.com.javatest.entity;

import java.util.Date;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * 使用者實體類
 * 
 * @author  Rodge
 * @time    2018年11月9日 下午12:05:35
 * @version 1.0.0
 */
public class User {

	/**
	 * 主鍵ID
	 */
	private int id;
	
	/**
	 * 姓名
	 */
	private String name;
	
	/**
	 * 性別:1.男 2.女 3.保密
	 */
	private String sex;
	
	/**
	 * 生日
	 */
	private Date birthday;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
        @Override  
        public String toString() {  
            return ReflectionToStringBuilder.toString(this);  
        }
}

新增sql語句:

<insert id="save" parameterType="cn.com.javatest.entity.User">
    INSERT INTO t_user (
	    name,
	    sex,
	    birthday
    ) VALUES (
	    #{name},
	    #{sex},
	    #{birthday}
    )
</insert>

新增返回ID的sql語句(ID為整型):

<insert id="save" parameterType="cn.com.javatest.entity.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
	    SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT INTO t_user (
	    name,
	    sex,
	    birthday
    ) VALUES (
	    #{name},
	    #{sex},
	    #{birthday}
    )
</insert>

新增返回ID的sql語句(ID為字串):

<insert id="save" parameterType="cn.com.javatest.entity.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.String">  
	    SELECT REPLACE(UUID(), '-', '') FROM DUAL
    </selectKey>
    INSERT INTO t_user (
	    name,
	    sex,
	    birthday
    ) VALUES (
	    #{name},
	    #{sex},
	    #{birthday}
    )
</insert>

批量新增sql語句:

<insert id="saveBatch" parameterType="java.util.List">
    INSERT INTO t_user (
        name,
        sex,
        birthday
    ) VALUES
    <foreach collection="list" item="entity" index="index" separator=",">
        (#{entity.id}, 
         #{entity.sex},
         #{entity.birthday})
    </foreach>
</insert>

更新sql語句:

<update id="update" parameterType="cn.com.javatest.entity.User">
    UPDATE t_user SET
        name = #{name},
        sex = #{sex},
        birthday = #{birthday}
    WHERE del_flag = '0' AND id = #{id}	
</update>

動態更新sql語句:

<update id="update" parameterType="cn.com.javatest.entity.User">
    UPDATE t_user
    <set>
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="sex != null and sex != ''">
            sex = #{sex},
        </if>
        <if test="birthday != null">
            birthday = #{birthday},
        </if>
    </set>
    WHERE del_flag = '0' AND id = #{id}
</update>

批量更新sql語句:

<update id="updateBatch" parameterType="cn.com.javatest.entity.User">
    <foreach collection="list" item="entity" separator=";">
        UPDATE t_user SET
            name = #{entity.name},
            sex = #{entity.sex},
            birthday = #{entity.birthday}
        WHERE del_flag = '0' AND id = #{entity.id}
    </foreach>
</update>

刪除sql語句(邏輯刪除):

<update id="delete">
    UPDATE t_user SET del_flag = '1' WHERE id = #{id}
</update>

刪除sql語句(物理刪除):

<delete id="delete">
    DELETE FROM t_user WHERE id = #{id}
</delete>

批量刪除sql語句:

<delete id="deleteBatch">
    DELETE FROM t_user WHERE id IN
    <foreach collection="ids" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</delete>

根據主鍵ID查詢sql語句:

<select id="findById" resultType="cn.com.javatest.entity.User">
    SELECT
        id,
        name,
        sex,
        birthday
    FROM t_user 
    WHERE del_flag = '0' AND id = #{id}
</select>

多引數動態查詢sql語句:

<select id="selectList" resultType="cn.com.javatest.entity.User">
    SELECT
        t.id,
        t.name,
        t.sex,
        t.birthday
    FROM t_user t
    <where>
        t.del_flag = '0'
        <if test="name != null and name != ''">
            AND t.name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="sex != null and sex != ''">
            AND t.sex = #{sex}
        </if>
        <if test="startDay != null and startDay != ''">
            AND t.birthday &gt;= #{startDay}
        </if>
        <if test="endDay != null and endDay != ''">
            AND t.birthday &lt;= #{endDay}
        </if>
    </where>	
</select>

根據主鍵ID批量查詢sql語句:

<select id="selectList" resultType="cn.com.javatest.entity.User">
    SELECT
        t.id,
        t.name,
        t.sex,
        t.birthday
    FROM t_user t
    WHERE t.del_flag = '0' AND t.id IN
    <foreach collection="ids" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>	
</select>

多引數批量查詢sql語句:

<select id="findByParams" resultType="cn.com.javatest.entity.User">
    <foreach collection="list" item="entity" separator="UNION ALL">
        SELECT
            t.id,
            t.name,
            t.sex,
            t.birthday
        FROM t_user t
        WHERE t.del_flag = '0' 
        AND t.name = #{entity.name}
        AND t.sex = #{entity.sex}
        AND t.birthday = #{entity.birthday}
    </foreach> 
</select>