添加数据并返回主键

添加数据就是常规操作了。我们需要返回主键的用途举个例子就是当用户提交订单的时候,我们需要返回一个订单号。(订单号作为主键)

  1. void add(Brand brand);

然后映射文件

  1. <insert id="add" useGeneratedKeys="true" keyProperty="id">
  2. insert into tb_brand (brand_name, company_name, ordered, description, status)
  3. values (#{brandName},#{companyName},#{ordered},#{description},#{status});
  4. </insert>

对于支持自动生成记录主键的数据库,如:MySQL,SQL Server,此时设置useGeneratedKeys参数值为true,在执行添加记录之后可以获取到数据库自动生成的主键ID
测试代码

  1. public static void testAdd() throws IOException {
  2. int status = 1;
  3. String companyName = "波导手机";
  4. String brandName = "波导";
  5. String description = "手机中的战斗机";
  6. int orderd = 100;
  7. Brand brand = new Brand();
  8. brand.setStatus(status);
  9. // brand.setCompanyName(companyName);
  10. // brand.setDescription(description);
  11. // brand.setOrdered(orderd);
  12. // brand.setBrandName(brandName);
  13. String resource = "mybatis-config.xml";
  14. InputStream inputStream = Resources.getResourceAsStream(resource);
  15. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  16. SqlSession sqlSession = sqlSessionFactory.openSession();
  17. // SqlSession sqlSession1 = sqlSessionFactory.openSession(true);这样也可以设置为事务自动提交
  18. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  19. brandMapper.add(brand);
  20. Integer id = brand.getId();
  21. System.out.println(id);
  22. //提交事务
  23. sqlSession.commit();
  24. sqlSession.close();
  25. }

修改数据

之前我们主要是在查询方面做相关得操作。现在我们进行一个添加数据的操作。

先在接口中写一个方法

  1. // 完成一个修改的功能
  2. int update(Brand brand);

然后再映射文件中编写具体的sql语句

  1. <update id="update">
  2. update tb_brand
  3. <set>
  4. <if test="brandName != null and brandName != ''">
  5. brand_name = #{brandName},
  6. </if>
  7. <if test="companyName != null and companyName !=''">
  8. company_name = #{companyName},
  9. </if>
  10. <if test="ordered != null">
  11. ordered = #{ordered},
  12. </if>
  13. <if test="description != null and description != ''">
  14. description = #{description},
  15. </if>
  16. <if test="status !=null">
  17. status = #{status}
  18. </if>
  19. </set>
  20. where id = #{id}
  21. </update>

一个智能的set标签
当在 update 语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值

然后测试代码

  1. public static void testupdate() throws IOException {
  2. int status = 1;
  3. String companyName = "波导手机";
  4. String brandName = "波导";
  5. String description = "波导手机手机中的战斗机";
  6. int orderd = 200;
  7. int id = 5;
  8. Brand brand = new Brand();
  9. brand.setStatus(status);
  10. brand.setCompanyName(companyName);
  11. brand.setDescription(description);
  12. brand.setOrdered(orderd);
  13. brand.setBrandName(brandName);
  14. brand.setId(id);
  15. String resource = "mybatis-config.xml";
  16. InputStream inputStream = Resources.getResourceAsStream(resource);
  17. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  18. SqlSession sqlSession = sqlSessionFactory.openSession();
  19. // SqlSession sqlSession1 = sqlSessionFactory.openSession(true);这样也可以设置为事务自动提交
  20. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  21. // brandMapper.add(brand);
  22. int count = brandMapper.update(brand);
  23. //获取到影响的行数
  24. // Integer id = brand.getId();
  25. // System.out.println(id);
  26. //提交事务
  27. sqlSession.commit();
  28. sqlSession.close();
  29. }

如果我们不手动设置事务提交的话,那么是添加不成功的,默认的话,mybatis会默认为手动提交方式,如果不手动提交,mybatis会回滚事务。