@Test public void testGetByNameOrAddress(){ List users = usersMapper.getByNameOrAddress("username", "小"); //List users = usersMapper.getByNameOrAddress("address", "市"); users.forEach(System.out::println); } }
测试输出
根据用户名模糊查询
Checking to see if class com.example.mapper.TestUsersMapper matches criteria [is assignable to Object] Checking to see if class com.example.mapper.UsersMapper matches criteria [is assignable to Object] Opening JDBC Connection Created connection 1293462056. Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28]
==> Preparing: select id, username, birthday, sex, address from users where username like concat('%', ?, '%') ==> Parameters: 小(String)
<== Columns: id, username, birthday, sex, address <== Row: 2, 小王, 2001-07-12, 1, 芜湖市 <== Row: 3, 小张, 1999-02-22, 1, 长沙 <== Row: 29, 小昕, 2001-03-14, 女, 忻州 <== Total: 3 Users{id=2, userName='小王', birthday=Thu Jul 12 00:00:00 CST 2001, sex='1', address='芜湖市'} Users{id=3, userName='小张', birthday=Mon Feb 22 00:00:00 CST 1999, sex='1', address='长沙'} Users{id=29, userName='小昕', birthday=Wed Mar 14 00:00:00 CST 2001, sex='女', address='忻州'} Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28] Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28] Returned connection 1293462056 to pool. Process finished with exit code 0
根据地址模糊查询
Checking to see if class com.example.mapper.TestUsersMapper matches criteria [is assignable to Object] Checking to see if class com.example.mapper.UsersMapper matches criteria [is assignable to Object] Opening JDBC Connection Created connection 1293462056. Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28]
==> Preparing: select id, username, birthday, sex, address from users where address like concat('%', ?, '%') ==> Parameters: 市(String)
<== Columns: id, username, birthday, sex, address <== Row: 2, 小王, 2001-07-12, 1, 芜湖市 <== Row: 7, 学委, 2001-05-13, 2, 平顶山市 <== Total: 2 Users{id=2, userName='小王', birthday=Thu Jul 12 00:00:00 CST 2001, sex='1', address='芜湖市'} Users{id=7, userName='学委', birthday=Sun May 13 00:00:00 CST 2001, sex='2', address='平顶山市'} Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28] Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4d18aa28] Returned connection 1293462056 to pool. Process finished with exit code 0
测试结果分析
sql标签在底层分别被解析为
==> Preparing: select id, username, birthday, sex, address from users where username like concat('%', ?, '%') ==> Parameters: 小(String)
==> Preparing: select id, username, birthday, sex, address from users where address like concat('%', ?, '%') ==> Parameters: 市(String)