使用Gradle編譯項目 傳送門
項目已托管到Github上 傳送門
SpringBoot使用MySQL實現
實現功能:普通用戶注冊、普通用戶登錄、管理員通過edit-user頁面和show-all-user頁面對用戶信息的修改

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="css/login.css" /> <script src="js/jquery.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <script> $(document).ready(function() { //打開會員登錄 $("#Login_start_").click(function() { $("#regist_container").hide(); $("#_close").show(); $("#_start").animate({ left : '350px', height : '520px', width : '400px' }, 500, function() { $("#login_container").show(500); $("#_close").animate({ height : '40px', width : '40px' }, 500); }); }); //打開會員注冊 $("#Regist_start_").click(function() { $("#login_container").hide(); $("#_close").show(); $("#_start").animate({ left : '350px', height : '520px', width : '400px' }, 500, function() { $("#regist_container").show(500); $("#_close").animate({ height : '40px', width : '40px' }, 500); }); }); //關閉 $("#_close").click(function() { $("#_close").animate({ height : '0px', width : '0px' }, 500, function() { $("#_close").hide(500); $("#login_container").hide(500); $("#regist_container").hide(500); $("#_start").animate({ left : '0px', height : '0px', width : '0px' }, 500); }); }); //去 注冊 $("#toRegist").click(function() { $("#login_container").hide(500); $("#regist_container").show(500); }); //去 登錄 $("#toLogin").click(function() { $("#regist_container").hide(500); $("#login_container").show(500); }); }); </script> </head> <body style="background-color: #DCDCDC;"> <a id="Login_start_" class="btn btn-danger" style="width: 100px; height: 40px; border-radius: 0;">登陸</a> <a id="Regist_start_" class="btn btn-success" style="width: 100px; height: 40px; border-radius: 0;">注冊</a> <!-- 會員登錄 --> <!--<div id='Login_start' style="position: absolute;" >--> <div id='_start'> <div id='_close' style="display: none;"> <span class="glyphicon glyphicon-remove"></span> </div> <br /> <!--登錄框--> <div id="login_container"> <div id="lab1"> <span id="lab_login">會員登錄</span> <span id="lab_toRegist">  還沒有賬號 <span id='toRegist' style="color: #EB9316; cursor: pointer;">立即注冊</span> </span> </div> <div style="width: 330px;"> <span id="lab_type1">手機號/賬號登陸</span> </div> <div id="form_container1"> <br /> <form action="" th:action="@{~/login.action}"> <input type="text" class="form-control" placeholder="手機號/用戶名" id="login_number" value="Gary" name="username"/> <input type="password" class="form-control" placeholder="密碼" id="login_password" name="password" /> <input type="submit" value="登錄" class="btn btn-success" id="login_btn" /> </form> <span id="rememberOrfindPwd"> <span> <input id="remember" type="checkbox" style="margin-bottom: -1.5px;" /> </span> <span style="color: #000000"> 記住密碼     </span> <span style="color: #000000"> 忘記密碼 </span> </span> </div> <div style="display: block; width: 330px;"> <span id="lab_type2">使用第三方直接登陸</span> </div> <div style="width: 330px; height: 100px; border-bottom: 1px solid #FFFFFF;"> <br /> <button id="login_QQ" type="button" class="btn btn-info"> <img src="img/qq32.png" style="width: 20px; margin-top: -4px;" />  QQ登錄 </button> <button id="login_WB" type="button" class="btn btn-danger"> <img src="img/sina32.png" style="width: 20px; margin-top: -4px;" />  微博登錄 </button> </div> </div> <!-- 會員注冊 --> <div id='regist_container' style="display: none;"> <div id="lab1"> <span id="lab_login">會員注冊</span> <span id="lab_toLogin">  已有賬號 <span id='toLogin' style="color: #EB9316; cursor: pointer;">立即登錄</span> </span> </div> <form action="" th:action="@{~/register.action}"> <div id="form_container2" style="padding-top: 25px;"> <input type="text" class="form-control" value="Gary" placeholder="用戶名" id="regist_account" name="username" /> <input type="password" class="form-control" placeholder="密碼" id="regist_password1" name="password" /> <input type="password" class="form-control" placeholder="確認密碼" id="regist_password2" /> <input type="text" class="form-control" placeholder="手機號" id="regist_phone" name="telephone" /> <input type="text" class="form-control" placeholder="驗證碼" id="regist_vcode" /> <!--<button id="getVCode" type="button" class="btn btn-success" >獲取驗證碼</button>--> <input id="getVCode" type="button" class="btn btn-success" value="點擊發送驗證碼" onclick="sendCode(this)" /> </div> <input type="submit" value="注冊" class="btn btn-success" id="regist_btn" /> </form> </div> </div> </body> <script type="text/javascript"> var clock = ''; var nums = 30; var btn; function sendCode(thisBtn) { btn = thisBtn; btn.disabled = true; //將按鈕置為不可點擊 btn.value = '重新獲取(' + nums + ')'; clock = setInterval(doLoop, 1000); //一秒執行一次 } function doLoop() { nums--; if (nums > 0) { btn.value = '重新獲取(' + nums + ')'; } else { clearInterval(clock); //清除js定時器 btn.disabled = false; btn.value = '點擊發送驗證碼'; nums = 10; //重置時間 } } $(document).ready(function() { $("#login_QQ").click(function() { alert("暫停使用!"); }); $("#login_WB").click(function() { alert("暫停使用!"); }); }); </script> </html>

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Hello Gary!</h1> </body> </html>

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="" th:action="@{~/edit.action}" method="post"> <input name="id" type="hidden" th:value="${user.id}"> 用戶名: <input name="username" type="text" th:value="${user.username}"> 密碼: <input name="password" type="text" th:value="${user.password}"> 電話: <input name="telephone" type="text" th:value="${user.telephone}"> <input type="submit" value="提交"> </form> </body> </html>

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table border="1px"> <tr> <th>用戶名</th> <th>密碼</th> <th>電話</th> <th>編輯</th> </tr> <tr th:each="user : ${userList}"> <th th:text="${user.username}">132</th> <th th:text="${user.password}">123</th> <th th:text="${user.telephone}">123</th> <th> <a th:href="@{'~/toEdit/'+${user.id}}">修改</a> </th> </tr> </table> </body> </html>

