1.获取主键自增的 的方法
1.添加 useGeneratedKeys=true
<insert id="insert1" parameterType="TUser" useGeneratedKeys="true" keyProperty="id">
2 .order 表示 执行insert 之后 LAST_INSERT_ID() 最后一次执行的主键
<selectKey keyProperty="id" order="AFTER" resultType="int"> select LAST_INSERT_ID() </selectKey>
insert into t_user (id, user_name, real_name,
sex, mobile,
email,
note, position_id)
values (#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{realName,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT}, #{mobile,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{note,jdbcType=VARCHAR},
2 实现like 查询的方法
1. 参数中直接加入%%
param.setUsername("%CD%");
param.setPassword("%11%");
<select id="selectPersons" resultType="person" parameterType="person">
select id,sex,age,username,password from person where true
<if test="username!=null"> AND username LIKE #{username}</if>
<if test="password!=null">AND password LIKE #{password}</if>
</select>
2. bind标签
bind 中不需要加#
<select id="selectPersons" resultType="person" parameterType="person">
<bind name="pattern" value="'%' + _parameter.username + '%'" />
select id,sex,age,username,password
from person
where username LIKE #{pattern}
</select>
3. CONCAT
where username LIKE concat(concat('%',#{username}),'%')
4 使用$
可以直接通过<if test="name != '' and name != null">and Name like '%${name}%' </if>这样来查询,
注意"$"符号,不是用“#”
3 $ 和#的区别 $是使用的Statement #使用的PreStatement
本文介绍两种MyBatis中主键自增的配置方法:使用useGeneratedKeys属性与selectKey元素。并详细讲解了四种实现Like模糊查询的方式:直接在参数中加入通配符、使用bind标签、CONCAT函数以及使用$符号。
1295

被折叠的 条评论
为什么被折叠?



