4.1.6 添加数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ewi7FVGy-1663854167487)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829151644117.png)]
1. 接口方法
/*
* 添加信息
* */
void add(Brand brand);
2. SQL语句
<insert id="add">
insert into tb_brand(brand_name,company_name,ordered,description,status)
values(#{brandName},#{companyName},#{ordered},#{description},#{status});
</insert>
3. 测试方法
/**
* 添加信息
* */
@Test
public void testAdd() throws IOException {
// 接受参数
int status = 1;
String brandName = "惠普电脑";
String companyName = "惠普";
String description = "光影精灵";
int ordered = 1000;
// 封装对象
Brand brand = new Brand();
brand.setBrandName(brandName);
brand.setStatus(status);
brand.setDescription(description);
brand.setOrdered(ordered);
brand.setCompanyName(companyName);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// SqlSession sqlSession = sqlSessionFactory.openSession();
// 设置自动提交事务,便不需要手动提交事务了
SqlSession sqlSession = sqlSessionFactory.openSession(true);
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.add(brand);
// 手动提交事务
// sqlSession.commit();
sqlSession.close();
}
测试结果如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ALQr9zD8-1663854167487)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829153536750.png)]
4. 添加 - 主键返回
数据添加成功之后,有时需要获得插入数据库数据的主键(主键是自增长)
-
sql语句修改成如下,在insert标签添加如下属性
<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>
- useGeneratedKeys : 能够获取自动增长的主键值。true表示获取
- keyProperty : 指定将获取到的主键值封装到哪个属性中。本案例是封装到brand 的 id 属性中
-
测试中能够打印出来则表示获取成功
4.1.7 修改
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KLiINy6G-1663854167488)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829155943793.png)]
1. 接口方法
/*
* 修改信息
* */
int update(Brand brand);
2. 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>
set标签可以用于动态包含需要更新的列,而忽略其他不更新的列
3. 测试方法
修改指定id参数所对应的数据的相关列数据
当指定的参数为多行数据同时拥有时,会同时修改
/*
* 修改信息
* */
@Test
public void testUpdate() throws IOException {
// 接受参数
int status = 1;
String brandName = "惠普电脑";
String companyName = "惠普";
String description = "光影精灵";
int ordered = 200;
int id = 5;
// 封装对象
Brand brand = new Brand();
// brand.setStatus(status);
// brand.setBrandName(brandName);
// brand.setDescription(description);
brand.setOrdered(ordered);
// brand.setCompanyName(companyName);
brand.setId(id);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
int count = brandMapper.update(brand);
System.out.println(count);
sqlSession.close();
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Y95Kosb-1663854167488)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829163859834.png)]
4.1.8 删除某一行数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dtlbh3r2-1663854167489)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829164049339.png)]
1. 接口方法
/*
* 根据id,删除某一行数据
* */
void deleteById(Brand brand);
2. SQL语句
<!-- 根据id,删除一行数据 -->
<delete id="deleteById">
delete from tb_brand where id = #{id};
</delete>
3. 测试方法
/*
* 根据id,删除某一行数据
* */
@Test
public void testDeleteById() throws IOException {
// 接收参数
int id = 7;
//
Brand brand = new Brand();
brand.setId(id);
//
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.deleteById(brand);
sqlSession.close();
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2Qc0E6w2-1663854167490)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829165236694.png)]
4.1.9 批量删除
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XVgEBrEQ-1663854167490)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829165502817.png)]
1. 接口方法
/*
* 批量删除数据
* */
void deleteByIds(int[] ids);
2. SQL语句
编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了 foreach 标签供使用
foreach 标签 —— 用来迭代任何可迭代的对象,如数组、集合
-
collection 属性
- mybatis 会将数组参数封装为一个 Map 集合
- 默认:array = 数组
- 使用 @Param 注解改变 map 集合的默认 key 名称
- mybatis 会将数组参数封装为一个 Map 集合
-
item 属性 : 本次迭代获取到的元素
-
separator 属性 : 集合项迭代之间的分隔符。
foreach
标签不会错误的添加多余的分隔符,即最后一次迭代不会加分隔符 -
open 属性 : 该属性值是在拼接 SQL 语句之前拼接的语句,只会拼接一次
-
close 属性 : 该属性值是在拼接 SQL 语句之后拼接的语句,只会拼接一次
<!-- 批量删除 -->
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
若 ids = {1,2,3}。则拼接后的sql语句为:
delete from tb_brand where id in (1,2,3);
3. 测试方法
/**
* 批量删除
* */
@Test
public void testDeleteByIds() throws IOException {
//
int[] ids = {9,10,11};
//
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.deleteByIds(ids);
sqlSession.close();
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yczioUhV-1663854167490)(C:\Users\18517\AppData\Roaming\Typora\typora-user-images\image-20220829223310417.png)]
4.1.10 Mybatis 参数传递
Mybatis 接口方法可以接收各种参数。
- 多个参数
- 单个参数:单个参数又可以是如下类型
- POJO 类型
- Map 集合类型
- Collection 集合类型
- List 集合类型
- Array 类型
- 其他类型
1. 多个参数
接收多个参数需要使用 @Param 注解
User select(@Param("username") String username,@Param("password") String password);
<select id="select" resultType="user">
select *
from tb_user
where
username=#{username}
and password=#{password}
</select>
接口参数是多个时,每个参数都是用 @Param 注解
2. 单个参数
-
POJO类型
直接使用。要求 属性名 与 参数占位符名称 一致
-
Map 集合类型
直接使用。要求 map集合的键名 与 参数占位符名称 一致
-
Collection 集合类型、List 集合类型、Array 类型
Mybatis 会将集合封装到 map 集合中。
可以使用 @Param 注解替换map集合中默认的键名
-
其他类型
如 int 类型。参数占位符名称叫什么都可用,但尽量见名知意
4.2 注解实现 CRUD
使用注解进行开发。如下:
@Select(value = "select * from tb_user where id = #{id}")
public User select(int id);
注解是用来替换映射配置文件方式配置的,故用了注解,便不需要再在映射文件书写对应的 statement
对 CRUD 操作有对应的注解
- 查询 : @Select
- 添加 : @Insert
- 修改 : @Update
- 删除 : @Delete
注意:
注解完成简单功能方便,复杂的功能会很麻烦。故用配置文件完成复杂功能