Java从入门到入土之mybatis动态sql篇

1. where

<select id="getByName" resultMap="student" parameterType="String">
        select * from student
            <where>
                
                <if test="name != null">
                    name = #{name}
                if>
            where>
select>

测试类

public class Test1 {

    public static void main(String[] args) throws IOException {
        /*获取配置文件mybatis-config.xml*/
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        /*建造会话工厂SqlSessionFactory*/
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /*生产SqlSession*/
        SqlSession session = sessionFactory.openSession();
        /*通过反射获取TeacherMapper*/
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        /*调用TeacherMapper的方法*/
        List<Student> student = mapper.getByName("张三");
        System.out.println(student);
    }
}

运行结果

2019-07-24 14:31:58  [ main:0 ] - [ DEBUG ]  ==>  Preparing: select * from student WHERE name = ? 
2019-07-24 14:31:58  [ main:71 ] - [ DEBUG ]  ==> Parameters: 张三(String)
2019-07-24 14:31:58  [ main:93 ] - [ DEBUG ]  <==      Total: 1
Student{sid=1, name='张三', tid=1}

Process finished with exit code 0

执行的sql语句为 select * from student WHERE name = ? 参数为 张三(String) 结果为1条记录。

2019-07-24 14:34:10  [ main:0 ] - [ DEBUG ]  ==>  Preparing: select * from student 
2019-07-24 14:34:10  [ main:64 ] - [ DEBUG ]  ==> Parameters: 
2019-07-24 14:34:10  [ main:101 ] - [ DEBUG ]  <==      Total: 2
[Student{sid=1, name='张三', tid=1}, Student{sid=2, name='赵武', tid=1}]

Process finished with exit code 0

执行的sql语句为 select * from student 没有参数 结果为2条记录。

2. trim

<select id="getBySid" resultMap="student">
        SELECT * FROM student
        
        <trim prefix="WHERE" prefixOverrides="AND |OR ">
            
            <if test="name != null">  name=#{name}if>
            <if test="sid != null">AND sid=#{sid}if>
        trim>
select>

name不为null时运行结果

2019-07-24 14:50:44  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE name=? AND sid=? 
2019-07-24 14:50:44  [ main:71 ] - [ DEBUG ]  ==> Parameters: 张三(String), 1(Integer)
2019-07-24 14:50:44  [ main:98 ] - [ DEBUG ]  <==      Total: 1
Student{sid=1, name='张三', tid=1}

Process finished with exit code 0

执行的sql语句为 SELECT * FROM student WHERE name=? AND sid=? 参数为张三(String), 1(Integer) 结果为1条记录。

name为null时运行结果

2019-07-24 14:52:33  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE sid=? 
2019-07-24 14:52:33  [ main:83 ] - [ DEBUG ]  ==> Parameters: 1(Integer)
2019-07-24 14:52:33  [ main:121 ] - [ DEBUG ]  <==      Total: 1
Student{sid=1, name='张三', tid=1}

执行的sql语句为 SELECT * FROM student WHERE sid=? 参数为 1(Integer) 结果为1条记录,去掉了后面的and name = ?

3. set

<update id="update" parameterType="com.pojo.Student">
        UPDATE student
        <set>
            
            <if test="name != null">name=#{name},if>
            <if test="sid != null">tid=#{tid}if>
        set>
        <where>
            <if test="sid != null">sid = #{sid}if>
        where>
update>

都不为null运行结果
执行方法后别忘了提交事务,session.commit()

2019-07-24 15:05:59  [ main:0 ] - [ DEBUG ]  ==>  Preparing: UPDATE student SET name=?, tid=? WHERE sid = ? 
2019-07-24 15:05:59  [ main:58 ] - [ DEBUG ]  ==> Parameters: 赵六(String), 5(Integer), 1(Integer)
2019-07-24 15:05:59  [ main:71 ] - [ DEBUG ]  <==    Updates: 1

Process finished with exit code 0

执行的sql语句为 UPDATE student SET name=?, tid=? WHERE sid = ? 参数为 赵六(String), 5(Integer), 1(Integer) 结果为1条记录
name为null运行结果

