mybatis XML SQL基本配置


<!--分页参数查询-->
<select id="findByPage1" resultType="user">
    select * from t_user limit #{startIndex},#{pageSize}
</select>
<!--分页参数查询-->
<select id="findByPage2" resultType="user">
    select * from t_user limit #{param1},#{param2}
</select>


<!--多条件查询QueryVO封装条件-->
<select id="findByQueryVO" resultType="user">
    select * from t_user
    where user_name like concat("%",#{user.username},"%") and sex = #{user.sex}
    limit #{startIndex},#{pageSize}
</select>
<!--多条件查询Map封住条件-->
<select id="findByMap" resultType="user">
    select * from t_user
    where user_name like concat("%",#{username},"%") and sex = #{sex}
    limit #{startIndex},#{pageSize}
</select>
<select id="selectAll" resultMap="userResultMap">
    select * from t_user
</select>
<!--回显主键-->
<!--  第一种int类型
    keyColumn:主键字段名
    keyProperty:主键属性名
    resultType:属性类型
    order:执行时机
            可选择:AFTER,BEFORE 只能写大写
    select last_insert_id():查询最后一次新增的id
-->
<insert id="insert01">
  <selectKey keyColumn="id" keyProperty="id" resultType="int" order="AFTER">
      /*查询最后一次新增的id  并封装到user里面*/
    select last_insert_id()
  </selectKey>
    INSERT INTO `t_user` (
        `id`,
        `user_name`,
        `birthday`,
        `sex`,
        `home_address`
        )
    VALUES
      (
        null,
        #{username},
        #{birthday},
        #{sex},
        #{address}
      )
</insert>

<!--第二种获取主键方式int类型  是否自动回显主键-->
<insert id="insert02" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    INSERT INTO `t_user` (
    `id`,
    `user_name`,
    `birthday`,
    `sex`,
    `home_address`
    )
    VALUES
    (
    null,
    #{username},
    #{birthday},
    #{sex},
    #{address}
    )
</insert>
<!--  第三种String类型
keyColumn:主键字段名
keyProperty:主键属性名
resultType:属性类型
order:执行时机
        可选择:AFTER,BEFORE 只能写大写
select last_insert_id():查询最后一次新增的id

-->


/查询最后一次新增的id 并封装到user里面/
select uuid()/自己生成一个传递过去/

INSERT INTO t_user (
id,
user_name,
birthday,
sex,
home_address
)
VALUES
(
null,
#{username},
#{birthday},
#{sex},
#{address}
)

原文地址:https://www.cnblogs.com/x-i-n/p/14111086.html