ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

heim-mybatis基础-----------------------08

2020-02-29 13:05:57  阅读:236  来源: 互联网

标签:List 08 list vo add heim user mybatis select


代码:G:\CODE_MY\heimabase\mybatis\03\day03_eesy_02datasourceSQLMy

为什么出现动态的sql:我们根据条件查询的话传一个对象不确定条件是什么的时候要用到条件查询的。

/**
     * 根据传入参数条件
     * @param user 查询的条件:有可能有用户名,有可能有性别,也有可能有地址,还有可能是都有
     * @return
     */
    List<User> findUserByCondition(User user);

sql语句的内容是无关大小写的,实体类就是#对应的要大小写敏感。

---------------------------------------------------01----------------------------------------------------------------------

如何解决where 1=1问题?

老的写法:

<select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user where 1=1
        <if test="userName != null">
          and username = #{userName}
        </if>
        <if test="userSex != null">
            and sex = #{userSex}
        </if>
    </select>

新的写法:

<select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user
        <where>
            <if test="userName != null">
                and username = #{userName}
            </if>
            <if test="userSex != null">
                and sex = #{userSex}
            </if>
        </where>
    </select>

-----------------------------------------------------02---------------------------------------------------------------------

<!-- 根据queryvo中的Id集合实现查询用户列表 -->
    <select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
        <include refid="defaultUser"></include>
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in (" close=")" item="uid" 这个是uid 
 separator=",">
                    #{uid} 这个也得是uid
                </foreach>
            </if>
        </where>
    </select>
  /**
     * 测试foreach标签的使用
     */
    @Test
    public void testFindInIds(){
        QueryVo vo = new QueryVo();
        List<Integer> list = new ArrayList<Integer>();
        list.add(41);
        list.add(42);
        list.add(46);
        vo.setIds(list);


        //5.执行查询所有方法
        List<User> users = userDao.findUserInIds(vo);
        for(User user : users){
            System.out.println(user);
        }

    }

知识点:

抽取重复的sql语句。

  <!-- 了解的内容:抽取重复的sql语句-->
    <sql id="defaultUser">
        select * from user
    </sql>

-----------------------------------------------------03---------------------------------------------------------------------

菜鸟级别选手 发布了322 篇原创文章 · 获赞 11 · 访问量 1万+ 私信 关注

标签:List,08,list,vo,add,heim,user,mybatis,select
来源: https://blog.csdn.net/qq_28764557/article/details/104572115

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有