配置文件实现CRUD
数据的
查询
、按条件查询
、添加
、删除
、批量删除
、修改
等功能,而这些功能其实就是对数据库表中的数据进行CRUD操作。避免Mapper接口和Mapper映射文件会经常写错,导致不匹配
安装 MyBatisX 插件
MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。
主要功能
XML映射配置文件 和 接口方法 间相互跳转
根据接口方法生成 statement
数据库表(tb_brand)及数据准备
-- 删除tb_brand表 drop table if exists tb_brand; -- 创建tb_brand表 create table tb_brand ( -- id 主键 id int primary key auto_increment, -- 品牌名称 brand_name varchar(20), -- 企业名称 company_name varchar(20), -- 排序字段 ordered int, -- 描述信息 description varchar(100), -- 状态:0:禁用 1:启用 status int ); -- 添加数据 insert into tb_brand (brand_name, company_name, ordered, description, status) values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0), ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1), ('小米', '小米科技有限公司', 50, 'are you ok', 1);
实体类 Brand
public class Brand { // id 主键 private Integer id; // 品牌名称 private String brandName; // 企业名称 private String companyName; // 排序字段 private Integer ordered; // 描述信息 private String description; // 状态:0:禁用 1:启用 private Integer status; }
1.2 查询-查询所有数据
编写接口方法:Mapper接口
参数:无
结果:List<Brand>
编写SQL语句和SQL映射文件
//Mapper接口 //查询所有 List<Brand> selectAll();
<select id = "selectAll" resultType = "brand"> select * from tb_brand; </select>
1.3 查询-结果映射
实体属性名和数据库表列名不一致,不能自动封装数据
给字段起别名
<select id="selectAll" resultType="brand"> select id, brand_name as brandName, company_name as companyName, ordered, description, status from tb_brand; </select>
使用resultMap定义字段和属性的映射关系
在映射配置文件中使用resultMap定义 字段 和 属性 的映射关系
<resultMap id="brandResultMap" type="brand"> <!-- id:完成主键字段的映射 column:表的列名 property:实体类的属性名 result:完成一般字段的映射 column:表的列名 property:实体类的属性名 --> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/> </resultMap>
2.使用resultMap替换resultType
<select id="selectAll" resultMap="brandResultMap"> select * from tb_brand; </select>
实体类属性名 和 数据库表列名 不一致,不能自动封装数据
起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样
可以定义 <sql>片段,提升复用性
resultMap:定义<resultMap> 完成不一致的属性名和列名的映射
而我们最终选择使用 resultMap的方式。
1.4 查询-查看详情
<select resultMap="" id="" > select * from tb_brand where id = #{id} </select>
条件查询在sql中传参的定义
mybatis提供了两种参数占位符:
#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。从上述例子可以看出使用#{} 底层使用的是
PreparedStatement
${} :拼接SQL。底层使用的是
Statement
,会存在SQL注入问题
1.5 查询-条件查询
条件查询多个参数
使用
@Param("参数名称")
标记每一个参数,在映射配置文件中就需要使用#{参数名称}
进行占位将多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。(占位符内容必须和实体类属性名保持一致)
将多个参数封装到map集合中,将map集合作为接口的方法参数。(占位符里面的内容必须和map集合中键的名称一致)
在
.xml
映射配置文件中编写statement
,使用resultMap
而不是使用resultType
<select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName} </select>
1.6 查询-多条件-动态条件查询
传输多个参数,条件参数的个数不一定
Mybatis对动态SQL有很强大的支撑:
if 在sql语句中进行逻辑判断
choose (when, otherwise)
trim (where, set)
foreach
<where>作用:
替换where关键字
会动态的去掉第一个条件前的 and
如果所有的参数没有值则不加where关键字
<select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand <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>
1.7 查询-单条件-动态条件查询
处理单条件动态查询的标签应该具备什么特性?
select * from tb_brand <where> // 能够智能的判断当前是否需要添加where , 能够把多余的and 的删除 <choose> <when test="属性!=null and 属性!='' "> 列名称 = #{属性值} </when> <when test="属性!=null and 属性!='' "> 列名称 = #{属性值} </when> </choose> <otherwise> 2=2 </otherwise> </where>
<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>
1.8 查询-基础添加
<insert id="insertBrand">
insert into 表名称 values(null,#{实体的属性名称},... ...)
</insert>
编写接口方法
在
BrandMapper
接口中定义添加方法。/** * 添加 */ void add(Brand brand);
在.xml
映射配置文件中编写添加数据的statement
<insert id="add"> insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}); </insert>
参数:除了id之外的所有的数据。id对应的是表中主键值,而主键我们是 自动增长 生成的。
1.9 添加-主键返回
什么场景下需要返回主键?
在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。
<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>
在 insert 标签上添加如下属性:
useGeneratedKeys:是够获取自动增长的主键值。true表示获取
keyProperty :指定将获取到的主键值封装到哪儿个属性里
1.10 修改-修改全部字段
<update id='updateBrand'>
update tb_brand
<set>
<if test="status!=null">
status=#{status},
</if>
<if test="brandName!=null and brandName!='' ">
brand_name=#{brandName}
</if>
</set>
where
id=#{id}
</update>
1.11 修改-修改动态字段
<if test="brandName != null and brandName != ''"> brand_name = #{brandName}, </if>
set 标签可以用于动态包含需要更新的列,忽略其它不更新的列。
<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>
1.12 删除-删除一个
<delete id="deleteBrandById" > delete from tb_brand where id=#{id} <!--delete from tb_brand where id in(?,?,?,?)--> </delete> <delete id="deleteBrandById" > delete from tb_brand where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete>
1.13 删除-批量删除
编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了
foreach
标签供我们使用foreach 标签
用来迭代任何可迭代的对象(如数组,集合)。
collection 属性:
mybatis会将数组参数,封装为一个Map集合。
默认:array = 数组
使用@Param注解改变map集合的默认key的名称
item 属性:本次迭代获取到的元素。
separator 属性:集合项迭代之间的分隔符。
foreach
标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次
close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次
接口中定义删除多行数据的方法。
/** * 批量删除 */ void deleteByIds(@Param("ids") int[] ids);
<delete id="deleteByIds"> delete from tb_brand where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> ; </delete>
假如数组中的id数据是{1,2,3},那么拼接后的sql语句就是:
delete from tb_brand where id in (1,2,3);