MyBatis中动态SQL
生活随笔
收集整理的這篇文章主要介紹了
MyBatis中动态SQL
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
01為什么使用sql語句的動態拼接
目前學習的內容sql語句都是直接寫死的,但是在實際的開發過程中很多的sql語句都是根據不同的業務情況進行不同的改變的
02 解決的方案
sql語句的動態拼接
03學習的標簽
if where when set trim foreach bind include sql
接口代碼1
XML文件1
<mapper namespace="com.bjsxt.mapper.FlowerMapper"><!--if標簽if(test){..}if(test){..}--><select id="selectMore" resultType="flower">SELECT * from flower where 1=1<!--OGNL表達式--><if test="param1!=null and param1!=''">and name=#{param1}</if><if test="param2!=null and param2!=''">and production=#{param2}</if></select><!--Where標簽的作用:會自動的增加where關鍵字,并且會把多余的第一個and去掉--><select id="selectMore2" resultType="flower">SELECT * from flower<!--OGNL表達式--><where><if test="param1!=null and param1!=''">name=#{param1}</if><if test="param2!=null and param2!=''">and production=#{param2}</if></where></select><select id="selectMore3" resultType="flower">SELECT * from flower<where><if test="param1!=null and param1!=''">name=#{param1}</if></where></select><!--if(){..}else if(){..}else if(){..}else {}--><select id="selectMore4" resultType="flower">SELECT * from flower<where><choose><when test="param1!=null and param1!=''">name=#{param1}</when><when test="param2!=null and param2!=''">and production=#{param2}</when><otherwise>1=1</otherwise></choose></where></select><!--Set 會自動增加set關鍵字,并且去除最后一個逗號--><update id="update">UPDATE flower<set><if test="name!=null and name!=''">name=#{name},</if><if test="production!=null and production!=''">production=#{production},</if></set>where id=#{id}</update><!--trim:prefix:添加前綴prefixOverrides:去除前綴suffix:添加后綴suffixOverrides:去除后綴--><update id="update2">UPDATE flower<trim prefix="set" suffixOverrides=","><if test="name!=null and name!=''">name=#{name},</if><if test="production!=null and production!=''">production=#{production}</if></trim>where id=#{id}</update></mapper>測試代碼1
// 動態SQL語句 public static void main(String[] args) throws IOException {//[1]解析myBatis.xml文件InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");//[2]獲得sqlsession工廠SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);//[3]獲得session對象SqlSession sqlSession = factory.openSession(true);//[4]執行方法FlowerMapper mapper = sqlSession.getMapper(FlowerMapper.class);//List<Flower> list = mapper.selectMore4("玫瑰花","");Flower f=new Flower();f.setId(9);mapper.update(f);//[5]關閉資源sqlSession.close(); }接口2
public interface FlowerMapper2 {List<Flower> selectMore(List<Integer> li);List<Flower> selectMore2(String name, String production);List<Flower> selectMore3();}XML2
<mapper namespace="com.bjsxt.mapper.FlowerMapper2"><!--foreach select * from flower where id in (1,2,3)--><select id="selectMore" resultType="flower">select * from flower where id in<foreach collection="list" open="(" separator="," close=")" item="it">#{it}</foreach></select><!--bind 模糊查詢--><select id="selectMore2" resultType="flower">select * from flower<where><if test="param1 != null and param1 != ''"><bind name="pa" value=" '%' + param1 + '%' "></bind>name like #{pa} <!--占位(推薦)--></if><if test="param2 != null and param2 != ''">and production like '%${param2}%' <!--拼接--></if></where></select><select id="selectMore3" resultType="flower">select <include refid="sq1"></include> from flower</select><!--定義(公共的)sql代碼片段--><sql id="sq1">id,name,price</sql><sql id="sq2">id,name,price,production</sql></mapper>測試3
FlowerMapper2 mapper = sqlSession.getMapper(FlowerMapper2.class);List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);//List<Flower> li = mapper.selectMore(list);List<Flower> li = mapper.selectMore2("花", "");System.out.println(li);總結
以上是生活随笔為你收集整理的MyBatis中动态SQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis中Mapper代理方式
- 下一篇: 如何快速制作虚拟数字人面部动画如何快速制