表blog
實體類
(使用了Lombok)
package com.zy.pojo; import lombok.Data; import java.util.Date; @Data public class Blog { private String id; private String title; private String author; private Date createTime; private int views; }
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/testmybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root password=123456
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
<properties resource="jdbc.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--是否開啟自動駝峰命名規則(camel case)映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> <typeAliases>
<!-- 方法1、定義一個alias別名,缺點在於需要一個實體類分別指定 <typeAlias type="com.zy.pojo.User" alias="user" />-->
<!-- 方法2、也可以使用package來給某個包下面的所有實體類自動創建別名, 自動創建的別名規則是類的類名並將首字母改為小寫 -->
<package name="com.zy.pojo"/>
</typeAliases> <!--配置環境-->
<environments default="mysql">
<!--有多個環境時,通過修改default="id"來實現選擇--> <environment id="mysql">
<!--JDBC事務管理-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="{username}"/>
<property name="password" value="{password}"/>
</dataSource>
</environment>
</environments> <!--每一個Mapper.XML都需要Mybatis核心配置文件中注冊-->
<!--如果文件放在resources下,則這么寫-->
<mappers>
<mapper resource="mapper/BlogMapper.xml"></mapper>
</mappers> </configuration>
二、< where >
當查詢時,select * from xxx where id=#{id}
如果出現多條件時,比如 select * from xxx where id=#{id} and name=#{name}
但是當 id 或者 name 未知的情況下,就沒有辦法實現查詢
所以使用動態SQL中的 < where > 進行查詢
< if >
BlogMapper
package com.zy.mapper; import com.zy.pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { //查詢博客
List<Blog> queryBlogIF(Map map); }
BlogMapper.xml:
<select id="queryBlogIF" parameterType="map" resultType="blog"> select * from blog <where>
<if test="title != null"> title = #{title} </if>
<if test="author != null"> and author = #{author} </if>
<if test="views != null"> and views = #{views} </if>
</where>
</select>
測試類
@Test public void queryBlogIF() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("title","45325"); map.put("author","efg"); List<Blog> blogs = blogMapper.queryBlogIF(map); for (Blog blog : blogs) { System.out.println(blog); } }
結果
如果不添加任何元素進入map
@Test public void queryBlogIF() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); List<Blog> blogs = blogMapper.queryBlogIF(map); for (Blog blog : blogs) { System.out.println(blog); } }
結果
< choose >
BlogMapper
package com.zy.mapper; import com.zy.pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { List<Blog> queryBlogChoose(Map map); }
BlogMapper.xml
<select id="queryBlogChoose" parameterType="map" resultType="blog"> select * from blog <where>
<choose>
<when test="title != null"> title = #{title} </when>
<when test="author != null"> and author = #{author} </when>
<otherwise> and views = #{views} </otherwise>
</choose>
</where>
</select>
測試類
@Test public void queryBlogChoose() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("title","45325"); map.put("author","efg"); map.put("views","322"); List<Blog> blogs = blogMapper.queryBlogChoose(map); for (Blog blog : blogs) { System.out.println(blog); } }
結果
存在多個成立的條件
比如:這個sql中,不使用choose的話會查出3條記錄
但只取choose中的第一個
使用 < trim >代替 < where > + SQL片段使用
BlogMapper.xml
<sql id="sql-title-author-views">
<if test="title != null"> title = #{title} </if>
<if test="author != null"> AND author = #{author} </if>
<if test="views != null"> AND views = #{views} </if>
</sql>
<select id="queryBlogIF" parameterType="map" resultType="blog"> select * from blog <trim prefix="where" prefixOverrides="AND">
<include refid="sql-title-author-views"></include>
</trim>
</select>
< set >
BlogMapper
package com.zy.mapper; import com.zy.pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { //更新博客
int updateBlog(Map map); }
BlogMapper.xml
<update id="updateBlog" parameterType="map"> update blog <set>
<if test="title != null"> title = #{title}, </if>
<if test="author != null"> author = #{author}, </if>
<if test="views != null"> views = #{views}, </if>
</set> where id = #{id} </update>
測試類
@Test public void queryBlogSet() { SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); map.put("title","6546"); map.put("author","fasfd"); map.put("views","6243"); map.put("id","d4303d0f-43f2-422e-8dbf-9a61ad872ff9"); blogMapper.updateBlog(map); List<Blog> blogs = blogMapper.queryBlogIF(map); for (Blog blog : blogs) { System.out.println(blog); } }
原本的數據庫
更新后
使用 < trim >代替 < set >
BlogMapper.xml
<update id="updateBlogSet" parameterType="map"> update blog <trim prefix="set" suffixOverrides=",">
<if test="title != null"> title = #{title}, </if>
<if test="author != null"> author = #{author}, </if>
<if test="views != null"> views = #{views}, </if>
</trim> where id = #{id} </update>
SQL片段
有時候,可以將一些功能相同的部分抽出,方便使用
抽取例子:
抽取前
<select id="queryBlogIF" parameterType="map" resultType="blog"> select * from blog <where>
<if test="title != null"> title = #{title} </if>
<if test="author != null"> and author = #{author} </if>
<if test="views != null"> and views = #{views} </if>
</where>
</select>
抽取后
<sql id="if-title-author-views">
<if test="title != null"> title = #{title} </if>
<if test="author != null"> and author = #{author} </if>
<if test="views != null"> and views = #{views} </if>
</sql> <!--where標簽 if-->
<select id="queryBlogIF" parameterType="map" resultType="blog"> select * from blog <where>
<include refid="if-title-author-views"></include>
</where>
</select>
測試
依舊能正常查出數據
注意點
1.最好基於單表定義SQL片段,盡量不要太復雜
2.不要存在 < where >
< ForEach >
對集合進行遍歷使用 ForEach
對 blog表 的 id 修改一下
BlogMapper
package com.zy.mapper; import com.zy.pojo.Blog; import java.util.List; import java.util.Map; public interface BlogMapper { //查詢1-2-3號記錄的博客
List<Blog> queryBlogForeach(Map map); }
BlogMapper.xml
<select id="queryBlogForeach" parameterType="map" resultType="blog"> select * from blog <trim prefix="where" prefixOverrides="AND">
<foreach collection="ids" item="id" open="AND (" close=")" separator="or"> id = #{id} </foreach>
</trim>
</select>
測試類
@Test public void queryBlogForEach(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); ArrayList<Integer> ids = new ArrayList<Integer>(); map.put("ids",ids); List<Blog> blogs = blogMapper.queryBlogForeach(map); for (Blog blog : blogs) { System.out.println(blog); } sqlSession.close(); }
結果
通過給ArrayList添加元素來實現想要的元素查詢
比如想查 id 為1,2,3的
@Test public void queryBlogForEach(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); HashMap map = new HashMap(); ArrayList<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); map.put("ids",ids); List<Blog> blogs = blogMapper.queryBlogForeach(map); for (Blog blog : blogs) { System.out.println(blog); } sqlSession.close(); }
結果