当前位置: 首页 > 工具软件 > JFinal-layui > 使用案例 >

JFinal框架从入门到精通(二)——jfinal+layui实现简单的增删改查

傅旺
2023-12-01

直接上代码

index.jsp前台列表页代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="shortcut icon" href="<%=path%>/images/favicon.ico">
<title>投票</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/common/layui/css/layui.css">
<script src="<%=path%>/common/layui/layui.js"></script>
<script src="<%=path%>/js/jquery.js"></script>
</head>
<body>

<div class="demoTable">
  搜索ID:
  <div class="layui-inline">
    <input class="layui-input" name="id" id="demoReload" autocomplete="off">
  </div>
  <button class="layui-btn zzk-search" data-type="reload">搜索</button>
  <button class="layui-btn zzk-add" data-type="add">添加</button>
</div>
	<table id="demo" lay-filter="test"></table>
	
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
$(function(){
	  $('.demoTable .zzk-add').on('click', function(){
		//alert("11");
		add();
		});
});
layui.use('table', function(){
  var table = layui.table;
  
  //第一个实例
  table.render({
    elem: '#demo'
    ,height: 312
    ,url: '<%=path%>/index1' //数据接口
    ,page: true //开启分页
    ,initSort: {
        field: 'Id' //排序字段,对应 cols 设定的各字段名
        ,type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
    }
    ,sort:true  //重点1:这里的sort表示 table表在取得接口数据后,对页面渲染后的table数据进行排序。同时,这里的true 会影响页面sort 上下小箭头的 显示效果
    ,cols: [[ //表头
      {field: 'Id', title: 'ID', width:80, sort: true, fixed: 'left'}
      ,{field: 'Name', title: '用户名', width:80}
      ,{field: 'Sex', title: '性别', width:80, sort: true}
      ,{field: 'Nationality', title: '国家', width:80} 
      ,{field: 'Phone', title: '电话', width: 80}
      ,{field: 'Salary', title: '工资', width: 135, sort: true}
      ,{fixed: 'right', title:'操作',width:178, align:'center', toolbar: '#barDemo'}
    ]]
  });

  table.on('sort(test)', function(obj){ //注:tool是工具条事件名,test是table原始容器的属性 lay-filter="对应的值"
	  console.log(obj.field); //当前排序的字段名
	  console.log(obj.type); //当前排序类型:desc(降序)、asc(升序)、null(空对象,默认排序)
	  console.log(this); //当前排序的 th 对象
	  
	  //尽管我们的 table 自带排序功能,但并没有请求服务端。
	  //有些时候,你可能需要根据当前排序的字段,重新向服务端发送请求,从而实现服务端排序,如:
	  table.reload('demo', {
	    initSort: obj //记录初始排序,如果不设的话,将无法标记表头的排序状态。 layui 2.1.1 新增参数
	    ,where: { //请求参数(注意:这里面的参数可任意定义,并非下面固定的格式)
	      field: obj.field //排序字段   在接口作为参数字段  field order
	      ,order: obj.type //排序方式   在接口作为参数字段  field order
	    }
	  });
	});
  

  //监听工具条
  table.on('tool(test)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      layer.msg('ID:'+ data.Id + ' 的查看操作');
    } else if(obj.event === 'del'){
      layer.confirm('真的删除行么', function(index){
    	  if(deletee(data.Id)){
    	        obj.del();
    	        layer.close(index);    		  
    	  };
      });
    } else if(obj.event === 'edit'){
      //layer.alert('编辑行:<br>'+ JSON.stringify(data.Id))
    	$(location).attr('href', '<%=path%>/edit?editId='+data.Id);
    }
  });
  
  var $ = layui.$, active = {
    reload: function(){
      var demoReload = $('#demoReload');
      
      //执行重载
      table.reload('demo', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          key: {
            parameter: {
            	Nationality:demoReload.val(),
            	uName:demoReload.val()
            }
          }
        }
      }, 'data');
    }
  };
  
  $('.demoTable .zzk-search').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
function add(){
	$(location).attr('href', '<%=path%>/add');
}
function deletee(Uid){
	var rt=false;
	$.ajax( {    
	    url:'<%=path%>/delete',// 跳转到 action    
	    data:JSON.stringify({    __method:"DELETE",
	             Uid : Uid
	    }),    
	    type:'delete',    
	    cache:false,    
	    dataType:'json', 
	    async : false,
	    success:function(data) {    
	        if(data.code =="1" ){  
	            //alert("修改成功!");
	            rt = true;
	        }else{    
	        	alert(data.msg);    
	        }    
	     },    
	     error : function() { 
	          alert("异常!");    
	     }    
	}); 
	return rt;
}
</script>
</body>
</html>

