本文共 11155 字,大约阅读时间需要 37 分钟。
注意:以下程序不用@Autowired导入的原因是会一直报bean创建失败的错误,解决方法尚未找到
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.5.RELEASE com.loey springboot-04-staffmanagement 1.0.0 11 org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine src/main/java **/*.xml org.mybatis.generator mybatis-generator-maven-plugin 1.3.6 GeneratorMapper.xml true true org.springframework.boot spring-boot-maven-plugin
public class Department { private Integer deptno; private String dname; private String loc;
public class Employee { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; private Department department;
DepartmentMapper
package com.loey.mapper;import com.loey.model.Department;import org.springframework.stereotype.Repository;import javax.annotation.Resource;import java.util.*;@Repositorypublic class DepartmentMapperImpl implements DepartmentMapper { private static Mapmap = null; static{ map = new HashMap (); map.put(10,new Department(10,"人事部","北京")); map.put(30,new Department(30,"财务部","北京")); map.put(20,new Department(20,"后勤部","北京")); } @Override public int deleteByPrimaryKey(Integer deptno) { map.remove(deptno); return 0; } @Override public int insert(Department record) { Integer deptno = record.getDeptno(); map.put(deptno,record); return 0; } @Override public Department selectByPrimaryKey(Integer deptno) { Department department = map.get(deptno); return department; } @Override public Collection selectAllDepartment() { Collection departments = map.values(); return departments; } @Override public int updateByPrimaryKey(Department record) { Integer deptno = record.getDeptno(); map.put(deptno,record); return 0; }}
EmployeeMapper
package com.loey.mapper;import com.loey.model.Employee;import org.springframework.stereotype.Repository;import java.util.Collection;import java.util.List;public interface EmployeeMapper { int deleteByPrimaryKey(Integer empno); int insert(Employee record); Employee selectByPrimaryKey(Integer empno); CollectionselectAllEmployees(); int updateByPrimaryKey(Employee record);}
@Autowired不能给静态变量注入
@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。Java中该注解的说明:
通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法) 详细解释参考 EmployeeMapperImplpackage com.loey.mapper;import com.loey.model.Department;import com.loey.model.Employee;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import javax.annotation.PostConstruct;import java.util.*;@Repositorypublic class EmployeeMapperImpl implements EmployeeMapper { // @Autowired不能给静态变量注入// @Autowired// private DepartmentMapper departmentMapper;/* @Autowired private DepartmentMapper myDepartmentMapper; @PostConstruct public void init(){ departmentMapper = this.myDepartmentMapper; System.out.println(departmentMapper); }*/ private static DepartmentMapper departmentMapper = new DepartmentMapperImpl(); //模拟数据库中的数据 private static Mapemployees = null; static { employees = new HashMap ();//创建一个员工表 employees.put(1,new Employee(1,"zhangsan","sing",2, new Date(2001 -1900,10 - 1,8),100000.0,10.0,10,departmentMapper.selectByPrimaryKey(10))); employees.put(2,new Employee(2,"lisi","dance",3,new Date(2002 - 1900,10 - 1,8),10000.0,5.0,30,departmentMapper.selectByPrimaryKey(30))); employees.put(3,new Employee(3,"wangwu","sing",null,new Date(2003 - 1900,10 - 1,8),200000.0,10.0,20,departmentMapper.selectByPrimaryKey(20))); } //主键自增 private static Integer initId = 3; @Override public int deleteByPrimaryKey(Integer empno) { employees.remove(empno); return 0; } @Override public int insert(Employee record) { initId++; Integer deptno = record.getDeptno(); Department department = departmentMapper.selectByPrimaryKey(deptno); record.setDepartment(department); record.setEmpno(initId); employees.put(initId,record); return 0; } @Override public Employee selectByPrimaryKey(Integer empno) { Employee employee = employees.get(empno); System.out.println(employee); return employee; } @Override public Collection selectAllEmployees() { Collection AllEmployees = employees.values(); System.out.println(AllEmployees); return AllEmployees; } @Override public int updateByPrimaryKey(Employee record) { Integer empno = record.getEmpno(); Integer deptno = record.getDeptno(); Department department = departmentMapper.selectByPrimaryKey(deptno); record.setDepartment(department); employees.put(empno,record); return 0; }}
DepartmentService
package com.loey.service;import com.loey.model.Department;import java.util.Collection;import java.util.List;public interface DepartmentService { int deleteByPrimaryKey(Integer deptno); int insert(Department record); Department selectByPrimaryKey(Integer deptno); CollectionselectAllDepartment(); int updateByPrimaryKey(Department record);}
DepartmentServiceImpl
package com.loey.service;import com.loey.mapper.DepartmentMapper;import com.loey.mapper.DepartmentMapperImpl;import com.loey.model.Department;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.Collection;import java.util.List;/** * @ClassName DepartmentServiceImpl * @Author loey * @Date Create in 2021/1/1 17:47 * @Version 1.0 **/@Servicepublic class DepartmentServiceImpl implements DepartmentService{ // @Autowired// private DepartmentMapper departmentMapper; private DepartmentMapper departmentMapper = new DepartmentMapperImpl(); @Override public int deleteByPrimaryKey(Integer deptno) { return departmentMapper.deleteByPrimaryKey(deptno); } @Override public int insert(Department record) { return departmentMapper.insert(record); } @Override public Department selectByPrimaryKey(Integer deptno) { return departmentMapper.selectByPrimaryKey(deptno); } @Override public CollectionselectAllDepartment() { return departmentMapper.selectAllDepartment(); } @Override public int updateByPrimaryKey(Department record) { return departmentMapper.updateByPrimaryKey(record); }}
EmployeeService
package com.loey.service;import com.loey.model.Employee;import java.util.Collection;import java.util.List;public interface EmployeeService { int deleteByPrimaryKey(Integer empno); int insert(Employee record); Employee selectByPrimaryKey(Integer empno); CollectionselectAllEmployees(); int updateByPrimaryKey(Employee record);}
EmployeeServiceImpl
package com.loey.service;import com.loey.mapper.EmployeeMapper;import com.loey.mapper.EmployeeMapperImpl;import com.loey.model.Employee;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.Collection;import java.util.List;@Servicepublic class EmployeeServiceImpl implements EmployeeService { // @Autowired// private EmployeeMapper employeeMapper; private EmployeeMapper employeeMapper = new EmployeeMapperImpl(); @Override public int deleteByPrimaryKey(Integer empno) { return employeeMapper.deleteByPrimaryKey(empno); } @Override public int insert(Employee record) { return employeeMapper.insert(record); } @Override public Employee selectByPrimaryKey(Integer empno) { return employeeMapper.selectByPrimaryKey(empno); } @Override public CollectionselectAllEmployees() { System.out.println(employeeMapper); return employeeMapper.selectAllEmployees(); } @Override public int updateByPrimaryKey(Employee record) { return employeeMapper.updateByPrimaryKey(record); }}
src/main/java **/*.xml
#设置连接数据库的配置spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/staff_management?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=1127
即使没有连接数据库也要写不然运行不了
1、css,js等放在static文件夹下
2、html 放在 templates文件夹下转载地址:http://vmuki.baihongyu.com/