<select id="getUserByDomain" parameterType="user" resultType="com.mybatis.domain.User">
SELECT
`id`,
`username`,
`birthday`,
`sex`,
`address`
FROM `user`
<!-- WHERE 1=1
<if test="username!=null and username!=''">
and username LIKE '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if> -->
<!-- where自动补全where关键字同时处理多余的and,不能手动加where关键字 -->
<where>
<if test="username!=null and username!=''">
and username LIKE '%${username}%'
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</select>
3.单元测试
@Test
public void testgetUserByDomain() {
SqlSession sql=SqlSessionFactoryUtils.getSql().openSession();
//接口代理实现类
UserMapper userMapper=sql.getMapper(UserMapper.class);
User user = new User();
user.setUsername("百");
user.setSex("1");
List<User> list=userMapper.getUserByDomain(user);
for(User user1:list)
System.out.println(user1);
sql.close();
}