1 什么是动态SQL
2 逻辑判断
2.1 if标签
- 作用:条件满足则往sql语句添加标签中的内容
- 实例
- xml文件
<!-- if标记,test属性是字符串类型的判断条件。判断条件中的变量是传入对象的属性、字典中的某个键值对 --><select id="listProduct-if" resultType="Product">select * from product_<if test="name!=null">where name like concat('%',#{name},'%')</if></select>
b. java文件
System.out.println("查询所有的");List<Product> ps = session.selectList("listProduct-if");for (Product p : ps) {System.out.println(p);}System.out.println("模糊查询");// 通过hashmap存名字// Map<String,Object> params = new HashMap<>();// params.put("name","子");// 通过对象存名字Product params=new Product();params.setName("子");// 因为要使用属性来进行条件判断,因此不能传入基本类型数据List<Product> ps2 = session.selectList("listProduct-if",params);for (Product p : ps2) {System.out.println(p);}
2.2 choose when otherwise标签
- 作用:choose父标签下,when内容满足条件则往sql语句里添加内容,如果没有任何when满足则添加otherwise标签。
- 实例
- xml文件
<<select id="listProduct-choose" resultType="Product">SELECT * FROM product_<where><choose><when test="name != null">and name like concat('%',#{name},'%')</when><when test="price !=null and price != 0">and price > #{price}</when><otherwise>and id >1</otherwise></choose></where></select>
- java文件
System.out.println("choose查询");List<Product> ps = session.selectList("listProduct-choose");for (Product p : ps) {System.out.println(p);}
3 动态查询条件、动态属性设置
3.1 where标签
- 作用:自动在标签内语句前添加where,并且去除前面多余的and|or。
- 实例
- xml文件
<select id="listProduct-where" resultType="Product">select * from product_<where><if test="name!=null">and name like concat('%',#{name},'%')</if><if test="price!=null and price>0">and price > #{price}</if></where></select>
- java文件
System.out.println("查询所有的");List<Product> ps = session.selectList("listProduct-where");for (Product p : ps) {System.out.println(p);}System.out.println("模糊查询-提供name 子");Product params1 = new Product();params1.setName("子");List<Product> ps2 = session.selectList("listProduct-where", params1);for (Product p : ps2) {System.out.println(p);}System.out.println("模糊查询-提供price 20");Product params2 = new Product();params2.setPrice((float)20);List<Product> ps3 = session.selectList("listProduct-where", params2);for (Product p : ps3) {System.out.println(p);}System.out.println("模糊查询-提供name 子和price 20");Product params3 = new Product();params3.setName("子");params3.setPrice((float)20);List<Product> ps4 = session.selectList("listProduct-where", params3);for (Product p : ps4) {System.out.println(p);
3.2 set标签
- 作用:自动在标签内语句前添加set,并且去除后面余的逗号,。
- 实例
- xml文件
<update id="updateProduct-set" parameterType="Product">update product_<set><if test="name != null">name=#{name},</if><if test="price != null">price=#{price}</if></set>where id=#{id}</update>
- java文件
Product p = new Product();p.setId(6);p.setName("巴拉拉能量");p.setPrice(50.5f);session.update("updateProduct-set",p);System.out.println("查询所有的");List<Product> ps = session.selectList("listProduct-where");for (Product p1 : ps) {System.out.println(p1);}
3.3 trim标签,定制化内容
作用:定制化标签作用。
实例
- xml文件
<!-- where标记prefix属性为WHERE,prefixOverrides属性为AND|OR --><select id="listProduct-trim-where" resultType="Product">select * from product_<trim prefix="WHERE" prefixOverrides="AND|OR"><if test="name!=null">and name like concat('%',#{name},'%')</if><if test="price!=null and price>0">and price > #{price}</if></trim></select><!-- set标记prefix属性为SET,suffixOverrides属性为, --><update id="updateProduct-trim-set" parameterType="Product">update product_<trim prefix="SET" suffixOverrides=","><if test="name != null">name=#{name},</if><if test="price != null">price=#{price}</if></trim>where id=#{id}</update>
- java文件
Product p = new Product();p.setId(6);p.setName("仙女棒");p.setPrice(90.8f);session.update("updateProduct-trim-set",p);System.out.println("查询price 20的");Product params = new Product();params.setPrice((float)20);List<Product> ps = session.selectList("listProduct-trim-where",params);for (Product p1 : ps) {System.out.println(p1);}
4 其他标签
4.1 foreach标签
- 作用:通常用where的in关键字作用,指定in的列表内容。
<!— item:集合中元素迭代时的别名,该参数为必选。 index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选 open:foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。该参数可选 separator:元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
close: foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。该参数可选。 collection:
要做foreach的对象,作为入参时,List对象默认用”list”代替作为键,数组对象有”array”代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param(“keyName”)来设置键,设置keyName后,list,array将会失效。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection
= “ids”.如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection
= “ids.id” —>
- 实例
- xml文件
<select id="listProduct-foreach" resultType="Product">SELECT * FROM product_WHERE ID in<foreach item="item" index="index" collection="list" open="("separator="," close=")">#{item}</foreach></select>
- java文件
System.out.println("查询1,3,4");List<Integer> ids = new ArrayList();ids.add(1);ids.add(3);ids.add(4);List<Product> ps = session.selectList("listProduct-foreach",ids);for (Product p : ps) {System.out.println(p);}
4.2 bind标签
- 作用:为该标签内一个新的变量绑定一个字符串。
- 实例
- xml文件
<select id="listProduct-bind" resultType="Product"><bind name="likename" value="'%' + name + '%'" />select * from product_ where name like #{likename}</select>
- java文件
System.out.println("bind查询");Map<String, String> params = new HashMap();params.put("name", "子");List<Product> ps = session.selectList("listProduct-bind", params);for (Product p : ps) {System.out.println(p);}