add.jsp新增页代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="shortcut icon" href="<%=path%>/images/favicon.ico">
<title>添加</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/common/layui/css/layui.css">
<script src="<%=path%>/common/layui/layui.js"></script>
<script src="<%=path%>/js/jquery.js"></script>
<style>
.layui-upload-img{width: 92px;height: 92px;margin: 0 10px 10px 0;}
</style>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>表单集合演示</legend>
</fieldset>
 
<form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-block">
      <input type="text" name="Name" lay-verify="required" lay-reqtext="用户名是必填项,岂能为空?" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">手机号</label>
      <div class="layui-input-inline">
        <input type="tel" name="Phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">年龄(非空数字)</label>
      <div class="layui-input-inline">
        <input type="text" name="Age" lay-verify="required|number" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">工资(非空数字)</label>
      <div class="layui-input-inline">
        <input type="text" name="Salary" lay-verify="required|number" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">所属地</label>
    <div class="layui-input-block">
      <select name="Nationalty" lay-filter="aihao">
        <option value=""></option>
        <option value="0">请选择</option>
        <option value="中国" selected="">中国</option>
        <option value="中国">美国</option>
      </select>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="sex" value="1" title="男" checked="">
      <input type="radio" name="sex" value="0" title="女">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
      
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>常规使用:普通图片上传</legend>
</fieldset>
 
<div class="layui-upload">
  <button type="button" class="layui-btn" id="test1">上传图片</button>
  <div class="layui-upload-list">
    <img class="layui-upload-img" id="demo1">
    <p id="demoText"></p>
  </div>
</div>   

</form>
 
          
<script>
layui.use('upload', function(){
	  var $ = layui.jquery
	  ,upload = layui.upload;
	  
	  //普通图片上传
	  var uploadInst = upload.render({
	    elem: '#test1'
	    ,url: '<%=path%>/upload' //改成您自己的上传接口
	    ,before: function(obj){
	      //预读本地文件示例,不支持ie8
	      obj.preview(function(index, file, result){
	        $('#demo1').attr('src', result); //图片链接(base64)
	      });
	    }
	    ,done: function(res){
		      console.log(res);
	      //如果上传失败
	      if(res.code <= 0){
	        return layer.msg('上传失败');
	      }
	      //上传成功
	    }
	    ,error: function(){
	      //演示失败状态,并实现重传
	      var demoText = $('#demoText');
	      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
	      demoText.find('.demo-reload').on('click', function(){
	        uploadInst.upload();
	      });
	    }
	  });
	  
	  
	  
	});

layui.use(['form'], function(){
  var form = layui.form
  ,layer = layui.layer;  
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位,且不能出现空格'
    ]
  });
  
  //监听提交
  form.on('submit(demo1)', function(data){
    //layer.alert(JSON.stringify(data.field), {
      //title: '最终的提交信息'
    //})
    if(submit(data.field)){
    	location.href='<%=path%>/index';
    	return false;
    }else{
    	return false;
    };
  });
});
function submit(data){
	var rt=false;
	$.ajax( {    
	    url:'<%=path%>/submitAdd',// 跳转到 action    
	    data:JSON.stringify(data),    
	    type:'post',    
	    cache:false,    
	    dataType:'json', 
	    async : false,
	    success:function(data) {    
	        if(data.code =="1" ){  
	            rt = true;
	        }else{    
	        	alert(data.msg);    
	        }    
	     },    
	     error : function() { 
	          alert("异常!");    
	     }    
	}); 
	return rt;
}
</script>
</body>
</html>

edit.jsp修改页代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="shortcut icon" href="<%=path%>/images/favicon.ico">
<title>添加</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/common/layui/css/layui.css">
<script src="<%=path%>/common/layui/layui.js"></script>
<script src="<%=path%>/js/jquery.js"></script>
<style>
.layui-upload-img{width: 92px;height: 92px;margin: 0 10px 10px 0;}
</style>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
  <legend>赋值和取值</legend>
</fieldset>
 
