mybatis中_parameter使用和常用sql

mybatis 中 _parameter 使用和常用 sql

在用自动生成工具生成的 mybatis 代码中,总是能看到这样的情况,如下:

<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.juhehl.kapu.pojo.TbCardExample" >  
    select  
    <if test="distinct" >  
      distinct  
    </if>  
    <include refid="Base_Column_List" />  
    from tb_card  
    <if test="_parameter != null" >  
      <include refid="Example_Where_Clause" />  
    </if>  
    <if test="orderByClause != null" >  
      order by ${orderByClause}  
    </if>  
  </select>  

可以看到有个 <if test="_parameter != null" >,如果只有一个参数,那么 _parameter 就代表该参数,如果有多个参数,那么 _parameter 可以 get(0) 得到第一个参数。

 

1. 简单数据类型,
此时 #{id,jdbcType=INTEGER} 中 id 可以取任意名字如 #{a,jdbcType=INTEGER},
如果需要 if test 则一定使用
<if test="_parameter != null">, 此处一定使用 _parameter != null 而不是 id != null
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Java.lang.Integer" > select <include refid="Base_Column_List" /> from base.tb_user <if test="_parameter != null"> where id = #{id,jdbcType=INTEGER} </if> </select>
2. 一个对象数据类型,
此时 #{name,jdbcType=CHAR},#{sex,jdbcType=CHAR} 中的 name 和 sex 一定要使用 user 对象的属性名 测试 user 对象
<if test="_parameter != null">,
测试 user 对象属性<if test="name != null">
或者<if test="#{name} != null">
int insert(User user); <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> insert into tb_user (name, sex) values (#{name,jdbcType=CHAR}, #{sex,jdbcType=CHAR})
3. 二个对象数据类型 List
<User> select(User user,Page page),
此时 if test 一定要<if test='_parameter.get("0").name != null'>
(通过 parameter.get(0) 得到第一个参数即 user);
where 语句 where name = #{0.name,jdbcType=CHAR}(通过 0.name 确保第一个参数 user 的 name 属性值)

不用 0,1 也可以取名 List<User> select(@param(user)User user,@param(page)Page page)
4. 集合类型,
此时 collection="list" 会默认找到参数的那个集合 idlist(collection="list" 这是默认写法, 入参为数组 Integer[] idarr, 则用 collection="array")
User selectUserInList(List
<Interger> idlist);
<select id="selectUserInList" resultType="User">
SELECT * FROM USER WHERE ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>

5. 对象类型中的集合属性,

此时 collection="oredCriteria" 会找到入参 example 这个非集合对象的 oredCriteria 属性, 此属性是一个集合
List
<User> selectByExample(UserExample example);
<where>
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >

6. map 类型 (分页查询教师信息)

public List
<Teacher> findTeacherByPage(Map<String, Object> map);
Map
<String, Object> params = new HashMap<String, Object>();
// 以 name 字段升序排序,params.put("sort", "name"); params.put("dir", "asc");
// 查询结果从第 0 条开始,查询 2 条记录 params.put("start", 0); params.put("limit", 2);
// 查询职称为教授或副教授的教师 params.put("title", "% 教授");
此时入参 map 的 key 相当于一个 object 的属性名,value 相当于属性值
<select id="findTeacherByPage"resultMap="supervisorResultMap" parameterType="java.util.Map">
select * from teacher where title like #{title}
order by ${sort} ${dir} limit #{start},#{limit}
</select>
7. 批量插入
<insert id="addRoleModule" parameterType="java.util.List">
INSERT INTO T_P_ROLE_MODULE (ROLE_ID, MODULE_ID)
<foreach collection="list" item="item" index="index" separator="UNION ALL">
SELECT #{item.roleId}, #{item.moduleId} FROM DUAL
</foreach>
</insert>
8.MyBatis+MySQL 返回插入的主键 ID
在 mapper 中指定 keyProperty 属性,示例如下:
我们在 insert 中指定了 keyProperty="userId" 和 useGeneratedKeys="true", 其中 userId 代表插入的 User 对象的主键属性。
System.out.println("插入前主键为:"+user.getUserId());
userDao.insertAndGetId(user);// 插入操作
System.out.println("插入前主键为:"+user.getUserId());

</span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">insert </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="insertAndGetId"</span><span style="color: rgba(255, 0, 0, 1)"> useGeneratedKeys</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> keyProperty</span><span style="color: rgba(0, 0, 255, 1)">="userId"</span><span style="color: rgba(255, 0, 0, 1)"> parameterType</span><span style="color: rgba(0, 0, 255, 1)">="com.chenzhou.mybatis.User"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">  

insert into user(userName,password,comment) values(#{userName},#{password},#{comment})
</insert>