分页排序

目录结构

com.geyao.mybatis.mapper

BlogMapper类

package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTitle(String title);List<Blog> selectBlogByTitle2(String title);List<Blog> selectBlogBySort(String column);List<Blog> selectBlogByPage(int offset,int pagesize);
}

BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper"><!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类--><!-- 根据id查询得到一个user对象--><resultMap type="Blog" id="blogResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><result column="authod_id" property="authodId" jdbcType="INTEGER"/></resultMap><select id="selectBlog" parameterType="int"   resultType="Blog">select id,title,authod_id as authodId,state,featured,stylefrom Blog where id=#{id}</select> <select id="selectBlog2" parameterType="int"  resultMap="blogResultMap">select *from Blog where id=#{id}</select><select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">select * from Blog where title like #{title}</select><select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">select * from Blog where title like '${value}'</select><select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">select * from Blog order by ${value}</select><select id="selectBlogByPage"  resultMap="blogResultMap">select * from Blog limit #{0},#{1}</select>
</mapper>

com.geyao.mybatis.pojo

Blog类

package com.geyao.mybatis.pojo;public class Blog {private Integer id;private String title;private int authodId;private String state;private Boolean featured;private String style;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getAuthodId() {return authodId;}public void setAuthodId(int authodId) {this.authodId = authodId;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", authodId=" + authodId + ", state=" + state + ", featured="+ featured + ", style=" + style + "]\n";}}

com.geyao.mybatis.util

MybatisUtil类

package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><typeAliases><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/>
</typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- 配置数据库连接信息 --><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/></mappers>
</configuration>

单元测试

com.geyao.mybatis.util

testSelectBlog类

package com.geyao.mybatis.mapper;import java.util.List;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.util.MyBatisUtil;public class testSelectBlog {@Testpublic void testSelectBlogNoInterface() {SqlSession session =MyBatisUtil.getSqlSession();Blog blog =(Blog)session.selectOne("com.geyao.mybatis.mapper.BlogMapper.selectBlog", 101);session.close();System.out.println(blog);}
@Test
public void testSelectBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);System.out.println(blog);
}@Test
public void testSelectBlog2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog2(1);System.out.println(blog);
}
@Test
public void testselectBlogByTitle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle("%g%");System.out.println(blogList);
}@Test
public void testselectBlogByTitle2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle2("%g%");System.out.println(blogList);
}
@Test
public void testselectBlogBySort() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogBySort("title");System.out.println(blogList);
}
@Test
public void testselectBlogByPage() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage(2,2);System.out.println(blogList);
}
}

jar包

链接:https://pan.baidu.com/s/1g6NgzfLc5uK9S4VL-03lHg
提取码:4r2m

运行结果

mybatis学习(23):分页1 多参数传递(索引方式)相关推荐

  1. mybatis学习(25):分页3 多参数传递(使用map)

    分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.Lis ...

  2. mybatis学习(24):分页2 多参数传递(使用注解)

    分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.Lis ...

  3. Mybatis学习笔记-CURD(基于配置文件的方式)

    User.java实体类 public class User {private int id;private String username;private int age;//... } userM ...

  4. MyBatis多参数传递之Map方式示例——MyBatis学习笔记之十三

    前面的文章介绍了MyBatis多参数传递的注解.参数默认命名等方式,今天介绍Map的方式.仍然以前面的分页查询教师信息的方法findTeacherByPage为例(示例源代码下载地址:http://d ...

  5. MyBatis多参数传递之混合方式——MyBatis学习笔记之十五

    在本系列文章的<MyBatis多参数传递之Map方式示例>一文中,网友mashiguang提问如下的方法如何传递参数:public List findStudents(Map condit ...

  6. 【Mybatis】Mybatis学习由浅入深(二)

    前言   上篇介绍到了Mybatis的优缺点,这篇接下来介绍一下流程情况和配置信息. MyBatis简介 Mybatis工作流程 加载配置信息初始化 通过配置文件或注解将配置信息加载成Statemen ...

  7. (五)MyBatis学习总结(超级详细哦,尤其适合初学者)

    什么是MyBatis? MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射. MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作. MyBa ...

  8. Java框架学习笔记--Mybatis学习

    一.Mybatis学习 1.Mybatis简介 简介:Mybatis开源免费框架.原名叫iBatis,2010在googlecode,2013年迁移到github2 作用:数据访问层框架.底层是对JD ...

  9. 【Mybatis】mybatis如何实现分页

    [Mybatis]mybatis如何实现分页 mysql分页功能原理 实现分页必要条件 必须知道某一页从哪里开始到哪里结束 必须知道页面的大小,也就是指定每页要显示多少条数据量 mysql分页的过程 ...

最新文章

  1. 记一次数据库崩溃的恢复
  2. php类方法语法错误捕获,php语法错误捕获
  3. python读写文件函数_Python读写文件
  4. spring源码分析之spring-core总结篇
  5. php中插入表格 标签,PHP_HTML中的表格元素,一,table标签。tablegt - phpStudy
  6. telnet 22正常 ssh无法连接_Telnet咋就不安全了呢?带你来看用户名和密码
  7. python read_csv chunk_Python 数据分析之逐块读取文本的实现
  8. 请教 Discuz syscache 中一段cache 的意思
  9. Linux有问必答:如何检查Linux的内存使用状况
  10. L. Mod(预处理+分块)
  11. pyecharts 间距_高月双色球20108期:红球首尾间距参考29区段
  12. 攻防世界(Ctf-Web 新手练习区 Writeup)
  13. Android反射修改view,Android 修改viewpage滑动速度的实现代码
  14. poco c++框架:日期时间
  15. 关于vs2015每次打开都要配置opencv问题
  16. 操作系统原理、实现与实践课后习题参考答案(已完结)
  17. 工行u盾显示316_工行U盾只显示金融@家怎么显密码
  18. 药品质量检测的方法之一:质谱分析法
  19. [转]在线生成条形码(39码、EAN-13)
  20. 深度剖析未来网络服务模式 《云交换白皮书》一文全讲透

热门文章

  1. 58-混沌感悟.(2015.2.11)
  2. iOS 两个tableview的 瀑布流
  3. 谈 数学之美 和 看见
  4. SVN Could not open the requested SVN filesystem解决办法
  5. 杭电 1233 最小生成树 kruskal()算法
  6. java xml转map_java练习本(原每日一练)(20190514)
  7. 腾讯的老照片修复算法,我把它搬到网上,随便玩
  8. Workbox-Window v4.x 中文版
  9. Linux C编程学习--main()函数简析
  10. 文件系统ext3的文件大小限制