标签:art val ase tar als 开始 nbsp 文件 索引
使用mybatis框架后,mybatis-generator可以为我们自动生成操作数据库(MYSQL)的*Mapper.xml文件+模型类+*Mapper接口,但是,mybatis-generator并不能为我们生成所有方法,例如根据输入的不定条件来查询数据,又比如按照某个参数升序或者降序并分页显示所查到的数据,下面介绍一下条件不定查询及按降序/升序分页显示的方法。
1、按条件不定查询(项目基于SSMM(spring+springmvc+maven+mybatis)框架整合的代码进行举例)
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>
List<Book> selectByCondition(Book record);
1 public List<Book> selectByCondition(Book book) { 2 return bookMapper.selectByCondition(book); 3 }
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 }
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、按降序/升序分页显示(基于缓存(本地缓存)花店的代码实现)
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>
1 List<Flower> selectLimit(@Param("start") int start, @Param("size") int size);
上述start表示开始显示记录索引,size表示显示记录的条数,为了方便找到这两个参数,这里使用了@Param注解
1 /* 2 * 传入开始显示记录的索引,即显示记录的条数,实现分页显示功能 3 */ 4 public List<Flower> selectLimit(int start, int size) { 5 return flowerMapper.selectLimit(start, size); 6 }
1 public List<Flower> selectLimit(Integer start, Integer size) { 2 return flowerDao.selectLimit(start, size); 3 }
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 }
标签:art val ase tar als 开始 nbsp 文件 索引
原文地址:http://www.cnblogs.com/wangna----558169/p/6085989.html