#Thymeleaf的編碼 spring.thymeleaf.encoding=UTF-8 #熱部署靜態文件 spring.thymeleaf.cache=false #使用HTML5的標准 spring.thymeleaf.mode=HTML5 #使用h2的控制台 spring.h2.console.enable=true #DataSource spring.datasource.url=jdbc:mysql:///garyuser spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #配置JPA spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update

package com.Gary.GaryuserDemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class GaryuserDemoApplication { public static void main(String[] args) { SpringApplication.run(GaryuserDemoApplication.class, args); } }

package com.Gary.GaryuserDemo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.Gary.GaryuserDemo.domain.User; import com.Gary.GaryuserDemo.service.UserServiceImpl; @RestController public class UserController { @Autowired private UserServiceImpl userServiceImpl; @RequestMapping("/") public ModelAndView index() { return new ModelAndView("/index.html"); } @RequestMapping("/register.action") public ModelAndView register(User user) { userServiceImpl.save(user); return new ModelAndView("redirect:/"); } @RequestMapping("/login.action") public ModelAndView login(User user) { boolean success = userServiceImpl.login(user); if(success) { return new ModelAndView("/welcome.html"); }else { return new ModelAndView("redirect:/"); } } @RequestMapping("/show.action") public ModelAndView show(Model model) { List<User> userList = userServiceImpl.findAll(); //把userList放入到模型中 model.addAttribute("userList",userList); return new ModelAndView("/show-all-user.html","userModel",model); } @RequestMapping("/toEdit/{id}") public ModelAndView toEdit(@PathVariable("id")Long id,Model model) { //根據ID得到用戶 User user = userServiceImpl.findById(id); //將用戶放入到model中 model.addAttribute("user",user); return new ModelAndView("/edit-user.html","userModel",model); } @RequestMapping("/edit.action") public ModelAndView Edit(User user) { userServiceImpl.save(user); return new ModelAndView("redirect:/show.action"); } }

package com.Gary.GaryuserDemo.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String username; private String password; private String telephone; protected User() { } public User(Long id,String username,String password,String telephone) { this.id=id; this.username=username; this.password=password; this.telephone=telephone; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }

package com.Gary.GaryuserDemo.repository; import org.springframework.data.repository.CrudRepository; import com.Gary.GaryuserDemo.domain.User; public interface UserRepository extends CrudRepository<User,Long>{ User findByUsernameAndPassword(String username, String password); }

package com.Gary.GaryuserDemo.service; import java.util.List; import com.Gary.GaryuserDemo.domain.User; public interface UserService { void save(User user); boolean login(User user); List<User> findAll(); User findById(Long id); }

package com.Gary.GaryuserDemo.service; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.Gary.GaryuserDemo.domain.User; import com.Gary.GaryuserDemo.repository.UserRepository; @Service public class UserServiceImpl implements UserService{ @Autowired private UserRepository userRepository; @Override public void save(User user) { userRepository.save(user); } @Override public boolean login(User user) { User loginUser = userRepository.findByUsernameAndPassword(user.getUsername(),user.getPassword()); return loginUser==null?false:true; } @Override public List<User> findAll() { Iterable<User> list = userRepository.findAll(); return (List<User>)list; } @Override public User findById(Long id) { Optional<User> user = userRepository.findById(id); return user.get(); } }
目錄結構
各個功能模塊的默認配置文件application.properties
#Thymeleaf的編碼 spring.thymeleaf.encoding=UTF-8 #熱部署靜態文件 spring.thymeleaf.cache=false #使用HTML5的標准 spring.thymeleaf.mode=HTML5 #使用h2的控制台 spring.h2.console.enable=true #DataSource spring.datasource.url=jdbc:mysql:///garyuser spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #配置JPA spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
實現過程分五步
2、用戶登錄功能
3、管理員查詢用戶界面功能
4、用戶信息回顯功能
5、管理員修改用戶信息功能
controller控制層
service業務邏輯層
repository持久層
User實體 設置主鍵ID自增
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String username; private String password; private String telephone;

package com.Gary.GaryuserDemo.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String username; private String password; private String telephone; protected User() { } public User(Long id,String username,String password,String telephone) { this.id=id; this.username=username; this.password=password; this.telephone=telephone; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
controller層獲接收用戶和管理員請求並做相應的回應
//訪問首頁面 @RequestMapping("/") public ModelAndView index() //用戶注冊 @RequestMapping("/register.action") public ModelAndView register(User user) //用戶登錄 @RequestMapping("/login.action") public ModelAndView login(User user) //展示用戶信息 @RequestMapping("/show.action") public ModelAndView show(Model model) //管理員修改用戶的信息 @RequestMapping("/toEdit/{id}") public ModelAndView toEdit(@PathVariable("id")Long id,Model model) //將修改的用戶信息重新存儲到數據庫中 @RequestMapping("/edit.action") public ModelAndView Edit(User user)
1、用戶注冊功能
注冊表單:用戶點擊注冊后發送請求到"@{~/register.action}"
<form action="" th:action="@{~/register.action}"> <div id="form_container2" style="padding-top: 25px;"> <input type="text" class="form-control" value="Gary" placeholder="用戶名" id="regist_account" name="username" /> <input type="password" class="form-control" placeholder="密碼" id="regist_password1" name="password" /> <input type="password" class="form-control" placeholder="確認密碼" id="regist_password2" /> <input type="text" class="form-control" placeholder="手機號" id="regist_phone" name="telephone" /> <input type="text" class="form-control" placeholder="驗證碼" id="regist_vcode" /> <!--<button id="getVCode" type="button" class="btn btn-success" >獲取驗證碼</button>--> <input id="getVCode" type="button" class="btn btn-success" value="點擊發送驗證碼" onclick="sendCode(this)" /> </div> <input type="submit" value="注冊" class="btn btn-success" id="regist_btn" /> </form>
用戶提交表單后,請求提交到controller層去接收"@{~/register.action}"請求
@RequestMapping("/register.action") public ModelAndView register(User user) { userServiceImpl.save(user); return new ModelAndView("redirect:/"); }
controller層接收到請求后由service去調用save()方法
@Autowired private UserRepository userRepository; @Override public void save(User user) { userRepository.save(user); }
service層去調用repository層save()方法
save()方法由CrudRepository()接口自動封裝好(本身提供增刪改查功能)
2、用戶登錄功能
登錄表單:用戶點擊登錄后發送請求到"@{~/register.action}"
<form action="" th:action="@{~/register.action}"> <div id="form_container2" style="padding-top: 25px;"> <input type="text" class="form-control" value="Gary" placeholder="用戶名" id="regist_account" name="username" /> <input type="password" class="form-control" placeholder="密碼" id="regist_password1" name="password" /> <input type="password" class="form-control" placeholder="確認密碼" id="regist_password2" /> <input type="text" class="form-control" placeholder="手機號" id="regist_phone" name="telephone" /> <input type="text" class="form-control" placeholder="驗證碼" id="regist_vcode" /> <!--<button id="getVCode" type="button" class="btn btn-success" >獲取驗證碼</button>--> <input id="getVCode" type="button" class="btn btn-success" value="點擊發送驗證碼" onclick="sendCode(this)" /> </div> <input type="submit" value="注冊" class="btn btn-success" id="regist_btn" /> </form>
Controller層處理用戶的登錄邏輯,判斷用戶輸入賬號密碼是否正確,輸入正確則跳轉到welcome.html頁面,否則重定向到index.html頁面
@RequestMapping("/login.action") public ModelAndView login(User user) { boolean success = userServiceImpl.login(user); if(success) { return new ModelAndView("/welcome.html"); }else { return new ModelAndView("redirect:/"); } }
controller層接收到請求后由service去調用login()方法
@Override public boolean login(User user) { User loginUser = userRepository.findByUsernameAndPassword(user.getUsername(),user.getPassword()); return loginUser==null?false:true; }
service層去調用repository層findByUsernameAndPassword()方法,findByUsernameAndPassword()方法繼承CrudRepository()接口
ublic interface UserRepository extends CrudRepository<User,Long>{ User findByUsernameAndPassword(String username, String password); }
3、管理員查詢用戶界面功能
管理員想要查看數據庫中的信息得去發送show.action請求,userList得到數據庫中用戶的信息后跳轉至show-all-user.html頁面
@RequestMapping("/show.action") public ModelAndView show(Model model) { List<User> userList = userServiceImpl.findAll(); //把userList放入到模型中 model.addAttribute("userList",userList); return new ModelAndView("/show-all-user.html","userModel",model); }
<table border="1px"> <tr> <th>用戶名</th> <th>密碼</th> <th>電話</th> <th>編輯</th> </tr> <tr th:each="user : ${userList}"> <th th:text="${user.username}">132</th> <th th:text="${user.password}">123</th> <th th:text="${user.telephone}">123</th> <th> <a th:href="@{'~/toEdit/'+${user.id}}">修改</a> </th> </tr>
4、用戶信息回顯功能
controller層接收管理員toEdit請求
@RequestMapping("/toEdit/{id}") public ModelAndView toEdit(@PathVariable("id")Long id,Model model) { //根據ID得到用戶 User user = userServiceImpl.findById(id); //將用戶放入到model中 model.addAttribute("user",user); return new ModelAndView("/edit-user.html","userModel",model); }
Optional 用來作為用戶存儲的容器 傳送門
@Override public List<User> findAll() { Iterable<User> list = userRepository.findAll(); return (List<User>)list; } @Override public User findById(Long id) { Optional<User> user = userRepository.findById(id); return user.get(); }
5、管理員修改用戶信息功能
controller層去修改用戶信息並重新刷新數據庫中User信息
@RequestMapping("/edit.action") public ModelAndView Edit(User user) { userServiceImpl.save(user); return new ModelAndView("redirect:/show.action"); }
edit-user.html去修改用戶頁面信息
<form action="" th:action="@{~/edit.action}" method="post"> <input name="id" type="hidden" th:value="${user.id}"> 用戶名: <input name="username" type="text" th:value="${user.username}"> 密碼: <input name="password" type="text" th:value="${user.password}"> 電話: <input name="telephone" type="text" th:value="${user.telephone}"> <input type="submit" value="提交"> </form>