Mybatis实现增删改查 -- Mybatis快速入门保姆级教程(二)(2)

简介: Mybatis实现增删改查 -- Mybatis快速入门保姆级教程(二)

5. 查询–条件查询

  1. 操作步骤

  1. 编写接口方法:Mapper接口
public interface BrandMapper {
    /**
     * 条件查询
     *  * 参数接收
     *      1. 散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
     *      2. 对象参数:对象的属性名称要和参数占位符名称一致
     *      3. map集合参数:map集合的键值要和参数占位符名称一致
     *
     */
//方式一:接收散装参数
    List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
//放肆二:接收对象参数
    //List<Brand> selectByCondition(Brand brand);
//方式三:接收map集合参数
    //List<Brand> selectByCondition(Map map);
}
  1. 编写SQL语句:SQL映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
    namespace:名称空间
-->
<mapper namespace="org.example.mapper.BrandMapper">
    <resultMap id="brandResultMap" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>
<!--
    条件查询
-->
    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
          and company_name like #{companyName}
          and brand_name like #{brandName}
    </select>
</mapper>
  1. 编写测试样例
public class MyBatisTest {
    @Test
    public void testSelectByCondition() 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);
//方式三:接收散装参数
//        Map map = new HashMap();
//        map.put("status" , status);
//        map.put("companyName", companyName);
//        map.put("brandName" , 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<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
//方式二:接收对象参数
        //List<Brand> brands = brandMapper.selectByCondition(brand);
//方式三:接收集合参数
        //List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);
        //5. 释放资源
        sqlSession.close();
    }
}
  1. 运行结果

  1. 小结

6.查询–多条件动态查询

(在多条件查询的基础上修改BrandMapper.xml中的SQL语句的书写部分)

  1. 情景导入

在多条件查询的基础案例中我们发现,查询多条件的sql语句需要获取当前状态status,企业名称companyname,品牌名称brandname三个参数,缺一不可。如果只输入品牌名称,该查询无法完成,不符合实际需求,报错实例(在传参代码书写时将status和companyname的部分注释掉)如下:

因此,我们需要动态SQL来辅助我们解决类似的问题。动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

  1. 操作步骤

  1. 编写SQL语句:SQL映射文件
<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where
            <if test="status != null">
                status = #{status}
            </if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName != '' ">
                and brand_name like #{brandName}
            </if>
    </select>
  1. 编写测试样例

  1. 运行结果

  1. 存在问题

当我们把where后面的第一个参数也注释掉我们发现程序就会报错,原因查看运行结果中的sql语句就一目了然了,如下:

(sql语句)

(测试样例)

(运行结果)

  1. 改进方式一

(sql语句书写改进)

(运行结果)

  1. 改进方式二

(sql语句书写改进)

(运行结果)

  1. 小结

7.条件查询–单条件动态查询

  1. 操作步骤

  1. 编写接口方法:Mapper接口
List<Brand> selectByConditionSingle(Brand brand);
  1. 编写SQL语句:SQL映射文件(注意区分,没用and连接)

otherwrise:不传参搜索的情况,相当于switch语句中的default

<select id="selectByConditionSingle" resultType="org.example.pojo.Brand">
        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>
            <otherwise>
                1 = 1
            </otherwise>
        </choose>
        </where>
    </select>
  1. 编写测试样例
@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<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);
        //5. 释放资源
        sqlSession.close();
    }
}
  1. 运行结果

相关文章
|
4月前
|
存储 Java 数据库连接
MyBatis-Plus 基础操作指南:实现高效的增删改查
MyBatis-Plus 基础操作指南:实现高效的增删改查
193 0
|
2月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
27 0
|
5天前
|
SQL Java 数据库连接
【mybatis】动态sql之批量增删改查
【mybatis】动态sql之批量增删改查
10 0
|
17天前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
16 2
|
2月前
|
XML Java 数据库连接
Mybatis-Plus学习小项目及详细教程
Mybatis-Plus学习小项目及详细教程
|
3月前
|
XML Java 数据库连接
【JavaEE进阶】 MyBatis使用XML实现增删改查
【JavaEE进阶】 MyBatis使用XML实现增删改查
|
3月前
|
Java 关系型数据库 数据库连接
【JavaEE进阶】 MyBatis使用注解实现增删改查
【JavaEE进阶】 MyBatis使用注解实现增删改查
|
3月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
3月前
|
SQL Java 数据库连接
【MyBatisPlus】通俗易懂 快速入门 详细教程
【MyBatisPlus】通俗易懂 快速入门 详细教程
83 0
|
3月前
|
SQL Java 数据库连接
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
55 0