博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
员工管理系统一:数据准备和配置项目环境
阅读量:3967 次
发布时间:2019-05-24

本文共 11155 字,大约阅读时间需要 37 分钟。

1.新建一个模块

在这里插入图片描述

在这里插入图片描述

2.配置项目环境及首页

注意:以下程序不用@Autowired导入的原因是会一直报bean创建失败的错误,解决方法尚未找到

1、导入依赖pom.xml

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

2、导入实体类

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;

3、编写mapper接口以及实现类

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 Map
map = 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); Collection
selectAllEmployees(); int updateByPrimaryKey(Employee record);}

@PostConstruct注解理解

@Autowired不能给静态变量注入

@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。

Java中该注解的说明

  • @PostConstruct该注解被用来修饰一个非静态的void()方法。
  • 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。
  • PostConstruct在构造函数之后执行,init()方法之前执行。

通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:

Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
详细解释参考

EmployeeMapperImpl

package 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 Map
employees = 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; }}

4、编写service接口以及实现类

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); Collection
selectAllDepartment(); 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 Collection
selectAllDepartment() {
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); Collection
selectAllEmployees(); 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 Collection
selectAllEmployees() {
System.out.println(employeeMapper); return employeeMapper.selectAllEmployees(); } @Override public int updateByPrimaryKey(Employee record) {
return employeeMapper.updateByPrimaryKey(record); }}

5、注意Maven资源导出问题!

src/main/java
**/*.xml

6.application.properties

#设置连接数据库的配置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

即使没有连接数据库也要写不然运行不了

3.导入静态资源

1、css,js等放在static文件夹下

2、html 放在 templates文件夹下

4.最终结构如下:

在这里插入图片描述

转载地址:http://vmuki.baihongyu.com/

你可能感兴趣的文章
数字视频基础(一)
查看>>
AM5728概述(1)
查看>>
AM5728概述(4)
查看>>
AM5728概述(6)
查看>>
RapidIO协议(1)
查看>>
RapidIO协议(2)
查看>>
DM8168 EMAC/MDIO模块(2)
查看>>
DM8168 EMAC/MDIO模块(3)
查看>>
DM8168 EMAC/MDIO模块(4)
查看>>
DM8168 EMAC/MDIO模块(5)
查看>>
DM8168 EMAC/MDIO模块(6)
查看>>
DM8168 EMAC/MDIO模块(7)
查看>>
DM8168 EMAC/MDIO模块(8)
查看>>
TVP5158的多路复用技术
查看>>
DM8168 HDVPSS的VIP Parser模块(1)
查看>>
DM8168 HDVPSS的VIP Parser模块(2)
查看>>
DM8168 HDVPSS的VIP Parser模块(5)
查看>>
ADIS16400/ADIS16405带磁力计的三轴惯性传感器(1)
查看>>
ADIS16400/ADIS16405带磁力计的三轴惯性传感器(2)
查看>>
ADIS16400/ADIS16405带磁力计的三轴惯性传感器(3)
查看>>