修改功能页面
xx-manager
更新
修改
回退
4.2 后台业务实现
4.2.1 实体类Employee
对应数据库中的表
package com.lbh.xxmanager.entity;
import javax.persistence.*;
/**
* Copyright©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 数据库操作层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©lbhbinhao@163.com
* @author liubinhao
* @date 2021/1/7
* ++++ ______ ______ ______
* +++/ /| / /| / /|
* +// | // | /_____/ |
* | | | | | | | | |
* | | | | | |________| | |
* | | | | | / | | |
* | | | | |/___________| | |
* | | |___________________ | |____________| | |
* | | / / | | | | | | |
* | |/ _________________/ / | | / | | /
* |_________________________|/b ||/ ||/
*/
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long> {
List findAllByStatus(int status);
Page findAllByStatus(int status, Pageable pageable);
Page findAllByStatusAndLocationLikeOrNameLikeOrEmailLike(int status,String locaion,String name,String email, Pageable pageable);
}
4.2.3 业务代码service
接口:
package com.lbh.xxmanager.service;
import com.lbh.xxmanager.entity.Employee;
import org.springframework.data.domain.Page;
import java.util.List;
/**
* Copyright©lbhbinhao@163.com
* @author liubinhao
* @date 2021/1/7
* ++++ ______ ______ ______
* +++/ /| / /| / /|
* +// | // | /_____/ |
* | | | | | | | | |
* | | | | | |________| | |
* | | | | | / | | |
* | | | | |/___________| | |
* | | |___________________ | |____________| | |
* | | / / | | | | | | |
* | |/ _________________/ / | | / | | /
* |_________________________|/b ||/ ||/
*/
public interface EmployeeService {
List findAllEmployees();
void saveEmployee(Employee employee);
Employee getEmployeeById(long id);
void deleteEmployeeById(long id);
Page findPaging(int no,int size);
Page findPaging(int no,int size,String searchKey);
}
业务实现类:
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©lbhbinhao@163.com
* @author liubinhao
* @date 2021/1/7
* ++++ ______ ______ ______
* +++/ /| / /| / /|
* +// | // | /_____/ |
* | | | | | | | | |
* | | | | | |________| | |
* | | | | | / | | |
* | | | | |/___________| | |
* | | |___________________ | |____________| | |
* | | / / | | | | | | |
* | |/ _________________/ / | | / | | /
* |_________________________|/b ||/ ||/
*/
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public List findAllEmployees() {
return employeeRepository.findAllByStatus(0);
}
@Override
public void saveEmployee(Employee employee) {
employee.setStatus(0);
employeeRepository.save(employee);
}
@Override
public Employee getEmployeeById(long id) {
Optional byId = employeeRepository.findById(id);
Employee employee = null;
if (byId.isPresent()){
employee = byId.get();
}else {
throw new RuntimeException(“该id员工不存在!”);
}
return employee;
}
@Override
public void deleteEmployeeById(long id) {
Employee employeeById = getEmployeeById(id);
employeeById.setStatus(1);
employeeRepository.save(employeeById);
}
@Override
public Page findPaging(int no, int size) {
Pageable pageable = PageRequest.of(no - 1,size);
return employeeRepository.findAllByStatus(0,pageable);
}
@Override
public Page 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接口
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©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 paging = null;
if (StringUtils.isEmpty(key)) {
paging = employeeService.findPaging(pageNo, 5);
}
else{
paging = employeeService.findPaging(pageNo, 5,key);
}
List 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 配置文件
springboot的配置文件
server.port=9001
spring.datasource.username=root
spring.datasource.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 这样写可以自动建表,更新表
# 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 总结
写一个这样简单的后台信息管理还是不难,看来笔者还是宝刀未老。下班回家开始做,不过写博客是真滴难,都过了12点还没有写
完,写博客和我写代码的时间也相差无几了。哎,我这可怜的头发啊。
(感谢阅读,希望对你所有帮助)
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
nate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# update 这样写可以自动建表,更新表
# 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 总结
写一个这样简单的后台信息管理还是不难,看来笔者还是宝刀未老。下班回家开始做,不过写博客是真滴难,都过了12点还没有写
完,写博客和我写代码的时间也相差无几了。哎,我这可怜的头发啊。
(感谢阅读,希望对你所有帮助)
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
[外链图片转存中…(img-rjNsvDFI-1721066739867)]
[外链图片转存中…(img-DprqSGa6-1721066739867)]
[外链图片转存中…(img-X2DRHgC7-1721066739868)]