1. 一个参数参数查询, 不需要加 @Param注解:
User getById(Integer id);
<select id="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);
<select id="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);
<select id="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;
<select id="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();
<select id="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);
<select id="query" resultType="org.example.entity.User">
select * from user where id in
<foreach collection="list" open="(" close=")" sparator="," item="s">
#{s}
</foreach>
</select>
List<User> inQueryUserList(Set<Integer> ids);
<select id="query" resultType="org.example.entity.User">
select * from user where id in
<foreach collection="collection" open="(" close=")" sparator="," item="s">
#{s}
</foreach>
</select>
List<User> inQueryUserList(Integer[] ids);
<select id="query" resultType="org.example.entity.User">
select * from user where id in
<foreach collection="array" open="(" close=")" sparator="," item="s">
#{s}
</foreach>
</select>
8. 动态(trim, 网上会看到,但是是一种非常古老的方式)sql:
<select id="query" resultType="org.example.entity.User">
select * from user
<!-- where下只要有一个if满足条件,mybatis会自动将 where 给我们带上 -->
<where>
<!-- 第一个拼接到where后的语句,会自动将and去掉 -->
<!-- 没有 && ||, 只有 and or -->
<if test="null != name and '' != name.trim()">
and name like #{name}
</if>
<if test="null != begin">
and birthday >= #{begin}
</if>
</where>
</select>
9. 删除:
<delete id="deleteById">
delete from user where id = #{id}
</delete>
9. 动态更新:
<update id="update" paramterType="org.example.entity.User">
update user
<!-- 只要有一个if满足会自动拼接上set;如果一个都不满足,那么会报错,所以需要在程序层面检查-->
<set>
<!-- 拼接到最后那个sql, 会自动将 “,” 去掉 -->
<if test="null != name and '' != name.trim()">
name = #{name},
</if>
</set>
</select>
10. 插入,主要需要注意的问题就是对于自动增长的主键的获取,推荐两种方式:
1. connection.prepareStatement(sql, PrepareStatement.RETURNED_GENERATED_KEY); // 利用这个特点
不会保存User中的id
<insert userGeneratedKeys="true" keyProperty="id" parameterType="org.User">
insert into user(name) values(#{name})
</inset>
这样才会保存Uu中的id
<insert id="addOtherTableData" useGeneratedKeys="true" keyProperty="id" parameterType="org.example.beans.Uu">
insert into uu(name) values (#{name})
</insert>
2. 利用MySQL的一个函数: select last_insert_id(); 会保存主键值给响应属性
<insert userGeneratedKeys="true" parameterType="org.User">
insert into user(name) values(#{name})
<selectKey order="AFTER" keyProperty="id" resultType="int">
select last_insert_id()
</selectKey>
</insert>
11.通用sql 的抽取:
<sql id="csql">
select * form user
</sql>
// exclude // 不包含
使用 <include refid="csql"></include>
12. 多表关联查询:
assosiation: 从多的一方将一的一方带出来。
collection: 从一的一方将多的一方带出来。
例子:
<sql id="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>
<resultMap id="companyResultMap" type="org.example.entity.Company">
<id property="id" column="cid"></id>
<result column="cname" property="name"></result>
<!-- association 表示当前数据对应的唯一一条数据 -->
<association property="area" javaType="org.example.entity.Area">
<id property="id" column="aid"></id>
<result column="province" property="province"/>
<result column="city" property="city"/>
</association>
<!-- javaType是 users的类型,ofType是List集合中装得数据的类型 -->
<collection property="users" javaType="java.util.List" ofType="org.example.entity.SystemUser">
<id property="id" column="uid"></id>
<result column="uname" property="name"></result>
<result column="avatar" property="avatar"/>
<result column="gender" property="gender"/>
<result column="birthday" property="birthday"/>
<result column="createTime" property="createTime"/>
<result column="updateTime" property="updateTime"/>
</collection>
</resultMap>
<select id="getCompanyById" resultMap="companyResultMap">
<include refid="commonSql"></include> where c.id = #{id}
</select>
mybatis复习
最新推荐文章于 2023-12-07 18:18:10 发布