• Mybatis实战练习四【单个条件(动态SQL)&添加数据】


    目录

    单个条件(动态SQL)

    编写接口方法、

    编写SQL语句

    编写测试方法

     添加数据

    编写接口方法

    编写SQL语句

    编写测试方法

    添加-主键返回


    单个条件(动态SQL

    如上图所示,在查询时只能选择 品牌名称当前状态企业名称 这三个条件中的一个,但是用户到底选择哪儿一个,我们并不能确定。这种就属于单个条件的动态SQL语句

    这种需求需要使用到 choose(when,otherwise)标签 实现, 而 choose 标签类似于Java 中的switch语句。

    通过一个案例来使用这些标签

     

    编写接口方法、

    BrandMapper 接口中定义单条件查询的方法。

    1. /**
    2. * 单条件动态查询
    3. * @param brand
    4. * @return
    5. */
    6. List selectByConditionSingle(Brand brand);

    编写SQL语句

    BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType

    1. <select id="selectByConditionSingle" resultMap="brandResultMap">
    2. select *
    3. from tb_brand
    4. <where>
    5. <choose>
    6. <when test="status != null">
    7. status = #{status}
    8. when>
    9. <when test="companyName != null and companyName != '' ">
    10. company_name like #{companyName}
    11. when>
    12. <when test="brandName != null and brandName != ''">
    13. brand_name like #{brandName}
    14. when>
    15. choose>
    16. where>
    17. select>

    编写测试方法

    test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

    1. @Test
    2. public void testSelectByConditionSingle() throws IOException {
    3. //接收参数
    4. int status = 1;
    5. String companyName = "华为";
    6. String brandName = "华为";
    7. // 处理参数
    8. companyName = "%" + companyName + "%";
    9. brandName = "%" + brandName + "%";
    10. //封装对象
    11. Brand brand = new Brand();
    12. //brand.setStatus(status);
    13. brand.setCompanyName(companyName);
    14. //brand.setBrandName(brandName);
    15. //1. 获取SqlSessionFactory
    16. String resource = "mybatis-config.xml";
    17. InputStream inputStream = Resources.getResourceAsStream(resource);
    18. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    19. //2. 获取SqlSession对象
    20. SqlSession sqlSession = sqlSessionFactory.openSession();
    21. //3. 获取Mapper接口的代理对象
    22. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    23. //4. 执行方法
    24. List brands = brandMapper.selectByConditionSingle(brand);
    25. System.out.println(brands);
    26. //5. 释放资源
    27. sqlSession.close();
    28. }

    执行测试方法结果如下:

    添加数据

    如上图是我们平时在添加数据时展示的页面,而我们在该页面输入想要的数据后添加 提交 按钮,就会将这些数据添加到数据库中。接下来我们就来实现添加数据的操作。

    • 编写接口方法

     

    • 参数:除了id之外的所有的数据。id对应的是表中主键值,而主键我们是 ==自动增长== 生成的。

     

    • 编写测试方法并执行

    明确了该功能实现的步骤后,接下来我们进行具体的操作。  

     

    编写接口方法

    BrandMapper 接口中定义添加方法。

    1. /**
    2. * 添加
    3. */
    4. void add(Brand brand);

    编写SQL语句

    BrandMapper.xml 映射配置文件中编写添加数据的 statement

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

    编写测试方法

    test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

    1. @Test
    2. public void testAdd() throws IOException {
    3. //接收参数
    4. int status = 1;
    5. String companyName = "波导手机";
    6. String brandName = "波导";
    7. String description = "手机中的战斗机";
    8. int ordered = 100;
    9. //封装对象
    10. Brand brand = new Brand();
    11. brand.setStatus(status);
    12. brand.setCompanyName(companyName);
    13. brand.setBrandName(brandName);
    14. brand.setDescription(description);
    15. brand.setOrdered(ordered);
    16. //1. 获取SqlSessionFactory
    17. String resource = "mybatis-config.xml";
    18. InputStream inputStream = Resources.getResourceAsStream(resource);
    19. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    20. //2. 获取SqlSession对象
    21. SqlSession sqlSession = sqlSessionFactory.openSession();
    22. //SqlSession sqlSession = sqlSessionFactory.openSession(true); //设置自动提交事务,这种情况不需要手动提交事务了
    23. //3. 获取Mapper接口的代理对象
    24. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    25. //4. 执行方法
    26. brandMapper.add(brand);
    27. //提交事务
    28. sqlSession.commit();
    29. //5. 释放资源
    30. sqlSession.close();
    31. }

    执行结果如下:  

    添加-主键返回

    在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。

    比如:添加订单和订单项,如下图就是京东上的订单

     

    订单数据存储在订单表中,订单项存储在订单项表中。

    • 添加订单数据

     

    添加订单项数据,订单项中需要设置所属订单的id  

    明白了什么时候 主键返回 。接下来我们简单模拟一下,在添加完数据后打印id属性值,能打印出来说明已经获取到了。

    我们将上面添加品牌数据的案例中映射配置文件里 statement 进行修改,如下

    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>

    在 insert 标签上添加如下属性:

    • useGeneratedKeys:是够获取自动增长的主键值。true表示获取

    • keyProperty :指定将获取到的主键值封装到哪儿个属性里

  • 相关阅读:
    62. 不同路径-动态规划-双百代码
    文件包含漏洞学习小结
    电气工程的标准是什么
    Express+MongoDB服务端开发教程
    js的流程控制
    QT启动 一个进程-一个exe文件方法
    【C++设计模式之观察者模式:行为型】分析及示例
    软考高级论文真题“论湖仓一体架构及其应用”
    Optional源码解析与实践
    Julia累加和累乘
  • 原文地址:https://blog.csdn.net/m0_64550837/article/details/126603874