Mybatis(5)添加和修改


当用户提交数据时,不应该让用户提交id,所以参数就是除了id之外的所有值。
返回值我们用void,因为返回的东西我们可以用异常处理代替。

当用户点击提交按钮时,把这一坨数据封装成对象就可以执行insert语句添加到数据库里边了。
1. 添加
- 首先在Mapper中写上方法
void add(Brand brand);
- 然后在sql映射配置文件写插入的语句(这里的参数名和实体类是对应的,而不是字段名,因为传参过来的参数是实体类的)
<!-- 添加-->
<insert id="add">
insert into tb_brand(brand_name, company_name, ordered, description, status)
values (#{brandName},#{companyName},#{ordered},#{description},#{status})
</insert>
- 编写测试用例
@Test
public void add() throws IOException {
//接收参数
int status=1;
String companyName="波导手机";
String brandName="波导";
String description="手机中的战斗机";
int ordered=100;
//封装对象
Brand brand=new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setDescription(description);
brand.setOrdered(ordered);
//1.获取sqlSessionFactory对象
String resources ="mybatis-config.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取mapper接口的处理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
// List<Brand> brands = mapper.selectByCondition(status, companyName, brandName);
mapper.add(brand);
//5.释放资源
sqlSession.close();
}
然后发现添加到数据库但是数据库并没有查询到这个信息,通过查找日志发现,因为事务是默认关闭提交的,这里需要手动提交一下。

sqlSession.commit();
如果你不想手动提交你可以给sqlSession一个参数,这个参数默认为false需要手动提交,设置为true是自动提交。
SqlSession sqlSession = sqlSessionFactory.openSession(true);
主键返回(返回主键的值)

- 在标签中添加两个属性useGeneratedKeys为true并且 keyProperty绑定id名称
<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>
- 在测试用例中get方法获取即可
Integer id = brand.getId();
System.out.println(id);
3.更新全部字段

- 在mapper接口中,定义方法
int update(Brand brand);
- 在sql映射配置文件中写sql语句
<!-- 更新-->
<update id="update">
update tb_brand
set brand_name=#{brandName},
company_name=#{companyName},
ordered=#{ordered},
description=#{description},
status=#{status}
where id=#{id}
</update>
3.编写测试用例
@Test
public void testUpdate() throws IOException {
//接收参数
int status=1;
String companyName="波导手机";
String brandName="波导";
String description="波导手机,手机中的战斗机";
int ordered=200;
int id=6;
//封装对象
Brand brand=new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setDescription(description);
brand.setOrdered(ordered);
brand.setId(id);
//1.获取sqlSessionFactory对象
String resources ="mybatis-config.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//2.获取SqlSession对象
// SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取mapper接口的处理对象
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
// List<Brand> brands = mapper.selectByCondition(status, companyName, brandName);
int update = mapper.update(brand);
System.out.println(update);
// sqlSession.commit();
//5.释放资源
sqlSession.close();
}
4.更新动态字段
为什么要动态更新字段呢?因为假如是个修改密码页面,如果我要修改密码提交后只会提交密码和id,其他的字段并没有用上,它们没有用到的字段会为空,所以我们需要修改动态字段。

<!-- 更新-->
<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>
where id=#{id}
</update>
我们动态更新字段就可以这么写,就是加了一堆if判断
但是呢,如果这些条件只有 一个被满足时sql语句就会变成 update tb_brand set brand_name=#{brandName}, where id=#{id} 多出来的逗号显然不符合语法。
如果这些条件一个都不满足就会变成 update tb_brand set where id=#{id}显然不符合语法。
那有什么解决办法呢?
mybatis提供了一个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>

浙公网安备 33010602011771号