sqlalchemy 配合bootstrap-table实现后台分页
创建公共的mysql连接函数
def db_session(): db_config='mysql+mysqldb://'+ mysqluser + ':' + mysqlpassword+'@'+mysqlip+'/'+dbname + '?charset=utf8' echo = True IsolationLevel='READ COMMITTED' engine=create_engine(db_config,pool_recycle=60,isolation_level=IsolationLevel,echo=echo) session=sessionmaker(bind=engine) return session()
python代码
# -*- encoding: utf-8 -*- import tornado from sqlalchemy import * from common.db_session import * from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor class checkJDBCList(tornado.web.RequestHandler): executor = ThreadPoolExecutor(20) @tornado.gen.coroutine def get(self): # 跳转页面 title = "test" ps = self.get_page_config(title) self.render('test.html',**ps)
@tornado.gen.coroutine def post(self): # 表格请求数据 system = self.get_arg("system", "%") offset = self.get_arg('offset', 0) order_field = self.get_arg('order_field', "createTime") order_type = self.get_arg("order_type", "desc") page_size = self.get_arg('pageSize', 10) # 我们自定义的方法不支持异步。所以使用协程来提升性能 total_count, connection_list = yield self.connection_number_warning(employee_number, system_enname, offset, order_field, order_type, page_size) # total和rows是必须要的2个参数,可以添加其他的参数 self.write({"total": total_count, "rows": connection_number_warning_list}) @run_on_executor def connection_number_warning(self, employee_number, system_enname, offset, order_field, order_type, page_size): total_count = 0 connection_numbe_list = [] session = db_session() try: connection_list = session.query() # 查询的字段 connection_list = connection_list.filter() # 查询条件 connection_list = connection_list.offset(offset) # 跳过多少行,分页用的 connection_list = connection_list.order_by(order_field + " " + order_type) # 排序字段和方式 connection_list = connection_list.limit(page_size).all() # 查询多少行数据 for connection_db in connection_list: # 将查询的结果转换成[{"key": "value"}]这种格式 connection = {} connection["system"] = connection_db[0] connection_number_list.append(connection) total_count = session.query(func.count("*")).filter().scalar() #查询符合条件的数据总量 except Exception as e: print e finally: session.close() return total_count, connection_number_warning_list