查看生成的项目结构
01.2 调整pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
<relativePath />
parent>
<groupId>com.zfhgroupId>
<artifactId>emsartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>sb06-empsname>
<description>员工管理系统演示CRUD *** 作description>
<properties>
<java.version>1.8java.version>
<maven-jar-plugin.version>3.1.1maven-jar-plugin.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
01.3 搭建结构
02 项目配置文件
# server config
server.port=9527
server.servlet.context-path=/emps
server.servlet.jsp.init-parameters.development=true
# app view
spring.application.name=emps
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
# app db
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
# mybatis
mybatis.mapper-locations=classpath:com/zfh/mapper/*.xml
mybatis.type-aliases-package=com.zfh.entity
# log
logging.level.root=info
logging.level.root.com.zfh.dao=debug
03 项目结构
03.1 数据库和数据
/*
Navicat MySQL Data Transfer
Source Server : MySQL57
Source Server Version : 50718
Source Host : localhost:3306
Source Database : testdb
Target Server Type : MYSQL
Target Server Version : 50718
File Encoding : 65001
Date: 2021-05-23 08:00:37
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`name` varchar(255) DEFAULT NULL COMMENT '账号',
`money` float DEFAULT NULL COMMENT '金额',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES ('1', 'peter123', '2900');
INSERT INTO `account` VALUES ('2', 'merry456', '3000');
INSERT INTO `account` VALUES ('3', 'tom', '100');
INSERT INTO `account` VALUES ('4', 'mike迈克', '1000');
INSERT INTO `account` VALUES ('5', 'cock公鸡先生', '800');
INSERT INTO `account` VALUES ('6', 'netty', '1000');
INSERT INTO `account` VALUES ('10', 'underwood', '4000');
INSERT INTO `account` VALUES ('11', 'wetty', '500');
-- ----------------------------
-- Table structure for emp
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`empno` int(11) NOT NULL AUTO_INCREMENT,
`ename` char(50) NOT NULL,
`job` char(100) DEFAULT NULL,
`hiredate` datetime DEFAULT NULL,
`sal` float DEFAULT NULL,
`comm` float DEFAULT NULL,
PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=22222223 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `emp` VALUES ('1001', '周大明', '总总钻风', '2021-05-20 00:00:00', '25000', '35000');
INSERT INTO `emp` VALUES ('1002', '周小明', '总钻风', '2021-05-12 00:00:00', '5000', '5000');
INSERT INTO `emp` VALUES ('1004', '周小明', '总钻风', '2021-05-12 00:00:00', '2000', '3000');
INSERT INTO `emp` VALUES ('1008', '郑框架', '项目经理', '2019-10-10 00:00:00', '3000', '2000');
INSERT INTO `emp` VALUES ('1009', '郑前端', '前端工程师', '2020-11-11 00:00:00', '1000', '1000');
INSERT INTO `emp` VALUES ('1023', '张学良', '少帅', '1920-10-10 00:00:00', '100000', '200000');
INSERT INTO `emp` VALUES ('1024', '张学友', '文艺部', '2019-11-11 00:00:00', '20000', '20000');
INSERT INTO `emp` VALUES ('2002', '张员工', 'CTO司机', '2020-06-06 00:00:00', '2000', '3000');
INSERT INTO `emp` VALUES ('11111111', '周星星', '小卧底', '2020-09-01 00:00:00', '1000', '2000');
INSERT INTO `emp` VALUES ('22222222', '吴达达', '总钻风', '2020-09-01 00:00:00', '2000', '4000');
-- ----------------------------
-- Table structure for news
-- ----------------------------
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '新闻编号',
`title` varchar(50) NOT NULL COMMENT '新闻标题',
`content` text COMMENT '新闻正文',
`userid` int(11) NOT NULL COMMENT '发布人id',
`pubtime` datetime NOT NULL COMMENT '发布时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='新闻表';
-- ----------------------------
-- Records of news
-- ----------------------------
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`gender` varchar(2) DEFAULT NULL,
`gradeClass` varchar(255) DEFAULT NULL,
`birth` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`cellphone` char(11) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('2019001001', '张学霸', '女', '18级', '1999-10-01 19:03:06', '13812341234', '1234567890', ' 成都市学府路24号1-1-1');
-- ----------------------------
-- Table structure for tb_emp
-- ----------------------------
DROP TABLE IF EXISTS `tb_emp`;
CREATE TABLE `tb_emp` (
`id` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`salary` double DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_emp
-- ----------------------------
INSERT INTO `tb_emp` VALUES ('a00001', '关羽', '2000', '30');
INSERT INTO `tb_emp` VALUES ('a00002', '张飞', '2100', '31');
INSERT INTO `tb_emp` VALUES ('a00004', '赵云', '3000', '31');
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`realname` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`gender` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('6b448c7c-e8d2-460b-8ffd-10754e6d143a', 'jtzheng', '郑洁姚', '123456', '女');
INSERT INTO `tb_user` VALUES ('d137cc7d-9a04-4fba-ab1c-a75a3041c12d', 'fhzheng', '郑丰华', '123456', '男');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(20) NOT NULL COMMENT '用户名称',
`passowrd` varchar(18) NOT NULL COMMENT '用户密码',
`sex` enum('0','1') NOT NULL COMMENT '性别,0男1女',
`profession` enum('0','1','2') DEFAULT NULL COMMENT '职业,0学生1老师2工人',
`favourite` enum('0','1','2') DEFAULT NULL COMMENT '爱好,0电脑网络1影视娱乐2棋牌娱乐',
`note` varchar(255) DEFAULT NULL COMMENT '个人说明',
`type` enum('0','1') NOT NULL DEFAULT '0' COMMENT '用户类型,0普通用户1管理员,默认为0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
-- ----------------------------
-- Records of user
-- ----------------------------
-- ----------------------------
-- Table structure for userinfo
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`username` varchar(10) NOT NULL COMMENT '用户名',
`password` varchar(20) DEFAULT NULL,
`sex` enum('男','女') NOT NULL COMMENT '性别',
`note` varchar(255) DEFAULT NULL COMMENT '说明',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('admin', 'admin', '男', '系统管理员');
INSERT INTO `userinfo` VALUES ('admin1', 'admin1', '女', '系统管理员1');
INSERT INTO `userinfo` VALUES ('admin2', 'admin2', '男', '系统管理员2');
INSERT INTO `userinfo` VALUES ('sa', 'sa', '男', '超级用户');
04 入口文件,即启动文件和实体层
04.1 入口文件
package com.zfh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.zfh.dao")
public class Sb06EmpsApplication {
public static void main(String[] args) {
SpringApplication.run(Sb06EmpsApplication.class, args);
}
}
04.2 实体层-用户实体
package com.zfh.entity;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String username;
private String realname;
private String password;
private String gender;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", realname=" + realname + ", password=" + password
+ ", gender=" + gender + "]";
}
public User(String id, String username, String realname, String password, String gender) {
super();
this.id = id;
this.username = username;
this.realname = realname;
this.password = password;
this.gender = gender;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
## 04.3 实体层-员工实体
```java
package com.zfh.entity;
import java.io.Serializable;
public class Emp implements Serializable {
private String id;
private String name;
private Double salary;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", salary=" + salary + ", age=" + age + "]";
}
public Emp(String id, String name, Double salary, Integer age) {
super();
this.id = id;
this.name = name;
this.salary = salary;
this.age = age;
}
public Emp() {
super();
// TODO Auto-generated constructor stub
}
}
05 控制器
05.1 测试控制器
package com.zfh.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("test")
public class TestController {
@RequestMapping("hello")
@ResponseBody
public String sayHello() {
System.out.println("Client request test/hello contrller...");
return "hello,Springboot";
}
@RequestMapping("index")
public String goIndex() {
System.out.println("Client request test/index contrller...");
return "index";
}
}
05.2 用户控制器
package com.zfh.controller;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zfh.entity.User;
import com.zfh.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/regist")
public String userRegist() {
return "/user/regist";
}
@RequestMapping("/login")
public String userLogin() {
return "/user/login";
}
@RequestMapping("/regist.do")
public String userRegistAction(User user) {
String id = UUID.randomUUID().toString();
user.setId(id);
System.out.println(user);
try {
userService.save(user);
} catch (Exception e) {
e.printStackTrace();
return "redirect:/user/regist";
}
return "redirect:/user/login";
}
@RequestMapping("/login.do")
public String userLoginAction(String username, String password) {
try {
User user = userService.findUserByUsernameAndPassword(username,password);
if (user != null) {
return "redirect:/emp/main";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:/user/login";
}
return "redirect:/user/login";
}
}
05.3 员工管理控制器
package com.zfh.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zfh.entity.Emp;
import com.zfh.service.EmpService;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empService;
@RequestMapping("/main")
public String empMain(Model m) {
List<Emp> emps = empService.findAll();
m.addAttribute("emps", emps);
return "/emp/main";
}
@RequestMapping("/add")
public String empAdd() {
return "/emp/add";
}
@RequestMapping("/edit")
public String empEdit(String id,Model m) {
Emp emp = empService.findEmpById(id);
// System.out.println("emp id:"+id);
m.addAttribute("emp", emp);
return "/emp/edit";
}
@RequestMapping("/add.do")
public String empAddAction(Emp emp) {
try {
empService.save(emp);
return "redirect:/emp/main";
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/emp/main";
}
@RequestMapping("/edit.do")
public String empEditAction(Emp emp) {
try {
empService.alter(emp);
return "redirect:/emp/main";
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/emp/main";
}
@RequestMapping("/del")
public String empDelAction(String id) {
try {
empService.remove(id);
return "redirect:/emp/main";
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/emp/main";
}
}
06 业务层
06.1 用户管理业务接口
package com.zfh.service;
import com.zfh.entity.User;
public interface UserService {
void save(User user) throws Exception;
User findUserByUsernameAndPassword(String username, String password);
}
06.2 用户管理业务接口实现
package com.zfh.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zfh.dao.UserDAO;
import com.zfh.entity.User;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserDAO userDAO;
@Override
public void save(User user) throws Exception {
userDAO.insertObj(user);
}
@Override
public User findUserByUsernameAndPassword(String username, String password) {
return userDAO.selectObjByNameAndPwd(username,password);
}
}
06.3 员工管理业务接口
package com.zfh.service;
import java.util.List;
import com.zfh.entity.Emp;
public interface EmpService {
List<Emp> findAll();
void save(Emp emp);
void alter(Emp emp);
Emp findEmpById(String id);
void remove(String id);
}
06.4 员工管理业务接口实现
package com.zfh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zfh.dao.EmpDAO;
import com.zfh.entity.Emp;
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDAO empDAO;
@Override
public List<Emp> findAll() {
return empDAO.selectAllObjs();
}
@Override
public void save(Emp emp) {
empDAO.insertObj(emp);
}
@Override
public void alter(Emp emp) {
empDAO.updateObj(emp);
}
@Override
public Emp findEmpById(String id) {
return empDAO.selectObjById(id);
}
@Override
public void remove(String id) {
empDAO.deleteObj(id);
}
}
07 数据访问层接口
07.1 数据访问接口泛型
package com.zfh.dao;
import java.util.List;
public interface BaseDAO<T,K> {
void insertObj(T t);
void updateObj(T t);
void deleteObj(K id);
T selectObjById(K id);
List<T> selectAllObjs();
Long getTotalObjsCount();
List<T> selectObjsByPage(Integer start,Integer size);
}
07.2 用户dao
package com.zfh.dao;
import com.zfh.entity.User;
public interface UserDAO extends BaseDAO<User, String> {
User selectObjByNameAndPwd(String username, String password);
}
07.3 员工dao
package com.zfh.dao;
import com.zfh.entity.Emp;
public interface EmpDAO extends BaseDAO<Emp, String> {
}
08 数据访问层mybatis的mapper配置
08.1 用户mapper
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zfh.dao.UserDAO" >
<insert id="insertObj" parameterType="com.zfh.entity.User">
insert into tb_user(id,username,realname,password,gender)
values(#{id},#{username},#{realname},#{password},#{gender})
insert>
<select id="selectObjById" parameterType="String" resultType="com.zfh.entity.User">
select
id,username,realname,password,gender
from
tb_user
where
id = #{id}
select>
<select id="selectObjByNameAndPwd" resultType="com.zfh.entity.User">
select
id,username,realname,password,gender
from
tb_user
where
username = #{username} and password=#{password}
select>
mapper>
08.2 员工mapper
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zfh.dao.EmpDAO" >
<insert id="insertObj">
insert into tb_emp(id,name,salary,age)
values(#{id},#{name},#{salary},#{age})
insert>
<delete id="deleteObj">
delete from tb_emp
where id=#{id}
delete>
<update id="updateObj">
update tb_emp
set name=#{name},salary=#{salary},age=#{age}
where
id=#{id}
update>
<select id="selectAllObjs" resultType="com.zfh.entity.Emp">
select
id,name,salary,age
from tb_emp
select>
<select id="selectObjById" parameterType="String" resultType="com.zfh.entity.Emp">
select
id,name,salary,age
from tb_emp
where
id = #{id}
select>
mapper>
09 视图部分
09.1 默认首页index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test page
webcome to springboot project, this is a jsp test page.
09.2 员工信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
员工管理首页
员工信息
序号
员工ID
员工姓名
员工工资
员工年龄
*** 作
添加员工
还没有员工数据!!!
${status.count}
${emp.id}
${emp.name}
${emp.salary}
${emp.age}
编辑
删除
09.3 添加员工
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
添加员工
添加员工
09.4 编辑员工
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
编辑员工
编辑员工
09.5 用户登录
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
user login
用户登录
09.6 用户注册
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
user regist
用户注册
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)