mybatis动态Sql模糊查询

一、Mybatis动态语句分为4种元素

mybatis动态Sql模糊查询_第1张图片

①if元素

mybatis动态Sql模糊查询_第2张图片

②where 元素

mybatis动态Sql模糊查询_第3张图片

 ③choose(   when     otherwise)

 mybatis动态Sql模糊查询_第4张图片

④set用在update上

mybatis动态Sql模糊查询_第5张图片

⑤foreache遍历

mybatis动态Sql模糊查询_第6张图片

 二、方法传递多个参数时如何为参数起名

mybatis动态Sql模糊查询_第7张图片

三、mybatis处理特殊字符

(1)使用转义字符替换

mybatis动态Sql模糊查询_第8张图片

 

     

(2)CDATA内容会原翻不动的解析<![CDATA[]]>

四、pagehelper分页插件---完成分页查询

使用步骤:

①引入相关依赖

        
            com.github.pagehelper
            pagehelper
            5.1.11
        

②在mybatis配置文件中引入插件

mybatis动态Sql模糊查询_第9张图片③使用分页的功能

public static void main(String[] args) throws Exception{
        Reader resourceAsReader = Resources.getResourceAsReader("mybatis-conf.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession sqlSession = sessionFactory.openSession();
        FoodMapper foodMapper = sqlSession.getMapper(FoodMapper.class);
        PageHelper.startPage(2, 3);

        List all = foodMapper.findAll();
        PageInfo pageInfo=new PageInfo(all);
        System.out.println("当前总条数:"+pageInfo.getTotal());
      System.out.println("当前页码:"+pageInfo.getPageNum());
      System.out.println("当前记录:"+pageInfo.getList());
      System.out.println("总页数:"+pageInfo.getPages());

    }

五、代码生成器----单表查询crud---generator

①引入依赖


    org.mybatis.generator
    mybatis-generator-core
    1.4.0

②generator的配置文件---必须放在工程下

mybatis动态Sql模糊查询_第10张图片





  
  


  
    
    
      
    
    
    
    

    
      
    

    
    
      
      
    

    
    
      
    

    
    
      
    
    
    

 ③测试

public static void main(String[] args) throws Exception {
    List warnings = new ArrayList();
    boolean overwrite = true;
    File configFile = new File("generator-mybatis.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
}

六、二级缓存

缓存是基于内存存储,为了减少对数据库的访问频率

mybatis默认支持一级缓存,基于session执行

开启二级缓存---默认不开启

mybatis动态Sql模糊查询_第11张图片

 

你可能感兴趣的:(sql,数据库,database)