Springmvc+easyui paging, querying (full crud)

Source: Internet
Author: User
Tags getmessage

Finally finished crud function, note, here will have some changes in front, usercontroller of the Listuser () has been rewritten, now put all tidy it up.

Jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Controller:

Package Com.yang.bishe.controller;import Java.util.hashmap;import Java.util.list;import java.util.Map;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.servletrequestutils;import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.servlet.modelandview;import Com.yang.bishe.entity.grid;import Com.yang.bishe.entity.json;import Com.yang.bishe.entity.user;import Com.yang.bishe.service.interfaces.IUserService; @Controller @requestmapping ("/user") public class Usercontroller Extends Basecontroller {@Autowiredprivate iuserservice userservice; @RequestMapping ("/list") public Modelandview    Golist () {return new Modelandview ("User/list"); } @RequestMapping ("/listusers") public String Listuser (httpservletrequest request,httpservletresponse response) Throws Exception {Page=servleTrequestutils.getintparameter (Request, "page", 1);//default value is 1rows=servletrequestutils.getintparameter (Request, "Rows" , 0); String queryuserid=request.getparameter ("Queryuserid");//Gets the user account to query grid Grid = new Grid (); String hql; List<user>users;if (queryuserid!=null) {hql= "from user as user where user. UserId like '% ' +queryuserid+ "% '";} Else{hql= "from User";} Users= (list<user>) userservice.find (HQL, page, rows), Grid.settotal (Userservice.count ("SELECT count (*) + hql)")    ; grid.setrows (users); Writejson (grid,response); return null; } @RequestMapping ("/adduser") public String addUser (User user,httpservletrequest request,httpservletresponse response ) throws Exception{json JSON = new JSON ();//used to send messages to the front End if (Userservice.getbyid (User.getuserid ())!=null) {json.setmsg (" New user failed, user already exists! ");} Else{userservice.save (user); Json.setmsg ("New user succeeded! "); Json.setsuccess (true);} Writejson (json,response); return null;} @RequestMapping ("/removeuser") public String Removeuser (httpservletrequest request,httpservletresponse response) throws Exception{json JSON = new JSON ();//used to send a message to the front end string Userid=request.getparameter ("id"); Try{userservice.delete ( (user) Userservice.getbyid (userId));//Do not know why to add (user) to the. Json.setmsg ("Delete succeeded! "); Json.setsuccess (true); Writejson (json,response); return null;} catch (Exception e) {json.setmsg ("Delete failed! "+e.getmessage ()); Writejson (json,response); return null;}} @RequestMapping ("/updateuser") public String UpdateUser (User user,httpservletresponse response) throws Exception{json JSON = new JSON ();//used to send message to front end try{userservice.update (user); Json.setmsg ("Update succeeded! "); Json.setsuccess (true); Writejson (json,response); return null;} catch (Exception e) {json.setmsg ("Update failed! "+e.getmessage ()); Writejson (json,response); return null;}}}

In fact, some logic inside the listuser can be put on the service, not yet ...

Service layer Nothing, is inheriting a baseservice

Package Com.yang.bishe.service.impl;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Com.yang.bishe.dao.ibasedao;import Com.yang.bishe.entity.User; Import Com.yang.bishe.service.base.baseserviceimpl;import com.yang.bishe.service.interfaces.iuserservice;@ Servicepublic class Userserviceimpl extends baseserviceimpl<user> implements Iuserservice {@Autowiredprivate Ibasedao<user> Userdao;}

Userdao are inherited Basedao: I will stick to some of the functions used (interface what is not posted out):

Package Com.yang.bishe.dao;import Java.io.serializable;import Java.math.biginteger;import java.util.List;import Java.util.map;import Org.hibernate.query;import Org.hibernate.sqlquery;import Org.hibernate.Session;import Org.hibernate.sessionfactory;import Org.hibernate.transform.transformers;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.repository;@ Repositorypublic class basedaoimpl<t>implements ibasedao<t> {@Autowiredprivate sessionfactory sessionfactory;/** * Get the current thing session * * @return org.hibernate.Session */public session getcurrentsession () {return Sessio Nfactory.getcurrentsession ();} @Overridepublic Serializable Save (T o) {if (o! = null) {return getcurrentsession (). Save (O); return null;} @SuppressWarnings ("unchecked") @Overridepublic T getById (class<t> C, Serializable ID) {return (T) Getcurrentsession (). Get (c, id);} @Overridepublic T getbyhql (String hql) {Query q = getcurrentsession (). CreateQuery (HQL); List<t&gT L = q.list (); if (l! = null && l.size () > 0) {return l.get (0);} return null;} @Overridepublic void Delete (T o) {if (o! = null) {getcurrentsession (). Delete (o);}} @Overridepublic void update (T o) {if (o! = null) {getcurrentsession (). Update (o);}} @Overridepublic list<t> Find (String hql) {Query q = getcurrentsession (). CreateQuery (HQL); return q.list ();}  @Overridepublic list<t> Find (String hql, int page, int rows) {Query q = getcurrentsession (). CreateQuery (HQL); return Q.setfirstresult ((page-1) * rows). setmaxresults (rows). List ();} @Overridepublic Long count (String hql) {//query q = getcurrentsession (). CreateQuery (HQL).; /return (Long) q.uniqueresult (); return (long) getcurrentsession (). CreateQuery (HQL). List (). get (0);} @Overridepublic int executehql (String hql) {Query q = getcurrentsession (). CreateQuery (HQL); return q.executeupdate ();} @SuppressWarnings ("unchecked") @Overridepublic list<map> findbysql (String sql) {sqlquery q = getcurrentsession () . createsqlquery (SQL); return Q.setresulttransformer (transformers.alias_to_entity_map). List ();} @Overridepublic list<map> findbysql (String sql, int page, int rows) {sqlquery q = getcurrentsession (). Createsqlquer Y (SQL); return Q.setfirstresult ((page-1) * rows). setmaxresults (rows). Setresulttransformer (transformers.alias_to_ ENTITY_MAP). List ();} @Overridepublic int ExecuteSQL (String sql) {sqlquery q = getcurrentsession (). createsqlquery (SQL); return Q.executeupdate ();} @Overridepublic BigInteger countbysql (String sql) {sqlquery q = getcurrentsession (). createsqlquery (SQL); return ( BigInteger) Q.uniqueresult ();}}


Second page:


Based on the UserID lookup, here is the fuzzy query:



Edit user (the dialog box that pops up will get the selected data):


After the change:







Springmvc+easyui paging, querying (full crud)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.