目录

如上图所示,在查询时只能选择 品牌名称、当前状态、企业名称 这三个条件中的一个,但是用户到底选择哪儿一个,我们并不能确定。这种就属于单个条件的动态SQL语句。
这种需求需要使用到 choose(when,otherwise)标签 实现, 而 choose 标签类似于Java 中的switch语句。
通过一个案例来使用这些标签
在 BrandMapper 接口中定义单条件查询的方法。
- /**
- * 单条件动态查询
- * @param brand
- * @return
- */
- List
selectByConditionSingle(Brand brand);
在 BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType
- <select id="selectByConditionSingle" resultMap="brandResultMap">
- select *
- from tb_brand
- <where>
- <choose>
- <when test="status != null">
- status = #{status}
- when>
- <when test="companyName != null and companyName != '' ">
- company_name like #{companyName}
- when>
- <when test="brandName != null and brandName != ''">
- brand_name like #{brandName}
- when>
- choose>
- where>
- select>
在 test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法
- @Test
- public void testSelectByConditionSingle() throws IOException {
- //接收参数
- int status = 1;
- String companyName = "华为";
- String brandName = "华为";
-
- // 处理参数
- companyName = "%" + companyName + "%";
- brandName = "%" + brandName + "%";
-
- //封装对象
- Brand brand = new Brand();
- //brand.setStatus(status);
- brand.setCompanyName(companyName);
- //brand.setBrandName(brandName);
-
- //1. 获取SqlSessionFactory
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //2. 获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //3. 获取Mapper接口的代理对象
- BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
- //4. 执行方法
- List
brands = brandMapper.selectByConditionSingle(brand); - System.out.println(brands);
-
- //5. 释放资源
- sqlSession.close();
- }
执行测试方法结果如下: 

如上图是我们平时在添加数据时展示的页面,而我们在该页面输入想要的数据后添加 提交 按钮,就会将这些数据添加到数据库中。接下来我们就来实现添加数据的操作。
编写接口方法
参数:除了id之外的所有的数据。id对应的是表中主键值,而主键我们是 ==自动增长== 生成的。
编写测试方法并执行
明确了该功能实现的步骤后,接下来我们进行具体的操作。
在 BrandMapper 接口中定义添加方法。
- /**
- * 添加
- */
- void add(Brand brand);
在 BrandMapper.xml 映射配置文件中编写添加数据的 statement
- <insert id="add">
- insert into tb_brand (brand_name, company_name, ordered, description, status)
- values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
- insert>
在 test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法
- @Test
- public void testAdd() 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 resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //2. 获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //SqlSession sqlSession = sqlSessionFactory.openSession(true); //设置自动提交事务,这种情况不需要手动提交事务了
- //3. 获取Mapper接口的代理对象
- BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
- //4. 执行方法
- brandMapper.add(brand);
- //提交事务
- sqlSession.commit();
- //5. 释放资源
- sqlSession.close();
- }
执行结果如下:
在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。
比如:添加订单和订单项,如下图就是京东上的订单
订单数据存储在订单表中,订单项存储在订单项表中。
添加订单数据
添加订单项数据,订单项中需要设置所属订单的id
明白了什么时候 主键返回 。接下来我们简单模拟一下,在添加完数据后打印id属性值,能打印出来说明已经获取到了。
我们将上面添加品牌数据的案例中映射配置文件里 statement 进行修改,如下
- <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>
在 insert 标签上添加如下属性:
useGeneratedKeys:是够获取自动增长的主键值。true表示获取
keyProperty :指定将获取到的主键值封装到哪儿个属性里