你真的会写mybatis.xml吗?养成好习惯写养眼好用的xml
我们使用mybatis无非就是进行一些增删改查的操作,但是简单的增删改查想要写好却大有门道。
增
对于实体的新增墙裂建议写一个通用的新增语句,这样在新增的时候就不需要每一次都去写一个新的语句了:
<insert id="insertSelective" parameterType="com.xyh.entity.QuartzJob"> insert into xyh_quartz_job <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="jobKey != null"> job_key, </if> <if test="jobGroup != null"> job_group, </if> <if test="jobStatus != null"> job_status, </if> <if test="jobCronExpress != null"> job_cron_express, </if> <if test="isDeleted != null"> is_deleted, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="jobKey != null"> #{jobKey,jdbcType=VARCHAR}, </if> <if test="jobGroup != null"> #{jobGroup,jdbcType=VARCHAR}, </if> <if test="jobStatus != null"> #{jobStatus,jdbcType=INTEGER}, </if> <if test="jobCronExpress != null"> #{jobCronExpress,jdbcType=VARCHAR}, </if> <if test="isDeleted != null"> #{isDeleted,jdbcType=INTEGER}, </if> </trim> </insert>
参数解析:
id:唯一标识
parameterType:传入的参数实体
trim:
rim标签有四个属性:prefix,prefixOverrides,suffix,suffixOverrides。
prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides的情况下) 。
如果有prefixOverrides,suffixOverrides 表示覆盖Overrides中的内容。
如果只有prefixOverrides,suffixOverrides 表示删除。
以上示例中suffixOverrides表示删除最后的','。
删
关于删除,一般我们会用到使用主键删除,或者传入参数删除这两种情况。
因此可以新增两条通用的语句:
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> delete from xyh_quartz_job where id = #{id,jdbcType=BIGINT} </delete> <delete id="deleteByExample" parameterType="com.xyh.entity.QuartzJobCriteria"> delete from xyh_quartz_job <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </delete>
这里又出现了一个include标签,他的意思是通过id引用其他的语句,以下是通用更新的语句:
<sql id="Update_By_Example_Where_Clause"> <where> <foreach collection="example.oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql>
参数解析
where:表示where条件
foreach:循环对象
foreach一共有三种参数类型,分别为List,Array,Map三种。
item:环体中的具体对象。
collection:要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。
if:条件判断
如果条件为真才会渲染if里的语句
choose...when:条件判断
当有多个条件时使用choose...when。
查
在工作中运用最多的就是查询了,怎么把查询写的漂亮也是有讲究的,首先我们一般都要定义一个Base_Column_List:
<sql id="Base_Column_List"> id, job_key, job_group, job_status, job_cron_express, remarks, job_desc, job_params, gmt_create, gmt_modified, is_deleted </sql>
包含了我们一般情况下都需要查询的字段,避免以后每次查询都要重复写这些字段。
除了字段,还有查询条件。查询条件千变万化,我们也可以先把能统一的部分通用出来:
<sql id="Example_Where_Clause"> <where> <foreach collection="oredCriteria" item="criteria" separator="or"> <if test="criteria.valid"> <trim prefix="(" prefixOverrides="and" suffix=")"> <foreach collection="criteria.criteria" item="criterion"> <choose> <when test="criterion.noValue"> and ${criterion.condition} </when> <when test="criterion.singleValue"> and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue"> and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue"> and ${criterion.condition} <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql>
这里的prefixOverrides表示删掉第一个and。
然后首先根据主键查询:
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from xyh_quartz_job where id = #{id,jdbcType=BIGINT} </select>
看起来十分的简介。
根据外部条件查询:
<select id="selectByExample" parameterType="com.xyh.entity.QuartzJobCriteria" resultMap="BaseResultMap"> select <if test="distinct"> distinct </if> <include refid="Base_Column_List" /> from xyh_quartz_job <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select>
根据外部条件查询数量:
<select id="countByExample" parameterType="com.xyh.entity.QuartzJobCriteria" resultType="java.lang.Long"> select count(*) from xyh_quartz_job <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> </select>
当然mybatis的通用mapper就是在帮我们做这些事,不过其实通用mapper底层的方式也是这样。我们在平时写mybatis.xml的时候要养成这种书写习惯,熟练运用各种标签,能提取出来的就提取出来,使我们的主干语句看起来通俗易懂,别人看我们写的代码也不至于那么头疼了。