<form class="layui-form" action="" lay-filter="example">
  <div class="layui-form-item">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-block">
    <input type="hidden" name="Id">
      <input type="text" name="Name" lay-verify="required" lay-reqtext="用户名是必填项,岂能为空?" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">手机号</label>
      <div class="layui-input-inline">
        <input type="tel" name="Phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">年龄(非空数字)</label>
      <div class="layui-input-inline">
        <input type="text" name="Age" lay-verify="required|number" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">工资(非空数字)</label>
      <div class="layui-input-inline">
        <input type="text" name="Salary" lay-verify="required|number" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">所属地</label>
    <div class="layui-input-block">
      <select name="Nationalty" lay-filter="aihao">
        <option value="0">请选择</option>
        <option value="中国" selected="">中国</option>
        <option value="美国">美国</option>
      </select>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="sex" value="true" title="男" checked="">
      <input type="radio" name="sex" value="false" title="女">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
    </div>
  </div>
      
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>常规使用:普通图片上传</legend>
</fieldset>
 
<div class="layui-upload">
  <button type="button" class="layui-btn" id="test1">上传图片</button>
  <div class="layui-upload-list">
    <img class="layui-upload-img" id="demo1">
    <p id="demoText"></p>
  </div>
</div>   

</form>
 
          
<script>
layui.use('upload', function(){
	  var $ = layui.jquery
	  ,upload = layui.upload;
	  
	  //普通图片上传
	  var uploadInst = upload.render({
	    elem: '#test1'
	    ,url: '<%=path%>/upload' //改成您自己的上传接口
	    ,before: function(obj){
	      //预读本地文件示例,不支持ie8
	      obj.preview(function(index, file, result){
	        $('#demo1').attr('src', result); //图片链接(base64)
	      });
	    }
	    ,done: function(res){
		      console.log(res);
	      //如果上传失败
	      if(res.code <= 0){
	        return layer.msg('上传失败');
	      }
	      //上传成功
	    }
	    ,error: function(){
	      //演示失败状态,并实现重传
	      var demoText = $('#demoText');
	      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
	      demoText.find('.demo-reload').on('click', function(){
	        uploadInst.upload();
	      });
	    }
	  });
	  
	  
	  
	});

layui.use(['form'], function(){
  var form = layui.form
  ,layer = layui.layer;  
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位,且不能出现空格'
    ]
  });
var ss="${ret}";
    form.val('example', {
      "Id": "${ret.data[0].Id}" ,
      "Name": "${ret.data[0].Name}" ,
      "Phone": "${ret.data[0].Phone}" ,
      "Age": "${ret.data[0].Age}" ,
      "Salary": "${ret.data[0].Salary}" ,
      "Nationalty": "${ret.data[0].Nationality}" ,
      "sex": "${ret.data[0].Sex}" 
    });
  //监听提交
  form.on('submit(demo1)', function(data){
    //layer.alert(JSON.stringify(data.field), {
      //title: '最终的提交信息'
    //})
    if(submit(data.field)){
    	location.href='<%=path%>/index';
    	return false;
    }else{
    	return false;
    };
  });
});
function submit(data){
	var rt=false;
	$.ajax( {    
	    url:'<%=path%>/submitEdit',// 跳转到 action    
	    data:JSON.stringify(data),    
	    type:'post',    
	    cache:false,    
	    dataType:'json', 
	    async : false,
	    success:function(data) {    
	        if(data.code =="1" ){  
	            rt = true;
	        }else{    
	        	alert(data.msg);    
	        }    
	     },    
	     error : function() { 
	          alert("异常!");    
	     }    
	}); 
	return rt;
}
</script>
</body>
</html>

indexController.java后台控制器代码

package com.zzkMavenJfinal.controller.index;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.zzkMavenJfinal.model.SysUser;
import com.zzkMavenJfinal.model.tb_Enclosure;
import com.zzkMavenJfinal.model.tb_staff;


