Source: blog csdn. net/weixin_ 44671737/article/details/112341805
abstract
For enterprises, people, materials, energy, capital and information are five important resources. People, materials, energy and capital are visible tangible resources, while information is an intangible resource. In the past, people paid more attention to tangible resources. After entering the era of information society and knowledge economy, information resources become increasingly important.
Because information resources determine how to make more effective use of material resources. Information resources are the crystallization of knowledge obtained in the struggle between human beings and nature. If we master information resources, we can make better use of tangible resources and make tangible resources give better benefits.
It can be seen that all kinds of management systems are still valuable. The companies I have experienced have their own management systems. Maybe some non internet companies rely on the information management services provided by other companies. The ERP system still plays an important role. The writer hurried after work and finally completed the xx management system after several hours of fierce competition, Here to share with you.
1. Technical selection
1.1 Mysql8
Why mysql8, not version 5. Yes, it's like the new and hate the old!!!
What's special about mysql8!
1. The performance is good. Yes, it is strong, so of course, go to Mysql8 and go to Mysql8 quickly.
2.Mysql8 also supports various awesome functions: SQL window functions, GIS, JSON extension syntax, etc. In short, these functions sound very good. Since the author hasn't figured it out, I won't repeat them.
1.2 springboot2
The current mainstream web development framework is the best choice. There is no more convenient than this. Why choose this? It is invincible and has no competitors. Automatic assembly, parent dependency, embedded tomcat, etc. are invincible.
I won't introduce the basics of Spring Boot. I recommend this practical tutorial:
https://github.com/javastacks...
1.3 template engine thymeleaf
It's helpless to use the template engine. The author won't use the front end. After all, people's energy is limited. However, this thing is still very useful. It is similar to learning to write jsp a few years ago.
2 project construction
2.1 create project
spring initializer
Name your favorite project
Check the required dependencies, mysql, thymeleaf, jpa
2.2 table building
Well, in order to be lazy, the author didn't build a table ddl. Instead, he went directly to jpa and built a required library, create database. After that, the preparatory work for the start of the project is finished, and the exciting code link is brought immediately.
3 project realization
3.1 project effect
Main page
That's it. In this way, the author has given full play to 200% of the front-end skills. All kinds of data access, copy, code modification, modification and modification finally have the upper appearance. It shouldn't be too ugly! Well, I admit it.
3.2 project structure
The structure is basically the same as that of mainstream web development projects:
The entity package of the corresponding database entity, the repo directory of the corresponding database operation, the service directory of the corresponding business code, and the controller directory of the corresponding restful api.
4 code implementation
4.1 front desk page
Home page is the handsome home page seen above!
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <meta charset="UTF-8"> <title>xx-manager</title> </head> <body> <header th:replace="xx-header.html"></header> <div class="container my-2"> <a th:href="@{/newEmployee}" class="btn btn-primary btn-sm mb-3"> newly added </a> <table border="1" class = "table table-striped table-responsive-md"> <thead> <tr> <th>full name</th> <th>mailbox</th> <th>Telephone</th> <th>address</th> <th>operation</th> </tr> </thead> <tbody> <tr th:each="employee : ${employees}"> <td th:text="${employee.name}"></td> <td th:text="${employee.email}"></td> <td th:text="${employee.mobile}"></td> <td th:text="${employee.location}"></td> <td><a th:href="@{/updateEmployee/{id}(id=${employee.id})}" class="btn btn-primary">modify</a> <a th:href="@{/deleteEmployee/{id}(id=${employee.id})}" class="btn btn-danger">delete</a></td> </tr> </tbody> </table> <div th:if="${totalPages > 1}"> <div class="row col-sm-10"> <div class="col-sm-2"> Total number: [[${items}]] </div> <div class="col-sm-1"> <span th:each="i: ${#numbers.sequence(1, totalPages)}"> <a th:if="${currentPage != i}" th:href="@{'/page/' + ${i}}">[[${i}]]</a> <span th:unless="${currentPage != i}">[[${i}]]</span> </span> </div> <div class="col-sm-1"> <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${currentPage + 1}}">next page</a> <span th:unless="${currentPage < totalPages}">next page</span> </div> <div class="col-sm-1"> <a th:if="${currentPage < totalPages}" th:href="@{'/page/' + ${totalPages}}">Last page</a> <span th:unless="${currentPage < totalPages}">Last page</span> </div> </div> </div> </div> <footer th:replace="footer.html"></footer> </body> </html>
New function page
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <meta charset="UTF-8"> <title>xx-manager</title> </head> <body> <header th:replace="xx-header.html"></header> <div class="container"> <h1>xx-manager</h1> <hr> <h2>preservation</h2> <form action="#" th:action="@{/saveEmployee}" th:object="${employee}" method="POST"> <input type="text" th:field="*{name}" placeholder="full name" class="form-control mb-4 col-4"> <input type="text" th:field="*{email}" placeholder="mailbox" class="form-control mb-4 col-4"> <input type="text" th:field="*{mobile}" placeholder="Telephone" class="form-control mb-4 col-4"> <input type="text" th:field="*{location}" placeholder="address" class="form-control mb-4 col-4"> <button type="submit" class="btn btn-info col-2">preservation</button> </form> <hr> <a th:href="@{/}">Back off</a> </div> <footer th:replace="footer.html"></footer> </body> </html>
Modify function page
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <meta charset="UTF-8"> <title>xx-manager</title> </head> <body> <header th:replace="xx-header.html"></header> <div class="container"> <h1>xx-manager</h1> <hr> <h2>to update</h2> <form action="#" th:action="@{/saveEmployee}" th:object="${employee}" method="POST"> <input type="hidden" th:field="*{id}" /> <input type="text" th:field="*{name}" class="form-control mb-4 col-4"> <input type="text" th:field="*{email}" class="form-control mb-4 col-4"> <input type="text" th:field="*{mobile}" class="form-control mb-4 col-4"> <input type="text" th:field="*{location}" class="form-control mb-4 col-4"> <button type="submit" class="btn btn-info col-2">modify</button> </form> <hr> <a th:href="@{/}">Back off</a> </div> <footer th:replace="footer.html"></footer> </body> </html>
4.2 background business realization
4.2.1 entity class Employee
Table in corresponding database
package com.lbh.xxmanager.entity; import javax.persistence.*; /** * Copyright(c)lbhbinhao@163.com * @author liubinhao * @date 2021/1/7 * ++++ ______ ______ ______ * +++/ /| / /| / /| * +/_____/ | /_____/ | /_____/ | * | | | | | | | | | * | | | | | |________| | | * | | | | | / | | | * | | | | |/___________| | | * | | |___________________ | |____________| | | * | | / / | | | | | | | * | |/ _________________/ / | | / | | / * |_________________________|/b |_____|/ |_____|/ */ @Entity @Table(name = "xx_employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column(name = "name") private String name; @Column(name = "email") private String email; @Column(name = "mobile") private String mobile; @Column(name = "location") private String location; @Column(name="status") private int status; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
4.2.2 database operation layer repo
package com.lbh.xxmanager.repo; import com.lbh.xxmanager.entity.Employee; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; /** * Copyright(c)lbhbinhao@163.com * @author liubinhao * @date 2021/1/7 * ++++ ______ ______ ______ * +++/ /| / /| / /| * +/_____/ | /_____/ | /_____/ | * | | | | | | | | | * | | | | | |________| | | * | | | | | / | | | * | | | | |/___________| | | * | | |___________________ | |____________| | | * | | / / | | | | | | | * | |/ _________________/ / | | / | | / * |_________________________|/b |_____|/ |_____|/ */ @Repository public interface EmployeeRepository extends JpaRepository<Employee,Long> { List<Employee> findAllByStatus(int status); Page<Employee> findAllByStatus(int status, Pageable pageable); Page<Employee> findAllByStatusAndLocationLikeOrNameLikeOrEmailLike(int status,String locaion,String name,String email, Pageable pageable); }
4.2.3 business code service
Interface:
package com.lbh.xxmanager.service; import com.lbh.xxmanager.entity.Employee; import org.springframework.data.domain.Page; import java.util.List; /** * Copyright(c)lbhbinhao@163.com * @author liubinhao * @date 2021/1/7 * ++++ ______ ______ ______ * +++/ /| / /| / /| * +/_____/ | /_____/ | /_____/ | * | | | | | | | | | * | | | | | |________| | | * | | | | | / | | | * | | | | |/___________| | | * | | |___________________ | |____________| | | * | | / / | | | | | | | * | |/ _________________/ / | | / | | / * |_________________________|/b |_____|/ |_____|/ */ public interface EmployeeService { List<Employee> findAllEmployees(); void saveEmployee(Employee employee); Employee getEmployeeById(long id); void deleteEmployeeById(long id); Page<Employee> findPaging(int no,int size); Page<Employee> findPaging(int no,int size,String searchKey); }
Business implementation class:
package com.lbh.xxmanager.service; import com.lbh.xxmanager.entity.Employee; import com.lbh.xxmanager.repo.EmployeeRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; /** * Copyright(c)lbhbinhao@163.com * * @author liubinhao * @date 2021/1/7 * ++++ ______ ______ ______ * +++/ /| / /| / /| * +/_____/ | /_____/ | /_____/ | * | | | | | | | | | * | | | | | |________| | | * | | | | | / | | | * | | | | |/___________| | | * | | |___________________ | |____________| | | * | | / / | | | | | | | * | |/ _________________/ / | | / | | / * |_________________________|/b |_____|/ |_____|/ */ @Service public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeRepository employeeRepository; @Override public List<Employee> findAllEmployees() { return employeeRepository.findAllByStatus(0); } @Override public void saveEmployee(Employee employee) { employee.setStatus(0); employeeRepository.save(employee); } @Override public Employee getEmployeeById(long id) { Optional<Employee> byId = employeeRepository.findById(id); Employee employee = null; if (byId.isPresent()){ employee = byId.get(); }else { throw new RuntimeException("Should id Employee does not exist!"); } return employee; } @Override public void deleteEmployeeById(long id) { Employee employeeById = getEmployeeById(id); employeeById.setStatus(1); employeeRepository.save(employeeById); } @Override public Page<Employee> findPaging(int no, int size) { Pageable pageable = PageRequest.of(no - 1,size); return employeeRepository.findAllByStatus(0,pageable); } @Override public Page<Employee> findPaging(int no, int size, String searchKey) { String key = "%"+searchKey+"%"; Pageable pageable = PageRequest.of(no - 1,size); return employeeRepository.findAllByStatusAndLocationLikeOrNameLikeOrEmailLike(0,key,key,key,pageable); } }
4.2.4 Web interface
package com.lbh.xxmanager.controller; import com.lbh.xxmanager.entity.Employee; import com.lbh.xxmanager.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.thymeleaf.util.StringUtils; import java.util.List; /** * Copyright(c)lbhbinhao@163.com * @author liubinhao * @date 2021/1/7 * ++++ ______ ______ ______ * +++/ /| / /| / /| * +/_____/ | /_____/ | /_____/ | * | | | | | | | | | * | | | | | |________| | | * | | | | | / | | | * | | | | |/___________| | | * | | |___________________ | |____________| | | * | | / / | | | | | | | * | |/ _________________/ / | | / | | / * |_________________________|/b |_____|/ |_____|/ */ @Controller public class EmployeeController { @Autowired private EmployeeService employeeService; @GetMapping("/") public String index(Model model){ model.addAttribute("employees",employeeService.findAllEmployees()); return "redirect:/page/1"; } @GetMapping("/newEmployee") public String newEmployee(Model model){ Employee employee = new Employee(); model.addAttribute("employee",employee); return "new_employee"; } @PostMapping("/saveEmployee") public String saveEmployee(@ModelAttribute Employee employee){ employeeService.saveEmployee(employee); return "redirect:/"; } @GetMapping("/updateEmployee/{id}") public String updateEmployee(@PathVariable Long id,Model model){ Employee employeeById = employeeService.getEmployeeById(id); model.addAttribute("employee",employeeById); return "update_employee"; } @GetMapping("/deleteEmployee/{id}") public String deleteEmployee(@PathVariable Long id){ employeeService.deleteEmployeeById(id); return "redirect:/"; } @GetMapping("/page/{pageNo}") public String findPaging(@PathVariable int pageNo, @RequestParam(required = false) String key, Model model){ Page<Employee> paging = null; if (StringUtils.isEmpty(key)) { paging = employeeService.findPaging(pageNo, 5); } else{ paging = employeeService.findPaging(pageNo, 5,key); } List<Employee> content = paging.getContent(); model.addAttribute("currentPage",pageNo); model.addAttribute("totalPages",paging.getTotalPages()); model.addAttribute("items",paging.getTotalElements()); model.addAttribute("employees",content); return "index"; } }
4.3 configuration file
springboot configuration file
server.port=9001 spring.datasource.username=root spring.datasource.password=Your database password spring.datasource.url=jdbc:mysql://localhost:3303/xx-manager?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&autoReconnect=true&useSSL=false&failOverReadOnly=false spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect # Update write this way to automatically create and update tables # Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto=update logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type=TRACE
5 Summary
It is not difficult to write such a simple background information management. It seems that the author is still young. I started to do it when I came home from work, but it's really difficult to write a blog. I haven't written it after 12 o'clock
After that, the time of blogging is almost the same as that of writing code. Oh, my poor hair.
Recent hot article recommendations:
1.1000 + Java interview questions and answers (2022 latest version)
2.Hot! The Java collaboration is coming...
3.Spring Boot 2.x tutorial, too complete!
4.Spring Boot 2.6 was officially released, a wave of new features..
5.Java development manual (Songshan version) is the latest release. Download it quickly!
Feel good, don't forget to like + forward!