mybatis框架执行SQL语句的时候需要配置xml文件与接口中的SQL方法进行映射,
-
首先是映射的名称,一般名称是相同的
-
namespace标签代表映射接口路径
- 在xml文件里面配置对应的属性,这个resultMap可以灵活的映射表的字段和实体类的属性名,然后在写SQL标签的时候,返回这个类型即可
<!--
id:唯一标识
type:映射的类型,支持别名
-->
<resultMap id="brandResultMap" type="brand">
<!--
id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
- 如果想要查询的字段比较多且多次书写,重复代码较多,可以使用SQL标签进行汇总
<sql id="brand_column">
id, brand_name as brandName, company_name as companyName, ordered, description, status
</sql>
<select id="selectAll" resultType="brand">
select
<include refid="brand_column" />
from tb_brand;
</select>
- 如果入参比较多,可以对入参进行封装,然后在xml里面进行动态SQL查询
//接口里面的方法,入参是map
List<Brand> selectByCondition(Map map);
//可以是单个参数,使用@Param注解进行映射,括号里面的要和xml文件里面的#{}里面的占位符相对应
List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
//还可以是实体类,直接封装到对象里面
//List<Brand> selectByCondition(Brand brand);
//可以使用where恒等式来消除SQL语句发生错误的因素或者使用<where>标签来执行SQL
//id里面映射的是接口里面的方法名
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
/* where 1 = 1*/
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test="brandName != null and brandName != '' ">
and brand_name like #{brandName}
</if>
</where>
</select>
- 可以使用chose标签进行选择查询,适用于下拉框选择多个条件中的一个进行查询
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose><!--相当于switch-->
<when test="status != null"><!--相当于case-->
status = #{status}
</when>
<when test="companyName != null and companyName != '' "><!--相当于case-->
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != ''"><!--相当于case-->
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
- 添加数据的时候,传入对应的实体类,#{}里面是占位符与实体类字段映射
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
- 更新数据的时候,可以选择更新哪些字段,与正常的SQL语句没什么区别
<update id="update">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null">
status = #{status}
</if>
</set>
where id = #{id};
</update>
- 删除数据,一般是根据业务主键进行删除,而不是表的主键,在公司中表一般都有业务主键,业务主键是由表的几个字段组成的唯一标识;
<delete id="deleteById">
delete from tb_brand where id = #{id};
</delete>
- 批量删除列表可以使用foreach标签来遍历入参,一般入参是一个数组array
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
更多推荐
mybatis有关xml文件配置SQL的相关基础知识
发布评论