mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.example.mapper.UsersMapper"><selectid="selectOnCondition"parameterType="user"resultType="user">
select
<includerefid="allColumns"/>
from
users
<where><iftest="userName != null and userName != ''">
and username like concat('%', #{userName},'%')
if><iftest="birthday != null">
and birthday=#{birthday}
if><iftest="sex != null and sex != ''">
and sex=#{sex}
if><iftest="address != null and address != ''">
and address=#{address}
if>where>select>mapper>
< where >标签相当于在原sql语句后补加了一个:where关键字,从底层输出的结果可以证实
==> Preparing: select id, username, birthday, sex, address from users WHERE username like concat('%', ?,'%')
注意:至少有一个< if >标签通过,where关键字才会被追加,不然不会追加
@TestpublicvoidtestSelectOnCondition(){
Useru=newUser();
List users = usersMapper.selectOnCondition(u);
users.forEach(System.out::println);
}
Checking to see ifclasscom.example.mapper.TestUsersMapper matches criteria [is assignable to Object]
Checking to see ifclasscom.example.mapper.UsersMapper matches criteria [is assignable to Object]
Opening JDBC Connection
Created connection 1340848245.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4febb875]
==> Preparing: select id, username, birthday, sex, address from users //这时sql语句后面没有追加where
==> Parameters:
<== Columns: id, username, birthday, sex, address
<== Row: 1, 荷包蛋, 2002-08-23, 女, 黑河
<== Row: 2, 小王, 2001-07-12, 1, 芜湖市
<== Row: 3, 小张, 1999-02-22, 1, 长沙
<== Row: 5, 段, 2001-03-10, 1, 太原
<== Row: 6, 范成群, 2002-01-19, 1, 鲅鱼圈
<== Row: 7, 学委, 2001-05-13, 2, 平顶山市
<== Row: 28, 逻辑, 2010-05-18, 男, 上海
<== Row: 29, 小昕, 2001-03-14, 女, 忻州
<== Row: 30, 小青, 1996-11-22, 女, 沈阳市
<== Total: 9
Users{id=1, userName='荷包蛋', birthday=Fri Aug 2300:00:00 CST 2002, sex='女', address='黑河'}
Users{id=2, userName='小王', birthday=Thu Jul 1200:00:00 CST 2001, sex='1', address='芜湖市'}
Users{id=3, userName='小张', birthday=Mon Feb 2200:00:00 CST 1999, sex='1', address='长沙'}
Users{id=5, userName='段', birthday=Sat Mar 1000:00:00 CST 2001, sex='1', address='太原'}
Users{id=6, userName='范成群', birthday=Sat Jan 1900:00:00 CST 2002, sex='1', address='鲅鱼圈'}
Users{id=7, userName='学委', birthday=Sun May 1300:00:00 CST 2001, sex='2', address='平顶山市'}
Users{id=28, userName='逻辑', birthday=Tue May 1800:00:00 CST 2010, sex='男', address='上海'}
Users{id=29, userName='小昕', birthday=Wed Mar 1400:00:00 CST 2001, sex='女', address='忻州'}
Users{id=30, userName='小青', birthday=Fri Nov 2200:00:00 CST 1996, sex='女', address='沈阳市'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4febb875]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4febb875]
Returned connection 1340848245 to pool.
Process finished with exit code 0
Checking to see ifclasscom.example.mapper.TestUsersMapper matches criteria [is assignable to Object]
Checking to see ifclasscom.example.mapper.UsersMapper matches criteria [is assignable to Object]
Opening JDBC Connection
Created connection 544966217.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@207b8649]
==> Preparing: update users SET address=? where id=?
==> Parameters: 哈尔滨(String), 1(Integer)
<== Updates: 1
更新成功!
Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@207b8649]
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@207b8649]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@207b8649]
Returned connection 544966217 to pool.
Process finished with exit code 0