public class indexController extends Controller {
	IndexService service = IndexService.SERVICE;
	public void index1() {
		String Nationality="";
		String field="";
		String order="";
		Integer ii=0;
		String sql=" from tb_staff ";
		if (getPara("key[parameter][Nationality]")!=null&&!"".equals(getPara("key[parameter][Nationality]"))) {
			ii++;
			String kw = getPara("key[parameter][Nationality]");
			Nationality="%"+kw+"%";;
			sql+=" where Nationality like ? ";
		}
		if (getPara("field")!=null&&!"".equals(getPara("field"))) {
			field=getPara("field");
		}
		if (getPara("order")!=null&&!"".equals(getPara("order"))) {
			order=getPara("order");
			sql+=" order by "+field+" "+order+" ";
		}
    	Object[] para = new Object[]{Nationality};
		Object[] obj = new Object[ii];
		for (int i = 0; i < obj.length; i++) {
			for (int j = 0; j < para.length; j++) {
				if (para[j]!=""&&para[j]!=null) {
					obj[i]=para[j];
					para[j]="";
					break;
				}
			}
		}
		Integer page = Integer.parseInt(getPara("page"));
		Integer limit = Integer.parseInt(getPara("limit"));
        Page<tb_staff> blogs=service.queryList1(page,limit,sql,obj);
        Ret ret=Ret.create();
        ret.setOk();
        ret.set("data",blogs.getList());
        ret.set("code",0);
        ret.set("msg","");
        ret.set("count",blogs.getTotalRow());
        //setAttr("ret", ret);
        renderJson(ret);
	}
	/**
     * 查找所有blog
     */
    public void index(){
        render("index.jsp");
    }
    public void delete() {
    	Integer code=0;
    	String msg="";
    	String string = HttpKit.readData(getRequest());
		//System.out.println(string);/
		JSONObject jsonObject = JSONObject.parseObject(string);
		int Uid = jsonObject.getIntValue("Uid");
		if (Uid!=0) {
			try {
				Integer kw = Uid;
				if (service.delect(kw)) {
					code=1;
					msg="删除成功!";
				} else {
					code=0;
					msg="删除失败!";
				}
			} catch (Exception e) {
				code=0;
				msg="删除失败!"+e.getMessage();
			}
		}
		Ret ret=Ret.create();
        ret.setOk();
        ret.set("code",code);
        ret.set("msg",msg);
        renderJson(ret);
    }

    public void add(){
        render("add.jsp");
    }
    public void submitAdd(){
    	Integer code=0;
    	String msg="";
    	String string = HttpKit.readData(getRequest());
		//System.out.println(string);/
		JSONObject jsonObject = JSONObject.parseObject(string);
		String Name = jsonObject.getString("Name");
		String Phone = jsonObject.getString("Phone");
		String Age = jsonObject.getString("Age");
		String Salary = jsonObject.getString("Salary");
		String Nationalty = jsonObject.getString("Nationalty");
		String sex = jsonObject.getString("sex");
		List<tb_staff> tbstasEs=new ArrayList<tb_staff>();
		tb_staff tbs=new tb_staff();
		tbs.set("Name", Name);
		tbs.set("Phone", Phone);
		tbs.set("Age", Age);
		tbs.set("Salary", Salary);
		tbs.set("Nationality", Nationalty);
		tbs.set("Sex", sex);
		tbstasEs.add(tbs);
		try {
			if (service.submitAdd(tbstasEs)) {
				code=1;
				msg="新增成功!";
			} else {
				code=0;
				msg="新增失败!";
			}
		} catch (Exception e) {
			code=0;
			msg="新增失败!"+e.getMessage();
		}
		Ret ret=Ret.create();
        ret.setOk();
        ret.set("code",code);
        ret.set("msg",msg);
        renderJson(ret);
    }
    public void submitEdit(){
    	Integer code=0;
    	String msg="";
    	String string = HttpKit.readData(getRequest());
		//System.out.println(string);/
		JSONObject jsonObject = JSONObject.parseObject(string);
		String Id = jsonObject.getString("Id");
		String Name = jsonObject.getString("Name");
		String Phone = jsonObject.getString("Phone");
		String Age = jsonObject.getString("Age");
		String Salary = jsonObject.getString("Salary");
		String Nationalty = jsonObject.getString("Nationalty");
		String sex = jsonObject.getString("sex");
		List<tb_staff> tbstasEs=new ArrayList<tb_staff>();
		tb_staff tbs=new tb_staff();
		tbs.set("Id", Id);
		tbs.set("Name", Name);
		tbs.set("Phone", Phone);
		tbs.set("Age", Age);
		tbs.set("Salary", Salary);
		tbs.set("Nationality", Nationalty);
		tbs.set("Sex", sex);
		tbstasEs.add(tbs);
		try {
			if (service.submitUpdate(tbstasEs)) {
				code=1;
				msg="修改成功!";
			} else {
				code=0;
				msg="修改失败!";
			}
		} catch (Exception e) {
			code=0;
			msg="修改失败!"+e.getMessage();
		}
		Ret ret=Ret.create();
        ret.setOk();
        ret.set("code",code);
        ret.set("msg",msg);
        renderJson(ret);
    }
    public void upload() {
		Ret ret=Ret.create();
        ret.setOk();
		Ret ret1=Ret.create();
        ret1.setOk();
        try {
            UploadFile file = getFile();
            System.out.println("--------file--------");
            File delfile = new File(file.getUploadPath()+"\\"+file.getFileName());
            System.out.println("=========="+delfile.getPath());
            ret.set("code",1);
            ret.set("msg","上传成功!");
            ret1.set("filePath",delfile.getPath());
            ret1.set("fileSize",delfile.length()/1024+"");
            ret.set("ret1",ret1);
        } catch (Exception e) {
            e.printStackTrace();
            ret.set("code",0);
            ret.set("msg",e.getMessage());
        }
        renderJson(ret);        
    }
    public void edit() {
    	Ret ret =new Ret();
    	ret.setOk();
    	try {
			Integer idInteger=getParaToInt("editId");
			//List<tb_staff> lstbList=new ArrayList<tb_staff>();
			List<tb_staff> lstbList=service.queryList2(idInteger);
            ret.set("code",1);
            ret.set("msg","ok");
            ret.set("data",lstbList);
		} catch (Exception e) {
			// TODO: handle exception
            ret.set("code",0);
            ret.set("msg",e.getMessage());
		}
		setAttr("ret", ret);
		render("edit.jsp");
    }
}

