1. 一个参数参数查询, 不需要加 @Param注解:
User getById(Integer id);
<selectid="getById"resultType="org.example.entity.User">
select * from user where id = #{id}
</select>
2. 多个参数,必须要加 @Param 注解,参数多了不推荐使用单个传参的方式,而是使用对象或者 Map集合的方式:
User getById(@Param("id") Integer id, @Param("name") String name);
<selectid="getById"resultType="org.example.entity.User">
select * from user where id > #{id} and name like #{name}
</select>
3. 多参数使用Map传入参数:
// Map的key和 mapper文件中的 #{} 中的内容是一致的。 map.put("id", 1); map.put("name", "%z%")
User getById(Map<String,Object> params);
<selectid="getById"parameterType="java.util.Map"resultType="org.example.entity.User">
select * from user where id > #{id} and name like #{name}
</select>
4. 多个参数使用对象传参:
User getById(QueryUser qu); // QueryUser中的属性名于 #{} 中的内容是一致的 private Integer id; private String name;
<selectid="getById"parameterType="org.example.beans.QueryUser"resultType="org.example.entity.User">
select * from user where id > #{id} and name like #{name}
</select>
5. 返回值:
如果返回列表: List<User> 来封装。
6. 如果跨表,多字段查询,使用 List<Map<String,Obejct>> 来封装查询的参数。
List<Map<String,Obejct>> query();
<selectid="query"resultType="java.util.Map">
select u.name uname, c.name cname from user u, company c where u.company_id = c.id
</select>
7. in查询:
List<User> inQueryUserList(List<Integer> ids);
<selectid="query"resultType="org.example.entity.User">
select * from user where id in
<foreachcollection="list"open="("close=")"sparator=","item="s">
#{s}
</foreach></select>
List<User> inQueryUserList(Set<Integer> ids);
<selectid="query"resultType="org.example.entity.User">
select * from user where id in
<foreachcollection="collection"open="("close=")"sparator=","item="s">
#{s}
</foreach></select>
List<User> inQueryUserList(Integer[] ids);
<selectid="query"resultType="org.example.entity.User">
select * from user where id in
<foreachcollection="array"open="("close=")"sparator=","item="s">
#{s}
</foreach></select>
8. 动态(trim, 网上会看到,但是是一种非常古老的方式)sql:
<selectid="query"resultType="org.example.entity.User">
select * from user
<!-- where下只要有一个if满足条件,mybatis会自动将 where 给我们带上 --><where><!-- 第一个拼接到where后的语句,会自动将and去掉 --><!-- 没有 && ||, 只有 and or --><iftest="null != name and '' != name.trim()">
and name like #{name}
</if><iftest="null != begin">
and birthday >= #{begin}
</if></where></select>
9. 删除:
<deleteid="deleteById">
delete from user where id = #{id}
</delete>
9. 动态更新:
<updateid="update"paramterType="org.example.entity.User">
update user
<!-- 只要有一个if满足会自动拼接上set;如果一个都不满足,那么会报错,所以需要在程序层面检查--><set><!-- 拼接到最后那个sql, 会自动将 “,” 去掉 --><iftest="null != name and '' != name.trim()">
name = #{name},
</if></set></select>
10. 插入,主要需要注意的问题就是对于自动增长的主键的获取,推荐两种方式:
1. connection.prepareStatement(sql, PrepareStatement.RETURNED_GENERATED_KEY); // 利用这个特点
不会保存User中的id
<insertuserGeneratedKeys="true"keyProperty="id"parameterType="org.User">
insert into user(name) values(#{name})
</inset>
这样才会保存Uu中的id
<insertid="addOtherTableData"useGeneratedKeys="true"keyProperty="id"parameterType="org.example.beans.Uu">
insert into uu(name) values (#{name})
</insert>
2. 利用MySQL的一个函数: select last_insert_id(); 会保存主键值给响应属性
<insertuserGeneratedKeys="true"parameterType="org.User">
insert into user(name) values(#{name})
<selectKeyorder="AFTER"keyProperty="id"resultType="int">
select last_insert_id()
</selectKey></insert>
11.通用sql 的抽取:
<sqlid="csql">
select * form user
</sql>
// exclude // 不包含
使用 <includerefid="csql"></include>
12. 多表关联查询:
assosiation: 从多的一方将一的一方带出来。
collection: 从一的一方将多的一方带出来。
例子:
<sqlid="commonSql">
select u.id uid, u.name uname, u.gender, u.email, u.birthday,
u.createTime, u.updateTime, u.avatar, c.id cid, c.name cname,
a.id aid, a.province, a.city
from user u join company c on u.company_id = c.id
join area a on a.id = c.area_id
</sql><resultMapid="companyResultMap"type="org.example.entity.Company"><idproperty="id"column="cid"></id><resultcolumn="cname"property="name"></result><!-- association 表示当前数据对应的唯一一条数据 --><associationproperty="area"javaType="org.example.entity.Area"><idproperty="id"column="aid"></id><resultcolumn="province"property="province"/><resultcolumn="city"property="city"/></association><!-- javaType是 users的类型,ofType是List集合中装得数据的类型 --><collectionproperty="users"javaType="java.util.List"ofType="org.example.entity.SystemUser"><idproperty="id"column="uid"></id><resultcolumn="uname"property="name"></result><resultcolumn="avatar"property="avatar"/><resultcolumn="gender"property="gender"/><resultcolumn="birthday"property="birthday"/><resultcolumn="createTime"property="createTime"/><resultcolumn="updateTime"property="updateTime"/></collection></resultMap><selectid="getCompanyById"resultMap="companyResultMap"><includerefid="commonSql"></include> where c.id = #{id}
</select>