zoukankan      html  css  js  c++  java
  • MYSQL复杂查询(条件不定查询+按降序/升序分页显示)

          使用mybatis框架后,mybatis-generator可以为我们自动生成操作数据库(MYSQL)的*Mapper.xml文件+模型类+*Mapper接口,但是,mybatis-generator并不能为我们生成所有方法,例如根据输入的不定条件来查询数据,又比如按照某个参数升序或者降序并分页显示所查到的数据,下面介绍一下条件不定查询及按降序/升序分页显示的方法。

    1、按条件不定查询(项目基于SSMM(spring+springmvc+maven+mybatis)框架整合的代码进行举例)

    • 在BookMapper.xml中加入selectByCondition方法
     1     <select id="selectByCondition" resultMap="BaseResultMap"
     2         parameterType="com.wn.model.Book">
     3         select
     4         <include refid="Base_Column_List" />
     5         from t_book
     6         <where>
     7             <if test="bookName!=null">
     8                 book_name=#{bookName,jdbcType=VARCHAR}
     9             </if>
    10             <if test="author!=null">
    11                 AND author=#{author,jdbcType=VARCHAR}
    12             </if>
    13             <if test="publishDate!=null">
    14                 AND publish_date=#{publishDate,jdbcType=TIMESTAMP}
    15             </if>
    16         </where>
    17     </select>
    • 在BookMapper.java中加入:
    List<Book> selectByCondition(Book record);
    • 在BookDao加入
    1     public List<Book> selectByCondition(Book book) {
    2         return bookMapper.selectByCondition(book);
    3     }
    • 在BookService加入
    1     public List<Book> selectByCondition(String bookName, String author, Date publishDate) {
    2         Book book = new Book();
    3         book.setBookName(bookName);
    4         book.setAuthor(author);
    5         book.setPublishDate(publishDate);
    6         return bookDao.selectByCondition(book);
    7     }
    • 在BookController加入
    1     @RequestMapping(value = "/selectByCondition", method = RequestMethod.GET)
    2     public List<Book> selectByCondition(@RequestParam(value="bookName",required=false) String bookName,
    3                                         @RequestParam(value="author",required=false) String author, 
    4                                         @RequestParam(value="publishDate",required=false) @DateTimeFormat(pattern = "yyyy-mm-dd HH:mm:ss")Date publishDate) {
    5         return bookService.selectByCondition(bookName, author, publishDate);
    6     }

    2、按降序/升序分页显示(基于缓存(本地缓存)花店的代码实现)

    • 在FlowerMapper.xml中加入
    1    <select id="selectLimit" resultMap="BaseResultMap"
    2         parameterType="java.lang.Integer">
    3         select
    4         <include refid="Base_Column_List" />
    5         from t_flower
    6         order by price DESC limit #{start}, #{size}
    7     </select>
    • 在FlowerMapper.java中加入:
    1 List<Flower> selectLimit(@Param("start") int start, @Param("size") int size);

    上述start表示开始显示记录索引,size表示显示记录的条数,为了方便找到这两个参数,这里使用了@Param注解

    • 在FlowerDao中加入:
    1      /*
    2      * 传入开始显示记录的索引,即显示记录的条数,实现分页显示功能
    3      */
    4     public List<Flower> selectLimit(int start, int size) {
    5         return flowerMapper.selectLimit(start, size);
    6     }
    • 在FlowerService中加入:
    1     public List<Flower> selectLimit(Integer start, Integer size) {
    2         return flowerDao.selectLimit(start, size);
    3     }
    • 在FlowerController中加入
    1     @RequestMapping(value = "/getFlowerByLimit", method = RequestMethod.GET)
    2     public List<Flower> getFlowerByLimit(@RequestParam("start") Integer start, @RequestParam("size") Integer size) {
    3         return flowerService.selectLimit(start, size);
    4     }
  • 相关阅读:
    iview table的render()函数基本的用法
    【整理】iview Tree数据格式问题,无限递归树处理数据
    【整理】用JSON-server模拟REST API
    【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
    【整理】 vue-cli 打包后显示favicon.ico小图标
    【整理】treeGrid 树形表格
    【整理】iview中刷新页面的时候更新导航菜单的active-name
    [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用
    在.vue文件中让html代码自动补全的方法(支持vscode)
    解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号
  • 原文地址:https://www.cnblogs.com/wangna----558169/p/6085989.html
Copyright © 2011-2022 走看看