MyBatis学习(五)

视频观看地址:http://edu.51cto.com/course/14674.html?source=sohtml

一、动态sql

1.一、if

需求:查询所有用户,若是用户填写了姓名,按照姓名的模糊查询,若是用户填写了姓名和年龄,则按姓名和年龄一块儿查找java

一、接口定义sql

public List<User> querybyUser(User vo) throws Exception;

二、mapper.xml配置数组

<select id = "querybyUser" resultMap="UserMap">
    select * from tb_user where 1=1
    <if test="userName!=null and userName!=''">
       and user_Name like #{userName}
    </if>
    <if test = "age!=null">
        and age =#{age}
    </if>
  </select>

三、测试方法mybatis

@Test
    public void testIf()throws Exception{
        User user = new User();
        user.setUserName(null);
        user.setAge(17);
        List<User> list = userMapper.querybyUser(user);
        for (User u : list) {
            System.out.println(u);
        }
    }

四、日志oracle

DEBUG - Opening JDBC Connection
DEBUG - Created connection 276327391.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - ==>  Preparing: select * from tb_user where 1=1 and age =? 
DEBUG - ==> Parameters: 17(Integer)
DEBUG - <==      Total: 1
User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - Returned connection 276327391 to pool.

1.二、where

在前面的sql中的咱们认为的定义了一个where 1=1这样的东西,此时咱们能够经过mybatis的where标签来搞定app

<select id = "querybyUser" resultMap="UserMap">
    select * from tb_user 
    <where>
        <if test="userName!=null and userName!=''">
            and user_Name like #{userName}
        </if>
        <if test = "age!=null">
            and age =#{age}
        </if>
    </where>

  </select>

where :自动根据需求生成,而且能够剔除不须要andide

直接测试便可测试

1.三、foreach

另一个动态 SQL 通用的必要操做是迭代一个集合, 一般是构建在 IN 条件中的。ui

接口方法定义

public List<User> querybyIn(QueryUser vo) throws Exception;

mapper.xml

<select id = "querybyIn" resultMap="UserMap">
        select * from tb_user
        <where>
            <if test="userids!=null">
                <!-- 
                    collection:要迭代集合或者数组
                -->
                <foreach collection="userids" open="userid in (" close=")" separator="," item="userid">
                    #{userid}
                </foreach>
            </if>
        </where>
  </select>

测试方法

@Test
    public void testforeach()throws Exception{
        QueryUser queryUser= new QueryUser();
        queryUser.setUserids(new int[]{8,9,2,3});
        List<User> list = userMapper.querybyIn(queryUser);
        for (User u : list) {
            System.out.println(u);
        }
    }

日志

