5.22,mybatis动态sql

    xiaoxiao2022-07-07  186

    SQL 语句中。 动态 SQL 元素和使用 JSTL 或其他相似的基于 XML 的文本处理器相似。在 MyBatis 之 前的版本中,有很多的元素需要来了解。MyBatis 3 大大提升了它们,现在用不到原先一半 的元素就能工作了。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。 if choose (when, otherwise) trim (where, set) foreach if 用在处理where后面跟一个条件 @Param(“name”) String name,@Param(“gradeid”) int gradeid 当方法有多个的参数 需要使用@Param指明将形参给xml文件中的那个参数

    public interface EmployeeDao {

    List<Employee> queryEmployee(@Param("name") String name,@Param("gradeid") int gradeid);

    } Xml

    <!-- type填写实体类的别名 --> <resultMap type="Employee" id="ee"> <id property="id" column="id" /> <result property="name1" column="name" /> <result property="sex" column="sex" /> <result property="hiredate" column="hiredate" /> <!-- 当属性为对象的时候用 association --> <association property="dept" javaType="Dept"> <id property="deptno" column="deptno" /> <result property="deptname" column="deptname" /> </association> </resultMap> <select id="queryEmployee" resultMap="ee" > SELECT * from employee where 1=1 <if test="name !=null"> and `name` like CONCAT('%',#{name},'%') </if> <if test="gradeid !=0"> and deptno=#{gradeid} </if> </select>

    choose, when, otherwise 有时我们不想应用所有的条件, 相反我们想选择很多情况下的一种。 Java 中的 switch 和 语句相似,MyBatis 提供 choose 元素。 使用Java 中的 switch来代替多个if SELECT * FROM BLOG WHERE state = ‘ACTIVE’ AND title like #{title} AND author_name like #{author.name} AND featured = 1

    Where 如果查询语句后面有多个条件 where 元素知道如果由被包含的标记返回任意内容,就仅仅插入“WHERE” 。而且,如 果以“AND”或“OR”开头的内容,那么就会跳过 WHERE不插入 SELECT * from employee and name like CONCAT(’%’,#{name},’%’) and deptno=#{gradeid}

    set

    场景: 更新 页面将需要更新的值传到后台 如果哪个属性不为空就进行更新操作

    EmployeeDao.xml UPDATE employee NAME=#{name1}, sex=#{sex}, hiredate=#{hiredate}, deptno=#{dept.deptno} where id=#{id}

    测试类 // 1.获取InputStream InputStream iStream = Test.class.getClassLoader().getResourceAsStream(“config.xml”); // 2.获取sqlsessionfactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(iStream); // 3.获取sqlsession SqlSession session = factory.openSession(); // 4.通过代理获取接口的对象 EmployeeDao eDao = session.getMapper(EmployeeDao.class); //更新 Employee employee=new Employee(); employee.setId(47); Dept dept=new Dept(); /* dept.setDeptno(1);*/ employee.setDept(dept); employee.setName1(“彭大大”); eDao.updateEmployee(employee); session.commit(); Foreach:批量删除 参数为list集合 map集合 DELETE from employee where id in ( 39,40,12) id为动态的

    参数为list集合

    EmployeeDao.xml item="id"代表循环遍历的临时变量名 collection=“list” 参数为List集合 该属性值为list 参数为数组该属性值为array DELETE from employee where id in #{id}

    测试类 public static void main(String[] args) { // 1.获取InputStream InputStream iStream = Test.class.getClassLoader().getResourceAsStream(“config.xml”); // 2.获取sqlsessionfactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(iStream); // 3.获取sqlsession SqlSession session = factory.openSession(); // 4.通过代理获取接口的对象 EmployeeDao eDao = session.getMapper(EmployeeDao.class);

    List ids=new ArrayList(); //in(39,40,12) ids.add(17); ids.add(18); eDao.deleteEmployee(ids); session.commit(); }

    参数为map集合 批量删除 并限制部门编号 sql: DELETE from employee where id in(37,45,47) and deptno=2 分析:1个参数为集合,1个参数为单个值 Foreach遍历Map通过key取值

    DELETE from employee where id in #{id} and deptno=#{deptno}

    测试类

    Mybatis缓存机制 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。 2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。 3. 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。 4. 二级缓存不同于hibernate ,它是一个映射文件级别的缓存 Mybatis一级缓存 一级缓存: 也就Session级的缓存(默认开启) 必须是同一个Session,如果session对象已经close()过了就不可能用了 查询条件是一样的 没有执行过session.clearCache()清理缓存 没有执行过增删改的操作(这些操作都会清理缓存) Mybatis二级缓存 使用之前需要开启 添加一个在userMapper.xml中 说明:

    映射语句文件中的所有select语句将会被缓存。映射语句文件中的所有insert,update和delete语句会刷新缓存。缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。缓存会根据指定的时间间隔来刷新。缓存会存储1024个对象 二级缓存的设置 <cache eviction=“FIFO” //回收策略为先进先出 flushInterval=“60000” //自动刷新时间60s size=“512” //最多缓存512个引用对象 readOnly=“true”/> //只读 注意:使用二级缓存的时候 sesssion需要提交才能生效 查询对应的对象的类需要实现 Serializable的接口 如果该类有引用其他类的对象作为属性,那么这个类也需要实现Serializable接口

    演示代码

    最新回复(0)