动态sql

        <!--数据动态sql处理 -->
        <select id="dynamicSql" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.GoodsEntity">
            <!--编写sql-->
            select * from t_goods
            <where>
                <!--test中是javabean的属性判断-->
                <if test="categoryId != null">
                    and category_id = #{categoryId}
                </if>
                <if test="currentPrice !=null">
                    <!-- < &lt > &gt & &amp -->
                    and current_price &lt; #{currentPrice}
                </if>
            </where>
        </select>
    public void DynamicSql() throws Exception {
        SqlSession sqlSession = null;
        try {
            //获取sql对象
            sqlSession = MybatisUtils.openSession();
            //实例化goods,插入数据
            Map map = new HashMap();
            map.put("categoryId",43);
            map.put("currentPrice",200f);
            //执行sql
            List<GoodsEntity> list = sqlSession.selectList("goods.dynamicSql",map);
            //提交数据
            sqlSession.commit();
            System.out.println(list);
            //查看连接状态
            Connection conn = MybatisUtils.getConnection(sqlSession);

        }catch (Exception e){
            sqlSession.rollback();//数据回滚
            throw e;
        }finally {
            MybatisUtils.release(sqlSession);
        }
    }
原文地址:https://www.cnblogs.com/wuheng-123/p/13836129.html