DEBUG - Opening JDBC Connection
DEBUG - Created connection 1060925979.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - ==>  Preparing: select * from tb_user WHERE userid in ( ? , ? , ? , ? ) 
DEBUG - ==> Parameters: 8(Integer), 9(Integer), 2(Integer), 3(Integer)
DEBUG - <==      Total: 4
User [userid=8, userName=阿珂, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:06:14 CST 2018]
User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018]
User [userid=2, userName=张三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - Returned connection 1060925979 to pool.

1.四、Set

set 元素能够被用于动态包含更新的列,而不包含不需更新的(动态更新)

接口定义

public int dycUpdate(User vo) throws Exception;

mapper.xml定义

<update id="dycUpdate">
    update tb_user
    <set>
        <if test="userName!=null and userName!=''">
            user_name=#{userName},
        </if>
        <if test="pwd!=null and pwd!=''">
            pwd=#{pwd},
        </if>
        <if test="age!=null">
            age=#{age},
        </if>
        <if test="sex!=null">
            sex=#{sex},
        </if>
        <if test="birthday!=null">
            birthday=#{birthday},
        </if>
    </set>
    where userid=#{userid}
  </update>

测试方法

@Test
    public void testdycUpdate()throws Exception{
        User user = new User();
        user.setUserName("hello");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setUserid(8);
        userMapper.dycUpdate(user);
        sqlSession.commit();
    }

日志

DEBUG - Opening JDBC Connection
DEBUG - Created connection 340839523.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - ==>  Preparing: update tb_user SET user_name=?, sex=?, birthday=? where userid=? 
DEBUG - ==> Parameters: hello(String), 男(String), 2018-08-14 10:19:19.311(Timestamp), 8(Integer)
DEBUG - <==    Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Returned connection 340839523 to pool.

1.五、动态添加

接口定义

public int dycInsert(User vo) throws Exception;

mapper.xml文件

<insert id="dycInsert">
        insert into tb_user (
                userid,
                <trim suffixOverrides=",">
                    <if test="userName!=null and userName!=''">
                        user_name,
                    </if>
                    <if test="pwd!=null and pwd!=''">
                        pwd,
                    </if>
                    <if test="age!=null">
                        age,
                    </if>
                    <if test="sex!=null">
                        sex,
                    </if>
                    <if test="birthday!=null">
                        birthday,
                    </if>
                </trim>

        ) values(
            seq_user.nextval,
            <trim suffixOverrides=",">
                <if test="userName!=null and userName!=''">
                #{userName},
                </if>
                <if test="pwd!=null and pwd!=''">
                    #{pwd},
                </if>
                <if test="age!=null">
                    #{age},
                </if>
                <if test="sex!=null">
                    #{sex},
                </if>
                <if test="birthday!=null">
                    #{birthday},
                </if>
            </trim>

        )   
  </insert>

测试方法

@Test
    public void testdycInsert()throws Exception{
        User user = new User();
        user.setUserName("yui");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setUserid(8);
        userMapper.dycInsert(user);
        sqlSession.commit();
    }

日志

DEBUG - Opening JDBC Connection
DEBUG - Created connection 1341177488.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - ==>  Preparing: insert into tb_user ( userid, user_name, sex, birthday ) values( seq_user.nextval, ?, ?, ? ) 
DEBUG - ==> Parameters: yui(String), 男(String), 2018-08-14 10:31:06.973(Timestamp)
DEBUG - <==    Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Returned connection 1341177488 to pool.

1.六、choose、when、otherwise

有时咱们不想应用全部的条件, 相反咱们想选择不少状况下的一种。 Java 中的 switch 和 语句类似,MyBatis 提供 choose 元素。

需求:查询全部用户

1.若是用户填写了性别和姓名,则按姓名查找

2.若是用户填写了姓名,按姓名查找

3.若是用户未填写姓名,可是填写了性别,则按性别查找

接口声明

public List<User> queryDyc(QueryUser vo) throws Exception;

mapper.xml文件

<select id="queryDyc" resultMap="UserMap">
        select * from tb_user
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    user_name=#{name}
                </when>
                <when test="sex!=null and sex!=''">
                    sex=#{sex}
                </when>
                <otherwise>
                    age>5
                </otherwise>
            </choose>
        </where>
  </select>

测试方法

@Test
    public void testIfelse()throws Exception{

        QueryUser queryUser= new QueryUser();
        queryUser.setName(null);
        queryUser.setSex(null);
        List<User> list = userMapper.queryDyc(queryUser);
        for (User u : list) {
            System.out.println(u);
        }
    }

日志

DEBUG - Opening JDBC Connection
DEBUG - Created connection 480779844.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - ==>  Preparing: select * from tb_user WHERE age>5 
DEBUG - ==> Parameters: 
DEBUG - <==      Total: 7
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018]
User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018]
User [userid=2, userName=张三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=4, userName=王五, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=5, userName=赵六, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - Returned connection 480779844 to pool.

1.七、sql片断

sql片断是为了将一些经常使用的sql语句保存起来,在须要的时候进行引用便可

若是sql片断和statement在同一个命名空间下,则直接引用

<sql id="basesql">
        select * from tb_user
  </sql>
  <select id="queryDyc" resultMap="UserMap">
        <include refid="basesql"/>
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    user_name=#{name}
                </when>
                <when test="sex!=null and sex!=''">
                    sex=#{sex}
                </when>
                <otherwise>
                    age>5
                </otherwise>
            </choose>
        </where>
  </select>

若是sql片断和statement不在同一个命名空间下,则直接则使用命名空间.sqlid的形式进行引用

<select id="queryDyc" resultMap="UserMap">
        <include refid="mycomm.baseselect"/>
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    user_name=#{name}
                </when>
                <when test="sex!=null and sex!=''">
                    sex=#{sex}
                </when>
                <otherwise>
                    age>5
                </otherwise>
            </choose>
        </where>
  </select>
相关文章
相关标签/搜索