mybatis学习记录~
Posted tsing0520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis学习记录~相关的知识,希望对你有一定的参考价值。
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)
CHOOSE标签
<select id="findSelectiveWithChoose" resultType="com.tsing.model.Blog"> SELECT * FROM blog WHERE `status` = 1 <choose> <when test="title != null"> AND title like #{title} </when> <when test="summary != null"> AND summary like #{summary} </when> <otherwise> AND content = ‘tsing‘ </otherwise> </choose> </select>
@Test public void testFindSelectiveWithChoose() { SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory(); SqlSession session = factory.openSession(true); BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = new Blog(); mapper.findSelectiveWithChoose(blog); }
日志输出
==> Preparing: SELECT * FROM blog WHERE `status` = 1 AND content = ‘tsing‘
==> Parameters:
WHERE标签
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if>
</select>
上述的sql语句可能导致以下的问题出现: SELECT * FROM BLOG WHERE AND title like ?
where元素只会在至少有一个子元素的条件返回SQL子句的情况下才会插入where子句。若子句的开头是and或者or,where元素也会将其移除。
<select id="findSelectiveWithWhere" resultType="com.tsing.model.Blog"> SELECT * FROM blog <where> <if test="title != null "> and title like #{title} </if> <if test="summary != null "> or summary like #{summary} </if> </where> </select>
@Test public void testFindSelectiveWithWhere() { SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory(); SqlSession session = factory.openSession(true); BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = new Blog(); blog.setSummary(""); mapper.findSelectiveWithWhere(blog); }
日志输出
==> Preparing: SELECT * FROM blog WHERE summary like ?
==> Parameters: (String)
TRIM标签
<select id="findSelectiveWithTrim" resultType="com.tsing.model.Blog"> SELECT * FROM blog <trim prefix="WHERE" prefixOverrides="AND |OR "> <if test="title != null "> and title like #{title} </if> <if test="summary != null "> or summary like #{summary} </if> </trim> </select>
@Test public void testFindSelectiveWithTrim() { SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory(); SqlSession session = factory.openSession(true); BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = new Blog(); blog.setTitle(""); //blog.setSummary(""); mapper.findSelectiveWithTrim(blog); }
日志输出
==> Preparing: SELECT * FROM blog WHERE title like ?
==> Parameters: (String)
以上是关于mybatis学习记录~的主要内容,如果未能解决你的问题,请参考以下文章
Spring+SpringMVC+MyBatis+Maven框架整合
ElasticSearch学习问题记录——Invalid shift value in prefixCoded bytes (is encoded value really an INT?)(代码片段