1. Preparation
http://start.spring.io/here address to directly generate the project information you need, how you IDE and integrated springboot so you can generate project information directly
Required Knowledge: Java,spring,springmvc,thymelafe
2 start
Based on the picture project structure information, we can infer that we need to write the following classes:
1 PackageCom.dgw.boot.dgw.domain;2 3 /**4 * @authordgw-pc5 * @data July 17, 20186 */7 Public classuser{8 9 PrivateLong ID;Ten PrivateString name; One PrivateString Email; A - PublicUser () { - the } - - PublicUser (Long ID, string name, string email) { - This. ID =ID; + This. Name =name; - This. email =email; + } A at @Override - PublicString toString () { - return"User [id=" + ID + ", name=" + name + ", email=" + email + "]"; - } - - PublicLong getId () { in returnID; - } to + Public voidsetId (Long id) { - This. ID =ID; the } * $ PublicString GetName () {Panax Notoginseng returnname; - } the + Public voidsetName (String name) { A This. Name =name; the } + - PublicString Getemail () { $ returnemail; $ } - - Public voidsetemail (String email) { the This. email =email; - }Wuyi}
Dao Beans
Public Interfaceuserrepository {User savaorupdateuser (user user); voidDeleteUser (LongID); User Getuserbyid (LongID); List<User>listusre (); } @Repository Public classUserrepositoryimplImplementsUserrepository {Private StaticAtomiclong count=NewAtomiclong (); PrivateConcurrentmap<long, user> usemap=NewConcurrenthashmap<>(); @Override Publicuser savaorupdateuser (user user) {Long ID=User.getid (); if(id==NULL) {ID=Count.incrementandget (); User.setid (ID); } This. Usemap.put (ID, user); returnuser; } @Override Public voidDeleteUser (LongID) { This. Usemap.remove (ID); } @Override PublicUser Getuserbyid (LongID) {return This. Usemap.get (ID); } @Override PublicList<user>listusre () {return NewArraylist<> ( This. Usemap.values ()); }}
Controller:
1 /**2 * Get a list of users from the user repository3 * @return4 */5 PrivateList<user>getuserlist () {6 returnUserrepository.listuser ();7 }8 9 /**Ten * Search for users One * @return A */ - @GetMapping - PublicModelandview list (model model) { theModel.addattribute ("UserList", Getuserlist ()); -Model.addattribute ("title", "User Management"); - return NewModelandview ("Users/list", "Usermodel", model); - } + - /** + * Query users by ID A * @parammessage at * @return - */ -@GetMapping ("{ID}") - PublicModelandview View (@PathVariable ("id"Long ID, model model) { -User User =Userrepository.getuserbyid (ID); -Model.addattribute ("User", user); inModel.addattribute ("title", "View User"); - return NewModelandview ("Users/view", "Usermodel", model); to } + - /** the * Get Form form page * * @paramUser $ * @returnPanax Notoginseng */ -@GetMapping ("/form") the PublicModelandview CreateForm (model model) { +Model.addattribute ("User",NewUser ()); AModel.addattribute ("title", "Create user"); the return NewModelandview ("Users/form", "Usermodel", model); + } - $ /** $ * New User - * @paramUser - * @paramresult the * @paramredirect - * @returnWuyi */ the @PostMapping - PublicModelandview Create (user user) { Wuuser =userrepository.saveorupateuser (user); - return NewModelandview ("Redirect:/users"); About } $ - /** - * Delete User - * @paramID A * @return + */ the@GetMapping (value = "Delete/{id}") - PublicModelandview Delete (@PathVariable ("id"Long ID, model model) { $ Userrepository.deleteuser (ID); the theModel.addattribute ("UserList", Getuserlist ()); theModel.addattribute ("title", "Delete user"); the return NewModelandview ("Users/list", "Usermodel", model); - } in the /** the * Modify User About * @paramUser the * @return the */ the@GetMapping (value = "Modify/{id}") + PublicModelandview Modifyform (@PathVariable ("id"Long ID, model model) { -User User =Userrepository.getuserbyid (ID); the BayiModel.addattribute ("User", user); theModel.addattribute ("title", "Modify user"); the return NewModelandview ("Users/form", "Usermodel", model); -}
Springboot User Crud