mybatis版本
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency>
动态SQL语句
IF标签
<select id="findSelective" parameterType="com.tsing.model.Blog" resultMap="BaseResultMap"> SELECT * FROM blog WHERE `status` = 1 <if test="title != null and title != '' "> and title like #{title} </if> <if test="summary != null "> and summary like #{summary} </if> </select>
@Test public void testFindSelective() { SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory(); SqlSession session = factory.openSession(true); BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = new Blog(); blog.setTitle(""); blog.setSummary(""); mapper.findSelective(blog) ; }
日志输出
==> Preparing: SELECT * FROM blog WHERE `status` = 1 and summary like ?
==> Parameters: (String)