添加数据并返回主键
添加数据就是常规操作了。我们需要返回主键的用途举个例子就是当用户提交订单的时候,我们需要返回一个订单号。(订单号作为主键)
void add(Brand brand);
然后映射文件
<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>
对于支持自动生成记录主键的数据库,如:MySQL,SQL Server,此时设置useGeneratedKeys参数值为true,在执行添加记录之后可以获取到数据库自动生成的主键ID
测试代码
public static void testAdd() throws IOException {
int status = 1;
String companyName = "波导手机";
String brandName = "波导";
String description = "手机中的战斗机";
int orderd = 100;
Brand brand = new Brand();
brand.setStatus(status);
// brand.setCompanyName(companyName);
// brand.setDescription(description);
// brand.setOrdered(orderd);
// brand.setBrandName(brandName);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
// SqlSession sqlSession1 = sqlSessionFactory.openSession(true);这样也可以设置为事务自动提交
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.add(brand);
Integer id = brand.getId();
System.out.println(id);
//提交事务
sqlSession.commit();
sqlSession.close();
}
修改数据
之前我们主要是在查询方面做相关得操作。现在我们进行一个添加数据的操作。
先在接口中写一个方法
// 完成一个修改的功能
int update(Brand brand);
然后再映射文件中编写具体的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标签
当在 update 语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值
然后测试代码
public static void testupdate() throws IOException {
int status = 1;
String companyName = "波导手机";
String brandName = "波导";
String description = "波导手机手机中的战斗机";
int orderd = 200;
int id = 5;
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setDescription(description);
brand.setOrdered(orderd);
brand.setBrandName(brandName);
brand.setId(id);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
// SqlSession sqlSession1 = sqlSessionFactory.openSession(true);这样也可以设置为事务自动提交
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
// brandMapper.add(brand);
int count = brandMapper.update(brand);
//获取到影响的行数
// Integer id = brand.getId();
// System.out.println(id);
//提交事务
sqlSession.commit();
sqlSession.close();
}
如果我们不手动设置事务提交的话,那么是添加不成功的,默认的话,mybatis会默认为手动提交方式,如果不手动提交,mybatis会回滚事务。