1.按条件查询数据
1.1已知参数查询法
/**
* @desc query user list
* @return List<User> list
*/
public List<User> queryUserList(){
//查询用户id大于8的用户
List<User> list = me.find("select * from user where id > 8");
return list;
}
1.1传参数查询法
需要传递的参数用占位符表示“?”,具体的参数用“,”隔开继续跟在方法后面。代码如下:
/**
* @desc query user list
* @return List<User> list
*/
public List<User> queryUserList(){
List<User> list = me.find("select * from user where id > ?",9);
return list;
}
2.分页查询
分页查询是开发系统中比较重要的操作,jfinal分页可以说是非常的简单。只需要编写sql查询语句,按约定传递分页参数即可。示例如下:
2.1 Model层的代码:
/**
* @desc query user list
* @return List<User> list
*/
public Page<User> queryUserList(){
//List<User> list = me.find("select * from user where id > ?",9);
//第一个参数表示:当前页;第二个参数表示每页显示多少条数据;
Page<User> list = me.paginate(1,4,"select * ","from user where id > ?",9);
return list;
}
2.2 html代码:
为了展示效果美观,这里使用了bootstrap这样一个前端框架。
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>用户列表</title>
<!-- Bootstrap -->
<!--link href="css/bootstrap.min.css" rel="stylesheet"-->
<link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>密码</th>
</tr>
</thead>
<!--循坏输出列表数据-->
<#list userList.getList() as user>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.pwd}</td>
</tr>
</#list>
</table>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!--script src="js/bootstrap.min.js"></script>-->
<script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>