设为首页 加入收藏

TOP

Java后端05(初识MyBatis)(四)
2023-08-26 21:11:24 】 浏览:124
Tags:Java 后端 初识 MyBatis
gt; </select>

测试类

@Test
public void test01(){
    List<Product> productList = sqlSession.selectList("listProduct");
    for (Product product : productList) {
        System.out.println(product);
    }
}

@Test
public void test02(){
    Map<String,String> params = new HashMap<>();
    params.put("productName","1");
    List<Product> productList = sqlSession.selectList("listProduct",params);
    for (Product product : productList) {
        System.out.println(product);
    }
}

多条件查询

<!--多条件查询的矛盾-->
<!--    where 标签会进行自动判断,如果所有的 if 条件都不成立,那么 sql 语句中不会出现 where 关键字
        只要有一个条件成立,就会自动去除冗余的 and,并自动添加 where-->
<select id="listProduct" resultType="Product">
    select * from product
    <where>
        <if test="productName != null">
            and productName like concat('%',#{productName},'%')
        </if>
        <if test="price != 0">
            and price > #{price}
        </if>
    </where>
</select>

测试类

@Test
public void test01(){
    Map<String,Object> params = new HashMap<>();
    params.put("productName","1");
    params.put("price",0);
    List<Product> productList = sqlSession.selectList("listProduct",params);
    for (Product product : productList) {
        System.out.println(product);
    }
}

动态sql更新

<update id="updateProduct">
    update product
    <set>
        <if test="productName != null">
            productName = #{productName},
        </if>
        <if test="price != null">
            price = #{price},
        </if>
        <if test="stock != null">
            stock = #{stock},
        </if>
    </set>
    where productId = #{productId}
</update>

测试类

@Test
public void test01(){
    Map<String,Object> params = new HashMap<>();
    params.put("productId","1");
    params.put("productName","product");
    params.put("price","10086");
    params.put("stock","10000");
    sqlSession.update("updateProduct",params);
    sqlSession.commit();
}

when otherwise 标签

<mapper namespace="com.iweb.entity">
<!--    mybatis 没有 else 标签,只能使用 when otherwise 表示 else,如果提供了任何条件,则进行条件铲鲟,如果没有提供任何条件参数,则使用 otherwise 作为条件-->
    <select id="listProduct" resultType="Product">
        select * from product
        <where>
            <choose>
                <when test="productName != null">
                    and name like concat('%',#{productName},'%')
                </when>
                <when test="price != null">
                    and price > #{price}
                </when>
                <otherwise>
                    and id > 5
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>

使用注解方式实现简单的sql

mapper接口文件(一定要注册!!!!!!)

/**
 * 通过接口(底层是java的JDK动态代理)实现 mybatis 调用
 * 开发人员只需要关心接口,实现类由 mybatis 动态生成
 * 1. 注解开发方式: 适用于简单场景(注解场景下编写一对多 多对一 或者是动态sql非常麻烦)
 * 2. xml配置文件开发方式:适用于所有场景(推荐)
 * @author te9uila
 * @since 2023/8/5
 */
public interface ProductMapper {
    @Insert("insert into product values (#{productId},#{productName},#{price},#{stock},#{userId})")
    void add(Product product);
    @Delete("delete from product where productId = #{productId}")
    void delete(String id);
    @Select(&
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇quarkus依赖注入之八:装饰器(De.. 下一篇零基础尝试搭建docker和nacos环境

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目