您的位置:首页 > 博客中心 > 数据库 >

mybatis 动态sql

时间:2022-03-15 15:04

MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。

动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

HouseDAO.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.etc.dao.HouseDAO">
 4 
 5    <!--查询单个条件 相当于switch=choose  when=case otherwise=default-->
 6     <select id="searchSim" resultType="house">
 7         select * from t_house
 8         <where>
 9             <choose>
10                 <when test="title!=null">
11                     title like ‘%${title}%‘
12                 </when>
13                 <when test="price!=null">
14                     price=#{price}
15                 </when>
16                 <otherwise>
17                     1=1
18                 </otherwise>
19             </choose>
20         </where>
21     </select>
22     <!--查询多个条件 if 如果存在就拼接
23     select * from t_house where title like ‘%?%‘ and price=?
24     -->
25     <select id="searchOdd" resultType="house">
26         select * from t_house
27         <where>
28             <if test="title!=null">
29                 title like ‘%${title}%‘
30             </if>
31             <if test="price!=null">
32                 and price=#{price}
33             </if>
34         </where>
35     </select>
36     <!--查询in collection集合名 item:参数名 open close=() separator用逗号拼接
37                 select * from t_house where id in (1,2,3)
38     -->
39     <select id="searchByIds" resultType="house">
40         select * from t_house where id in
41         <foreach collection="ids" item="id" open="(" close=")" separator=",">
42             #{id}
43         </foreach>
44     </select>
45     <!--更新(只更新存在的值)-->
46     <update id="update">
47         update t_house
48         <set>
49             <if test="title!=null">
50                 title=#{title},
51             </if>
52             <if test="price!=null">
53                 price=#{price}
54             </if>
55         </set>
56         where id =#{id}
57     </update>
58 
59 </mapper>

com.etc.dao.HouseDAO:
 1 package com.etc.dao;
 2 
 3 import com.etc.entity.House;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 
 8 public interface HouseDAO {
 9 
10     //查询单个条件 利用switch
11     List<House> searchSim(House house);
12     //查询多个条件 利用if
13     List<House> searchOdd(House house);
14     //查询in
15     List<House> searchByIds(@Param("ids") List<Integer> ids);
16     //更新(只更新存在的值)
17     void update(House house);
18 }

 

mybatis-config.xml:

技术图片技术图片
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC
 3         "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 别名 -->
 8     <typeAliases>
 9         <package name="com.etc.entity"></package>
10     </typeAliases>
11 
12     <!-- 配置环境变量 -->
13     <!-- 开发 测试  预生产 生产 -->
14     <environments default="development">
15         <environment id="development">
16             <transactionManager type="JDBC"/>
17             <dataSource type="POOLED">
18                 <property name="driver" value="com.mysql.jdbc.Driver"/>
19                 <property name="url"
20                           value="jdbc:mysql://127.0.0.1:3310/mybatis"/>
21                 <property name="username" value="root"/>
22                 <property name="password" value="123456"/>
23             </dataSource>
24         </environment>
25     </environments>
26 
27 
28     <!-- 配置mappers -->
29     <mappers>
30         <mapper resource="HouseDAO.xml"></mapper>
31     </mappers>
32 
33 </configuration>
View Code

 

com.etc.dao.HouseTest:

技术图片技术图片
 1 package com.etc.dao;
 2 
 3 import com.etc.entity.House;
 4 import org.apache.ibatis.io.Resources;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 8 import org.junit.Test;
 9 
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.List;
13 
14 public class HouseTest {
15 
16     @Test
17     public void test() throws IOException {
18         //加载配置文件 会话工厂
19         InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
20         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
21 
22         //会话   ==相当于数据库连接
23         SqlSession session=sqlSessionFactory.openSession();
24 
25         HouseDAO houseDAO=session.getMapper(HouseDAO.class);
26 
27         House house=new House();
28 //        house.setTitle("he");
29 //        house.setPrice(1300.0);
30 //        house.setId(1);
31 
32 //        List<House> search=houseDAO.searchSim(house);
33         List<House> search=houseDAO.searchOdd(house);
34 //        List<House> search=houseDAO.searchByIds(Arrays.asList(1,2,3));
35 //        houseDAO.update(house);
36         for (House h:search)
37             System.out.println(h);
38 
39 
40 
41         session.commit();
42         session.close();
43     }
44 }
View Code

 

热门排行

今日推荐

热门手游