今日学习笔记

1.表结构分析  
 
id  varchar2(32)  无意义、主键uuid
orderNum  varchar2(50)  日程编号 不为空 唯一
orderTime  timestamp  日程产生时间
orderDesc  varchar2(500)  日程描述(其它具体信息)
orderStatus  int  日程状态(0 未完成 1 已完成)
 
创建表sql
CREATE TABLE orders(
id varchar2(32) default SYS_GUID() PRIMARY KEY,
orderNum VARCHAR2(20) NOT NULL UNIQUE,
orderTime timestamp,
orderDesc VARCHAR2(500),
orderStatus INT,
)
insert into ORDERS (id, ordernum, ordertime,  orderdesc, orderstatus)
 
values ('0E7231DC797C486290E8713CA3C6ECCC', '12345', to_timestamp('01-03-2021 12:00:00.000000',
private int orderStatus;
private int peopleCount;
private Product product;
private String orderDesc;
//省略getter/setter
}
 
实体类
public class Traveller {
private String id;
private String name;
private String sex;
private String phoneNum;
private Integer credentialsType;
private String credentialsTypeStr;
private String credentialsNum;
private Integer travellerType;
private String travellerTypeStr;
//省略getter/setter
}
 
IProductDao的fifindById
@Select("select * from product where id=#{id}")
Product findById(String id) throws Exception;
 
2.分页查询
  2.1.PageHelper介绍
PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql、
oracle、mariaDB、DB2、SQLite、Hsqldb等。
  2.2.PageHelper使用
集成:引入分页插件有引入jar包和创建maven两种方式,这里使用的是Maven
  2.3.Maven搭建
在 pom.xml 中添加如下依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency
  2.4.配置
新版拦截器是 com.github.pagehelper.PageInterceptor 。 com.github.pagehelper.PageHelper
现在是一个特殊的 dialect 实现类,是分页插件的默认实现类,提供了和以前相同的用法
  2.5.在 MyBatis 配置 xml 中配置拦截器插件
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
  2.6.在 Spring 配置文件中配置拦截器插件
使用 spring 的属性配置方式,可以使用 plugins 属性像下面这样配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注意其他配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
params=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
  2.7.分页插件参数介绍
    1. helperDialect :分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 可以配置
helperDialect 属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:
oracle , mysql , mariadb , sqlite , hsqldb , postgresql , db2 , sqlserver , informix , h2 , sqlserver2012 , derby
特别注意:使用 SqlServer2012 数据库时,需要手动指定为 sqlserver2012 ,否则会使用 SqlServer2005 的
方式进行分页。
也可以实现 AbstractHelperDialect,然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。
    2. offsetAsPageNum :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。 当该参数
设置为true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。
    3. rowBoundsWithCount :默认值为 false ,该参数对使用 RowBounds 作为分页参数时有效。
当该参数设置为 true 时,使用 RowBounds 分页会进行 count 查询。
    4. pageSizeZero :默认值为 false ,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit =
0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。
    5. reasonable :分页合理化参数,默认值为 false 。当该参数设置为 true 时, pageNum<=0 时会查询第一
页, pageNum>pages (超过总数时),会查询最后一页。默认 false 时,直接根据参数进行查询。
    6. params :为了支持 startPage(Object params) 方法,增加了该参数来配置参数映射,用于从对象中根据属
性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable ,不配置映射的用默认值, 默认值为
pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
    7. supportMethodsArguments :支持通过 Mapper 接口参数来传递分页参数,默认值 false ,
分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
使用方法可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和ArgumentsObjTest。
    8. autoRuntimeDialect :默认值为 false 。设置为 true 时,允许在运行时根据多数据源自动识别对应方言
的分页 (不支持自动选择 sqlserver2012 ,只能使用 sqlserver ),用法和注意事项参考下面的场景五。
    9. closeConn :默认值为 true 。当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类
型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认 true 关闭,设置为false 后,
不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。
 
  2.8.基本使用
PageHelper的基本使用有6种,大家可以查看文档,最常用的有两种
1)RowBounds方式的调用
List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(1, 10));
使用这种调用方式时,可以使用RowBounds参数进行分页,这种方式侵入性最小,通过
RowBounds方式调用只是使用了这个参数,并没有增加其他任何内容。
分页插件检测到使用了RowBounds参数时,就会对该查询进行物理分页。
关于这种方式的调用,有两个特殊的参数是针对 RowBounds 的,可以参看上面的分页插件参数介绍
注:不只有命名空间方式可以用RowBounds,使用接口的时候也可以增加RowBounds参数,例如
//这种情况下也会进行物理分页查询
List<Country> selectAll(RowBounds rowBounds);
由于默认情况下的 RowBounds 无法获取查询总数,分页插件提供了一个继承自 RowBounds 的
PageRowBounds ,这个对象中增加了 total 属性,执行分页查询后,可以从该属性得到查询总数
2)PageHelper.startPage 静态方法调用
在需要进行分页的 MyBatis 查询方法前调用PageHelper.startPage 静态方法即可
紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
  2.9.Service
@Override
public List<Orders> findAllByPage(int page, int pageSize) throws Exception {
PageHelper.startPage(page, pageSize);
return ordersDao.findAllByPage();
}
  2.10.Controller
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1")
Integer page, @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer
pageSize) throws Exception {
List<Orders> ordersList = ordersService.findAllByPage(page, pageSize);
PageInfo pageInfo = new PageInfo(ordersList);
ModelAndView mv = new ModelAndView();
mv.setViewName("order-list");
mv.addObject("pageInfo", pageInfo);
return mv;
}
  2.11.分页查询页面Order-list.jsp
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px;"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting_desc">日程编号</th>
<th class="sorting_asc sorting_asc_disabled">名称</th>
<th class="sorting">时间</th>
<th class="text-center sorting">日程状态</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>


<c:forEach items="${pageInfo.list}" var="orders">

<tr>
<td><input name="ids" type="checkbox"></td>
<td>${orders.id }</td>
<td>${orders.orderNum }</td>
<td>${orders.product.productName }</td>
<td>${orders.orderTimeStr }</td>
<td class="text-center">${orders.orderStatusStr }</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">日程详情</button>
<button type="button" class="btn bg-olive btn-xs">编辑</button>
<button type="button" class="btn bg-olive btn-xs">删除</button>
</td>
</tr>
</c:forEach>
</tbody>

</table>
<!--数据列表/-->
posted @ 2021-01-05 16:12  计算机语言学习日志  阅读(115)  评论(0)    收藏  举报