Indexservice.java业务层代码

package com.zzkMavenJfinal.controller.index;

import java.sql.SQLException;
import java.util.List;

import org.eclipse.jdt.internal.compiler.flow.FinallyFlowContext;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.zzkMavenJfinal.model.SysUser;
import com.zzkMavenJfinal.model.tb_Enclosure;
import com.zzkMavenJfinal.model.tb_staff;


public class IndexService {
	public static final IndexService SERVICE = new IndexService();
	private SysUser blog=new SysUser().dao();
	private tb_Enclosure tb=new tb_Enclosure().dao();
	private tb_staff stb=new tb_staff().dao();


    public SysUser queryById(int id){
        return blog.findById(id);
    }

    public List<SysUser> queryList(){
        List<SysUser> blogList=blog.find("select * from tb_vote");
        return blogList;
    }
    public Page<tb_staff> queryList1(Integer page,Integer limit,String sql,Object[] obj){
        //List<tb_Enclosure> blogList=tb.find("select * from tb_Enclosure");
    	Page<tb_staff> blogList=stb.paginate(page, limit, "select *", sql, obj);
        return blogList;
    }
    public List<tb_staff> queryList2(Integer id){
        List<tb_staff> blogList=stb.find("select * from tb_staff where Id=?",id);
        return blogList;
    }
	public Boolean delect(int iid) {
		Integer ss=iid;
		int count = Db.delete("delete tb_staff where Id=?",iid);
    	return count == 1;
		//Boolean bool =stb.deleteById(id);
		//return bool;
	}

    public void delectById(int id){
        blog.deleteById(id);
    }
    public Boolean submitAdd(final List<tb_staff> votes) {

		return Db.tx(new IAtom() {
			public boolean run() throws SQLException {
				// TODO Auto-generated method stub
				String sql = "insert into tb_staff(Name, Sex, Age, Phone, Salary, Nationality) values(?,?,?,?,?,?)";
				Db.batch(sql, "Name, Sex, Age, Phone, Salary, Nationality", votes,
						votes.size());
				return true;
			}
		});
    }
    public Boolean submitUpdate(final List<tb_staff> votes) {

		return Db.tx(new IAtom() {
			public boolean run() throws SQLException {
				// TODO Auto-generated method stub
				String sql = "update tb_staff set Name=?, Sex=?, Age=?, Phone=?, Salary=?, Nationality=? where Id=? ";
				Db.batch(sql, "Name, Sex, Age, Phone, Salary, Nationality,Id", votes,
						votes.size());
				return true;
			}
		});
    }
}

Do you understand??
GitHub下载地址https://github.com/zzkjxp/jfinal-layui-.git

 类似资料: