mybatis动态sql与关联查询示例(Mapper文件)
<?xml version="1.0" encoding="UTF-8" ?><!--必要部分, 下面的内容可以全部自己编写, mapper映射文件的声明--><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd
动态SQL: 基于if标签对入参数据进行判断, 根据判断结果决定对应的sql语句是否出现在最终执行的sql中
常用标签:
1) if
2) where
3) set
4) choose, when, otherwise
5) sql, include
----------------------------------------------------
6) bind
7) trim
8) foreach
关联查询: 在结果集映射中, 添加具体关联查询效果的标签
association: 通过配置, 查询到唯一条需要的结果
collection: 通过配置, 查询到多条需要的结果
一对多和多对一查询:
多对一查询使用association标签
一对多查询使用collection标签
标签通用属性:
select: 用于进行关联查询的select标签的id
column: 查询时需要的入参数据来自于哪个字段
property:对象的属性
<?xml version="1.0" encoding="UTF-8" ?>
<!--必要部分, 下面的内容可以全部自己编写, mapper映射文件的声明-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 1. 配置根元素
属性:
namespace, 值是mapper映射文件对应的数据接口的全类名
-->
<mapper namespace="com.dao.StaffMapper">
<!-- 2. 声明结果集映射
属性:
id, 设置映射名称, 会被查询标签引用, 唯一\
type, 设置java对象类型, 全类名
子元素:
id, 设置与主键字段对应的成员变量
result, 设置与普通字段对应的成员变量
通用属性:
` property, 成员变量名
column, 数据表字段名
注: 在结果集映射中, collection标签要放在association标签的后面
-->
<resultMap id="staffMap" type="com.bean.vo.StaffVo">
<id property="id" column="id"></id>
<result property="userName" column="userName"></result>
<result property="password" column="password"></result>
<result property="realName" column="realName"></result>
<result property="gender" column="gender"></result>
<result property="birthday" column="birthday"></result>
<result property="state" column="state"></result>
<result property="roleIds" column="roleIds"></result>
<result property="deptId" column="deptId"></result>
<result property="hireDate" column="hireDate"></result>
<association property="dept" column="deptId"
select="selectDeptByDeptId"></association>
<collection property="courseList" column="id"
select="com.dao.CourseMapper.selectCourseByStuId"></collection>
</resultMap>
<!-- 3. 为数据接口中的方法绑定执行sql
4个操作sql的标签:
1) insert
2) delete
3) update
4) select
标签通用属性:
1) id, 用于设置标签与哪个接口中的方法进行绑定. 值为接口中的方法名
2) parameterType, 用于设置入参类型. 值为接口中对应方法的形参类型
注1: parameterType只写一个类型
注2: mapper映射文件中所有java类都默认使用全类名.
注3: 如果不想使用全类名, 则需在mybatis的配置文件中配置类的别名
3) resultMap, 用于设置查询结果集的转换方式. 值为结果集映射的id名
4) resultType, 设置结果集记录需要转换成的java对象类型
mybatis映射文件中的2种取值方式:
1) 使用#{}取值
底层使用PreparedStatement处理.
会将所有的#{}转换成占位符'?', 再通过预编译, 将#{}中指定的数据编译到sql中
2) 使用${}取值
底层使用Statement处理
需要在sql中自行处理数据格式.
有sql注入的隐患
{}中填写的内容:
[1] parameterType是实体类时, 写成员变量名
[2] parameterType是map集合时, 写map集合的键名
-->
<select id="selectStaffListByDeptId" resultMap="staffMap">
select * from t_staff
where deptId = #{0}
</select>
<select id="selectDeptByDeptId" resultType="com.bean.entity.Department">
select * from t_dept
where id = #{0}
</select>
<sql id="forSelect">
<if test="gender != null">and gender = #{gender}</if>
<if test="deptId != null">and deptId = #{deptId}</if>
<if test="birthdayMax != null">and birthday <= #{birthdayMax}</if>
<if test="birthdayMin != null">and birthday >= #{birthdayMin}</if>
<!--<if test="keyword != null and keyword != ''">
and (
realName like concat('%', #{keyword}, '%')
or userName like concat('%', #{keyword}, '%')
or id like concat('%', #{keyword}, '%')
)
</if>-->
<if test="keyword != null and keyword != ''">
<bind name="key_value" value="'%'+keyword+'%'"/>
and (
realName like #{key_value}
or userName like #{key_value}
or id like #{key_value}
)
</if>
</sql>
<!--
mybatis对java中的一些常用数据类型定义了默认的别名:
Map : map
Integer : int
List : list
-->
<select id="selectByPage" parameterType="map" resultMap="staffMap">
select * from t_staff
<where>
<include refid="forSelect"></include>
</where>
<choose>
<when test="order_by != null">order by ${order_by}</when>
<otherwise>order by id</otherwise>
</choose>
<if test="start_code != null and page_size != null">
limit #{start_code}, #{page_size}
</if>
</select>
<select id="selectCount" parameterType="map" resultType="int">
select count(id) from t_staff
<where>
<include refid="forSelect"></include>
</where>
</select>
<select id="select" parameterType="com.bean.entity.Staff" resultMap="staffMap">
select * from t_staff
<where>
<if test="id != null">and id = #{id}</if>
<if test="userName != null and userName != ''">and userName = #{userName}</if>
<if test="password != null and password != ''">and password = #{password}</if>
</where>
</select>
<select id="selectFor" parameterType="map" resultMap="staffMap">
select * from t_staff
<where>
<if test="list != null">
-- collection为要遍历的集合,item为临时变量,open 为左边要添加的内容,close为右边要添加的内容,
-- separator为 每个条件之间的连接词
<foreach collection="list" item="key" open="and (" close=")" separator="or">
realName like concat('%', #{key}, '%')
</foreach>
</if>
</where>
</select>
<update id="update" parameterType="com.bean.entity.Staff">
update t_staff
<set>
<if test="password != null and password != ''">password = #{password},</if>
<if test="gender != null">gender = #{gender},</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="com.bean.entity.Staff">
delete from t_staff
where id = #{id}
</delete>
<insert id="insert" parameterType="com.bean.entity.Staff">
insert into t_staff(<trim suffix=","><if test="id != null">`id`</if></trim>`userName`,`password`,`realName`,`gender`,`birthday`,`state`,`roleIds`,`deptId`,`hireDate`)
values(<trim suffix=","><if test="id != null">#{id}</if></trim>#{userName},#{password},#{realName},#{gender},#{birthday},#{state},#{roleIds},#{deptId},#{hireDate})
</insert>
</mapper>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)