1. where
<select id="getByName" resultMap="student" parameterType="String">
select * from student
<where>
<!-- 当name=null时在select * from student后加上where name = #{name}
否则执行select * from student语句-->
<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
<!--prefix:前缀
prefixoverride:去掉第一个and或者是or-->
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<!--如果name为null就只按sid的条件查询,不去掉and的话会出现语法错误-->
<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>
<!--更新name,tid-->
<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
<!--相当于if elseif else的组合-->
<choose>
<!--如果name != null 则执行WHERE name = #{name},后面的都不执行-->
<when test="name != null">
WHERE name = #{name}
</when>
<!--如果name == null 且 sid !=1 则执行WHERE sid =#{sid}-->
<when test="sid != 1">
WHERE sid =#{sid}
</when>
<!--上面条件都不符合时执行otherwise中的语句-->
<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
<!--查询sid在集合内的记录-->
<select id="selectIn" resultMap="student">
SELECT * FROM student WHERE sid IN
<!--collection:指定要遍历的集合:
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值
#{变量名}就能取出变量的值也就是当前遍历出的元素-->
<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条记录