1. 程式人生 > >基於SSM的健身房管理系統

基於SSM的健身房管理系統

# 基於SSM的健身房管理系統 > The project was made in 2020-05-05~2020-05-10 > > 謹以此片博文記錄下我的第一個Java小Demo > 程式碼請見[GitHub](https://github.com/CPG123456/GymMS) ## 專案展示 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223056729-1025028746.png) > **使用者登入頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223105878-2066053629.png) > **使用者註冊頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223115078-1710400359.png) > **使用者主頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223123422-8795051.png) > **使用者購買健身卡**(商城)**頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223132037-664136393.png) > **管理員登入頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223138826-2034480811.png) > **管理員主頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223149527-465864464.png) >**管理員新增使用者頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223242033-123391326.png) >**管理員編輯使用者頁** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223251566-250540578.png) >**細節1:常駐工具欄(內含搜尋欄)** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223259006-331494198.png) >**細節2:登入賬號密碼實時反饋(AJAX)** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223308617-203187542.png) >**細節3:翻頁功能&可選每頁顯示條數 ** ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223313817-1851190412.png) >**細節4:人性化的互動設計** ## 專案環境 - JDK: 1.8 - IDE: IDEA 201903 - DataBase: MySQL 8.0 - Mybatis: 3.5.2 - POM: Maven - Tomcat 9 - Bootstrap 3 - JQuery 2 - lombok 外掛 - 分頁外掛: PageHelper 5.1.11 - 涉及技術 MySQL資料庫,Spring,JavaWeb及MyBatis,簡單的前端知識 ## 專案詳情 ### 設計資料庫 > 大二沒有好好學系統設計與分析,畫的圖一塌糊塗。 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223327088-1597991601.png) > 包含兩個實體類,使用者&管理員 > > 生成該資料庫的sql檔案 在該[GitHub](https://github.com/CPG123456/GymMS)跳轉GitHub /sql 目錄下 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223339885-123568355.png) > 生成表如圖示 ### 設計要實現的功能 > 應該叫做用例圖吧,但我的好多符號都是錯的,再次後悔沒有好好學習! ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223346220-866693620.png) ### 製作前端頁面Demo ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223352024-1606258504.png) > 設計該圖的[網址](www.processon.com) 其實這一步放在後面也合適,我為了讓自己吃大餅,就先設計了一下。 ### 環境配置 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223407489-1370411315.png) > 專案檔案結構如圖所示 #### 基本步驟 1. 新建Maven專案,新增web支援 2. 匯入pom依賴 ```xml junit
junit 4.12
mysql mysql-connector-java 5.1.47 com.mchange c3p0 0.9.5.2 javax.servlet servlet-api 2.5 javax.servlet.jsp
jsp-api 2.2
javax.servlet jstl 1.2 org.mybatis mybatis 3.5.2 org.mybatis mybatis-spring 2.0.2 org.springframework
spring-webmvc 5.1.9.RELEASE
org.springframework spring-jdbc 5.1.9.RELEASE
``` 3. Maven資源過濾 ```xml src/main/java **/*.properties **/*.xml false src/main/resources **/*.properties **/*.xml false ``` 4. 建立基本結構框架 (如上所示[點此跳轉](# 環境配置) 5. 建立基本配置 - mybatis-config.xml ```xml ``` - applicationContext.xml ```xml ``` 6. Mybatis層編寫 - 資料庫配置檔案 **database.properties** ```properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456IDEA關聯資料庫 ``` - IDEA關聯資料庫 - 編寫MyBatis的核心配置檔案 ```xml ``` ### POJO Mapper Services的編寫 #### POJO 1. User ```java @Data //使用lombok外掛即可省寫get set @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String username; private String password; private String name; private String sex; private String telephone; private int age; private String begintime; private String endtime; private int remainday; private int addday; public static void main(String[] args) { User u = new User(); u.getTelephone(); } } ``` 2. Admin ```java @Data @NoArgsConstructor @AllArgsConstructor public class Admin { private int id; private String username; private String password; private String name; private String telephone; } ``` #### Mapper 1. 編寫DAO層的Mapper介面 1. 1 userMapper ```java public interface UserMapper { //通過id查詢 User queryUserById(int id); // 查詢到期時間 Date queryDateById(int id); //修改賬號密碼 int updateNP(User user); //login User userLogin(User user); //會員註冊 int userRegister(User user); } ``` 1. 2 adminMapper ```java @Mapper @Component public interface AdminMapper{ //查詢會員 List queryUser(); //新增會員 int addUser(User user); //刪除會員 int deleteUserById(int id); //更新會員 int updateUser(User user); //根據id查詢一個會員 User queryUserById(int id); //根據姓名查詢一個會員 User queryUserByName(String name); //login Admin adminLogin(Admin admin); //查詢使用者總數 int selectTotal(); // 分頁 @Select("select * from gymms.user") @Results({ // 使用者資訊,只要指定id列與屬性的對映關係,其他列會自動封裝(屬性與列一致) @Result(property = "id", column = "id"), // 資訊 @Result(property = "id", column = "id"), @Result(property = "username", column = "username"), @Result(property = "password", column = "password"), @Result(property = "name", column = "name"), @Result(property = "sex", column = "sex"), @Result(property = "telephone", column = "telephone"), @Result(property = "age", column = "age"), @Result(property = "begintime", column = "begintime"), @Result(property = "endtime", column = "endtime"), @Result(property = "remainday", column = "remainday") }) List findAll(); } ``` 2. 編寫介面對應的 Mapper.xml 檔案 2. 1 userMapper.xml ```xml update gymms.user set username = #{username},password = #{password} where id = #{id} insert into gymms.user(username,password,name,sex,telephone,age,begintime,endtime,remainday) values (#{username},#{password},#{name},#{sex},#{telephone},#{age},#{begintime},#{endtime},#{remainday}) ``` 2. 2 adminMapper.xml ```xml insert into gymms.user(username,password,name,sex,telephone,age,begintime,endtime,remainday) values (#{username},#{password},#{name},#{sex},#{telephone},#{age},#{begintime},#{endtime},#{remainday}) delete from gymms.user where id=#{id} update gymms.user set username = #{username},password = #{password},name = #{name},sex = #{sex},telephone = #{telephone}, age = #{age},endtime = #{endtime},remainday = #{remainday} where id = #{id} ``` 3. 編寫Service層的介面和實現類 3. 1 userService ```java public interface UserService { // 查詢到期時間 Date queryDateById(int id); //修改賬號密碼 int updateNP(User user); //login User userLogin(User user); //通過id查詢 User queryUserById(int id); //會員註冊 int userRegister(User user); } ``` 3. 2 adminService ```java public interface AdminService { //查詢會員 List queryUser(); //新增會員 int addUser(User user); //刪除會員 int deleteUserById(int id); //更新會員 int updateUser(User user); //根據id查詢一個會員 User queryUserById(int id); //根據姓名查詢一個會員 User queryUserByName(String name); //login Admin adminLogin(Admin admin); //查詢使用者總數 int selectTotal(); // 分頁 List findAll(); /** * 分頁查詢 * @param pageNum 當然頁 * @param pageSize 頁大小 * @return 返回PageHelper提供的封裝分頁引數的PageInfo物件 */ PageInfo findByPage(int pageNum, int pageSize); } ``` 3. 3 userServiceImpl ```java public class UserServiceImpl implements UserService { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public Date queryDateById(int id) { return userMapper.queryDateById(id); } public int updateNP(User user) { return userMapper.updateNP(user); } public User userLogin(User user) { return userMapper.userLogin(user); } public User queryUserById(int id) { return userMapper.queryUserById(id); } public int userRegister(User user) { return userMapper.userRegister(user); } } ``` 3. 4 adminServiceImpl ```java public class AdminServiceImpl implements AdminService { private AdminMapper adminMapper; public void setAdminMapper(AdminMapper adminMapper) { this.adminMapper = adminMapper; } public List queryUser() { return adminMapper.queryUser(); } public int addUser(User user) { return adminMapper.addUser(user); } public int deleteUserById(int id) { return adminMapper.deleteUserById(id); } public int updateUser(User user) { return adminMapper.updateUser(user); } public User queryUserById(int id) { return adminMapper.queryUserById(id); } public User queryUserByName(String name) { return adminMapper.queryUserByName(name); } public Admin adminLogin(Admin admin) { return adminMapper.adminLogin(admin); } public int selectTotal() { return adminMapper.selectTotal(); } public List findAll() { return adminMapper.findAll(); } public PageInfo findByPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List list = adminMapper.findAll(); PageInfo pageInfo = new PageInfo<>(list); return pageInfo; } } ``` #### Spring層 - 配置**Spring整合MyBatis**,我們這裡資料來源使用c3p0連線池; - 編寫Spring整合Mybatis的相關的配置檔案; spring-dao.xml ```xml ``` - **Spring整合service層** ```xml ``` #### SpringMVC層 - **web.xml** ```xml DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 DispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* 15 ``` - **spring-mvc.xml** ```xml ``` - **Spring配置整合檔案,applicationContext.xml** ```xml ``` #### Controller&檢視層 - UserController ```java package com.cc.controller; import com.cc.pojo.User; import com.cc.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @Controller @RequestMapping("/user") public class UserController { @Autowired @Qualifier("UserServiceImpl") private UserService userService; @RequestMapping("/toLogin") public String toLoginUser(Model model, HttpServletRequest request ) { try { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+" "+password); User user =new User(); user.setUsername(username); user.setPassword(password); User users = userService.userLogin(user); if (users.getId() > 0) { // model.addAttribute("user", users); model.addAttribute("user", users); return "userMain"; } } catch (Exception e) { model.addAttribute("error","Wrong username or password!"); return "redirect:/"; } return "redirect:/"; } @ResponseBody @RequestMapping("/toLogintest") public String userLogin(String name,String pwd) { String msg = "Wrong username or password!"; //模擬資料庫中存在資料 if ((name != null) && (pwd != null)) { // 從資料庫檢索是否存在該使用者 User user = new User(); user.setUsername(name); user.setPassword(pwd); try { User users = userService.userLogin(user); if (users.getUsername().equals(name) && users.getPassword().equals(pwd)) { msg = "Login Success"; } return msg; }catch (Exception e){} return msg; //g } return msg; } @RequestMapping("/userBuy") public String userBuyCard(Model model,int id) throws ParseException { User user = userService.queryUserById(id); System.out.println(user); model.addAttribute("user",user ); return "userMall"; } @RequestMapping("/userMianPage") public String userMianPage(Model model,int id) throws ParseException { User user = userService.queryUserById(id); System.out.println(user); model.addAttribute("user",user ); return "userMain"; } // 跳轉註冊頁面 @RequestMapping("/toUserRegister") public String toAddPaper() { return "register"; } //註冊 @RequestMapping("/userRegister") public String registerUser(User user) throws ParseException { SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String date = sp.format(new Date()); user.setBegintime(date); user.setEndtime(date); user.setRemainday(0); userService.userRegister(user); return "redirect:/user/toLogin"; } } ``` - AdminController ```java package com.cc.controller; import com.cc.pojo.Admin; import com.cc.pojo.User; import com.cc.service.AdminService; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; 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.RequestParam; import org.springframework.web.servlet.ModelAndView; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; @Controller @RequestMapping("/admin") public class AdminController { @Autowired @Qualifier("AdminServiceImpl") private AdminService adminService; @RequestMapping("/toAddUser") public String toAddPaper() { return "register"; } @RequestMapping("/addUser") public String addPaper(User user) throws ParseException { System.out.println(user); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式 Date date = dateFormat.parse(user.getBegintime()); // 指定日期 Calendar cl = Calendar.getInstance(); cl.setTime(date); cl.add(Calendar.DATE, user.getRemainday()); String temp = ""; temp = dateFormat.format(cl.getTime()); user.setEndtime(temp); adminService.addUser(user); return "redirect:/admin/findByPage"; } @RequestMapping("/toUpdateUser") public String toUpdateUser(Model model, int id) { User user = adminService.queryUserById(id); System.out.println(user); model.addAttribute("user",user ); return "updateUser"; } @RequestMapping("/updateUser") public String updateUser(Model model, User user) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式 Date date = dateFormat.parse(user.getBegintime()); // 指定日期 Calendar cl = Calendar.getInstance(); cl.setTime(date); int allday = user.getRemainday()+user.getAddday(); cl.add(Calendar.DATE, allday); String temp = ""; temp = dateFormat.format(cl.getTime()); user.setEndtime(temp); user.setRemainday(allday); adminService.updateUser(user); User users = adminService.queryUserById(user.getId()); model.addAttribute("user", users); return "redirect:/admin/findByPage"; } @RequestMapping("/del/{userId}") public String deleteBook(@PathVariable("userId") int id) { adminService.deleteUserById(id); return "redirect:/admin/findByPage"; } @RequestMapping("/toaLogin") public String toaLogin() { return "/aLogin"; } @RequestMapping("/aLogin") public String toLoginUser(Admin admin) { System.out.println(admin.getUsername()+admin.getPassword()); try { Admin admins = adminService.adminLogin(admin); if (admins.getId() > 0) { // model.addAttribute("user", users); return "redirect:/admin/findByPage"; } }catch (Exception e){ return "/aLogin"; } return "/aLogin"; } @RequestMapping("/findAll") public ModelAndView findAll() { //1.1 呼叫service List list = adminService.findAll(); //1.2 返回結果 ModelAndView mv = new ModelAndView(); mv.setViewName("order-list"); mv.addObject("list", list); return mv; } @RequestMapping("/findByPage") public String findByPage(Model model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "11") int pageSize) { PageInfo pageInfo = adminService.findByPage(pageNum, pageSize); // PageInfo pageInfo = adminService.findByPage(pageNum, pageSize); //1.1 呼叫service //1.2 返回結果 model.addAttribute("list", pageInfo.getList()); model.addAttribute("pageInfo", pageInfo); return "allUserPage"; } @RequestMapping("/searchUser") public String searchUser(Model model, String name) { try { User user = adminService.queryUserByName(name); System.out.println(user); model.addAttribute("user", user); if (user.getName() == null) { return "redirect:/admin/findByPage"; } return "updateUser"; }catch (Exception e){ return "redirect:/admin/findByPage"; } } // 商城 @RequestMapping("/buy") public String buyCard() throws ParseException { return "mall"; } } ``` ##### 檢視層 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510225726202-674712080.png) > 詳情見[GitHub](https://github.com/CPG123456/GymMS) ### 拓展與展望 以下功能在以後可拓展開發 ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223506173-1926830963.png) ![](https://img2020.cnblogs.com/blog/1593734/202005/1593734-20200510223514318-559773983.png) ## 結束與總結 這個小Demo做了五六天,分頁那裡卡了接近兩天,無數的坑等著踩。 期間也體會到了程式設計的樂趣,每天晚上都肝的快快樂樂。 所有程式碼放在GitHub供大家交流學習。 今天母親節呀,祝我媽媽母親節快樂!