|
楼主 |
发表于 2017-1-20 15:57:30
|
显示全部楼层
- //后台java
- public static void queryUser(ServletRequest request, ServletResponse response) threw SQLException, IOException, NamingException{
- JSONObject params=(JSONObject) JSONObject.parse(request.getParameter("params"));
- Object columns= params.get("columns");
- Integer limit=params.getInteger("limit");
- Integer offset = params.getInteger("offset");
- String search=params.getString("search");
-
- List<Object> sqlParams= new ArrayList<Object>();
- String sqlWhere="";
- if(!Util.isEmptyString(search)){
- sqlWhere =" WHERE (u.fID LIKE ? OR u.fName LIKE ? OR u.fPhoneNumber LIKE ? OR u.fAddress LIKE ?) ";
- search=search.indexOf("%")==-1?"%"+search+"%": search;
- for(Integer i=0;i<4,i++){
- sqlParams.add(search);
- }
- }
- String sql= "SELECT u.fID, u.fName, u.fPhoneNumber, u.fAddress, COUNT(ord.fID) AS orderCount "
- + " FROM takeout_user u "
- + " LEFT JOIN takeout_order ord ON u.fID = ord.fUserID "
- + sqlWhere
- + " GROUP BY u.fID, u.fName, u.fPhoneNumber, u.fAddress ";
-
- Table table=null;
- Connection=Util.getConnection(DATASOURCE_TAKEOUT);
- try{
- table=Util.queryData(conn,sql,sqlParams,columns,offset,limit);
- if(offset!=null&& offset.equals(1)){
- String sqlTotal="SELECT COUNT(*) FROM takeout_user u "+sqlWhere;
- Object total=Util.getValueBySQL(conn,sqlTotal,sqlParams);
- table.setTotal(Integer.parseInt(total.toString()));
- }
-
- }finally{
- conn.close();
- }
- Util.writeTableToResponse(response,table);
-
- }
- public static void saveUser(ServletRequest request, ServletResponse response) threw ParseException, SQLException, NamingException{
- JSONObject params=(JSONObject) JSONObject.parse(request.getParameter("params"));
- JSONObject data=params.getJSONObject("data");
- Table table= Transform.jsonToTable(data);
-
- Connection conn=Util.getConnection(DATASOURCE_TAKEOUT);
- try{
-
- conn.setAutoCommint(false);
- try{
- Collection<String> columns=table.getColumnName();
- column.remove("orderCount");
- Util.saveData(conn,table,TABLE_TAKEKOUT_USER,columns);
- conn.commit();
- }catch(SQLException e){
- conn.rollback();
- threw e;
- }
- }finally{
- conn.close();
- }
-
- }
复制代码 |
|