2019-07-24 15:09:17  [ main:0 ] - [ DEBUG ]  ==>  Preparing: UPDATE student SET tid=? WHERE sid = ? 
2019-07-24 15:09:17  [ main:49 ] - [ DEBUG ]  ==> Parameters: 5(Integer), 1(Integer)
2019-07-24 15:09:17  [ main:50 ] - [ DEBUG ]  <==    Updates: 1

Process finished with exit code 0

执行的sql语句为 UPDATE student SET tid=? WHERE sid = ? 参数为 5(Integer), 1(Integer) 结果为1条记录,去掉了name=?

4. choose

<select id="select" resultMap="student">
        SELECT * FROM student
        
        <choose>
            
            <when test="name != null">
                WHERE name = #{name}
            when>
            
            <when test="sid != 1">
                WHERE sid =#{sid}
            when>
            
            <otherwise>
                WHERE tid = 1
            otherwise>
        choose>
    select>

name不为null,运行结果

2019-07-24 15:41:15  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE name = ? 
2019-07-24 15:41:15  [ main:61 ] - [ DEBUG ]  ==> Parameters: 赵六(String)
2019-07-24 15:41:15  [ main:92 ] - [ DEBUG ]  <==      Total: 1
[Student{sid=1, name='赵六', tid=5}]

Process finished with exit code 0

执行的sql语句为 SELECT * FROM student WHERE name = ? 参数为 赵六(String) 结果为1条记录,
name为null且sid!=1,运行结果

2019-07-24 15:43:11  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE sid =? 
2019-07-24 15:43:11  [ main:68 ] - [ DEBUG ]  ==> Parameters: 2(Integer)
2019-07-24 15:43:11  [ main:98 ] - [ DEBUG ]  <==      Total: 1
[Student{sid=2, name='赵武', tid=1}]

Process finished with exit code 0

执行的sql语句为 SELECT * FROM student WHERE sid =? 参数为 2(Integer) 结果为1条记录,
name为nullqiesid==1,运行结果

2019-07-24 15:43:53  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE tid = 1 
2019-07-24 15:43:53  [ main:43 ] - [ DEBUG ]  ==> Parameters: 
2019-07-24 15:43:53  [ main:138 ] - [ DEBUG ]  <==      Total: 1
[Student{sid=2, name='赵武', tid=1}]

Process finished with exit code 0

执行的sql语句为 SELECT * FROM student WHERE tid = 1 无参数 结果为1条记录,运行的otherwise标签里的sql语句

5. foreach


    <select id="selectIn" resultMap="student">
        SELECT * FROM student WHERE sid IN
        
        <foreach collection="list" item="item_id" separator="," open="(" close=")">
          #{item_id}
        foreach>
    select>

测试类


public class Test1 {

    public static void main(String[] args) throws IOException {
        /*获取配置文件mybatis-config.xml*/
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        /*建造会话工厂SqlSessionFactory*/
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /*生产SqlSession*/
        SqlSession session = sessionFactory.openSession();
        /*通过反射获取TeacherMapper*/
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        /*调用TeacherMapper的方法*/
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        List<Student> student = mapper.selectIn(list);
        System.out.println(student);
    }
}

运行结果

2019-07-24 15:57:50  [ main:0 ] - [ DEBUG ]  ==>  Preparing: SELECT * FROM student WHERE sid IN ( ? , ? , ? , ? ) 
2019-07-24 15:57:50  [ main:57 ] - [ DEBUG ]  ==> Parameters: 1(Integer), 2(Integer), 3(Integer), 4(Integer)
2019-07-24 15:57:50  [ main:94 ] - [ DEBUG ]  <==      Total: 2
[Student{sid=1, name='赵六', tid=5}, Student{sid=2, name='赵武', tid=1}]

Process finished with exit code 0

foreach循环遍历了传入的list,将list的元素当做参数。
执行sql语句 Preparing: SELECT * FROM student WHERE sid IN ( ? , ? , ? , ? ) 参数为 (Integer), 2(Integer), 3(Integer), 4(Integer) 结果为2条记录

你可能感兴趣的:(Java从入门到入土之mybatis动态sql篇)