1. 创建maven项目
代码语言:javascript
复制<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>cn.javabs</groupId>
<artifactId>job</artifactId>
<version>1.0.1-SNAPSHOT</version>
<name>job</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--SpringMVC的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope><!--运行时期-->
<optional>true</optional>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Lombok简化实体类的插件包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--SpringBoot启动的测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--数据源连接池:德鲁伊-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<!--mail-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.1</version>
</dependency>
<!--上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!--分页的插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
2. 编写配置文件
代码语言:javascript
复制# 服务器
server.port=8081
#json的时间格式:
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#模板引擎
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
# 静态资源的释放
spring.mvc.static-path-pattern=/static/**
#文件上传的大小
spring.servlet.multipart.max-file-size=2MB
# 定义 加载 开发环境下的 配置文件
#spring.profiles.active=dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/job?useUnicode=true&characterEncoding=utf-8&serverTimeZone=GMT
spring.datasource.username=root
spring.datasource.password=sorry
mybatis.mapper-locations=classpath:/cn/javabs/job/mapper/*.xml
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.pageSize = 10
#热部署
spring.devtools.restart.enabled=true
3. 编写实体类
代码语言:javascript
复制package cn.javabs.job.entity;
import lombok.Data;
/**
* 用户实体类设计
* 1. 应聘者: 学生
* 2. 招聘者: 公司人力资源
*/
@Data
public class User {
// -----------------定义常量:--------------------
final int USER_SEX_MAN = 1; // 性别 男
final int USER_SEX_WOMAN = 2; // 性别 女
final int USER_SEX_UNKONW = 0; // 性别 未知
final String DEFAULT_HEAD_IMGAE = "common/default_img.jpg"; // 默认的用户头像
final String DEFAULT_WORK_EXP = "应届毕业生"; // 默认是应届毕业生
final String DEFAULT_DEGREE = "其他"; // 默认是其他、专科、本科、研究生
// -----------------变量:--------------------
private int userId;
private String username; // 用户名
private String password;// 密码
private String email;// 邮箱地址
private String headPic = DEFAULT_HEAD_IMGAE; // 用户头像
private String workExp = DEFAULT_WORK_EXP; // 工作经验,默认是应届毕业生
private String degree = DEFAULT_DEGREE; // 学历,默认是其他
private int sex = USER_SEX_UNKONW ;// 用户性别 默认是未知
private int type ; // 用户类别, 0-学生(应聘者);1-(公司的HR)招聘者
private String mobile ; // 手机号码
private String selfDescription ; // 自我描述
}
4. 编写数据持久层接口
代码语言:javascript
复制package cn.javabs.job.mapper;
import cn.javabs.job.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userMapper")
public interface UserMapper {
/**
* 后端获取所有用户的信息
* @return 返回的集合
*/
List<User> findAllUserList();
/**
* 按照邮箱查询用户的信息
* @param email
* @return 返回的对象
*/
User findUserByEmail(String email);
/**
* 按照条件(用户名或者性别或者学历)查询用户信息 返回的集合
* @param condition
* @return
*/
List<User> findUserByCondition(String condition);
/**
* 按照用户id查询用户信息
* @param userId
* @return 返回的对象
*/
User findUserByUserId(int userId);
/**
* 用户登录
* 根据用户名和密码及用户类型执行 登录
* @param user
* @return
*/
User login(User user);
int addUser(User user);
int delUser(int userId);
int editUser(User user);
}
5. 编写持久层映射文件
代码语言:javascript
复制<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "
http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.javabs.job.mapper.UserMapper">
<!--添加用户-->
<insert id="addUser" parameterType="cn.javabs.job.entity.User">
insert into user
<trim prefix="(" suffix=") values" suffixOverrides=",">
<if test="username != nul and username != '' ">
username,
</if>
<if test="password != nul and password != '' ">
password,
</if>
<if test="headPic != nul and headPic != '' ">
headPic,
</if>
<if test="sex != nul and sex != '' ">
sex,
</if>
<if test="email != nul and email != '' ">
email,
</if>
<if test="mobile != nul and mobile != '' ">
mobile,
</if>
<if test="degree != nul and degree != '' ">
degree,
</if>
<if test="workExp != nul and workExp != '' ">
workExp,
</if>
<if test="type != nul and type != '' ">
type,
</if>
<if test="selfDescription != nul and selfDescription != '' ">
selfDescription,
</if>
</trim>
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username != nul and username != '' ">
#{username},
</if>
<if test="password != nul and password != '' ">
#{password},
</if>
<if test="headPic != nul and headPic != '' ">
#{headPic},
</if>
<if test="sex != nul and sex != '' ">
#{sex},
</if>
<if test="email != nul and email != '' ">
#{email},
</if>
<if test="mobile != nul and mobile != '' ">
#{mobile},
</if>
<if test="degree != nul and degree != '' ">
#{degree},
</if>
<if test="workExp != nul and workExp != '' ">
#{workExp},
</if>
<if test="type != nul and type != '' ">
#{type},
</if>
<if test="selfDescription != nul and selfDescription != '' ">
#{selfDescription},
</if>
</trim>
</insert>
<!--修改用户-->
<update id="editUser" parameterType="cn.javabs.job.entity.User">
update user
<set>
<!-- <if test="username != nul and username != '' ">
#{username},
</if>-->
<if test="password != nul and password != '' ">
#{password},
</if>
<if test="headPic != nul and headPic != '' ">
#{headPic},
</if>
<if test="sex != nul and sex != '' ">
#{sex},
</if>
<if test="email != nul and email != '' ">
#{email},
</if>
<if test="mobile != nul and mobile != '' ">
#{mobile},
</if>
<if test="degree != nul and degree != '' ">
#{degree},
</if>
<if test="workExp != nul and workExp != '' ">
#{workExp},
</if>
<!-- <if test="type != nul and type != '' ">
#{type},
</if>-->
<if test="selfDescription != nul and selfDescription != '' ">
#{selfDescription},
</if>
</set>
where
userId = #{userId}
</update>
<!--删除用户-->
<delete id="delUser" parameterType="int">
delete from user
<where>
<if test="userId != nul and userId != '' ">
and userId = #{userId},
</if>
</where>
</delete>
<!--查询所有用户-->
<select id="findAllUserList" resultType="cn.javabs.job.entity.User">
select * from user
</select>
<!--按照邮箱查询用户的信息-->
<select id="findUserByEmail" resultType="cn.javabs.job.entity.User" parameterType="String">
select * from user
<where>
<if test="email != nul and email != '' ">
and email = #{email},
</if>
</where>
</select>
<!--按照条件(用户名或者性别或者学历)查询用户信息 返回的集合-->
<select id="findUserByCondition" resultType="cn.javabs.job.entity.User" parameterType="String">
select * from user
<where>
<!--用户名 进行模糊查询-->
<if test="username != nul and username != '' ">
and username like concat('%',#{username},'%'),
</if>
<if test="sex != nul and sex != '' ">
and sex = #{sex},
</if>
<if test="degree != nul and degree != '' ">
and degree = #{degree},
</if>
</where>
</select>
<!--根据用户编号查询用户信息-->
<select id="findUserByUserId" resultType="cn.javabs.job.entity.User" parameterType="int">
select * from user
<where>
<if test="userId != nul and userId != '' ">
and userId = #{userId}
</if>
</where>
</select>
<!--用户登录-->
<select id="login" resultType="cn.javabs.job.entity.User" parameterType="cn.javabs.job.entity.User">
select * from user
<where>
<if test="username != nul and username != '' ">
and username = #{username}
</if>
<if test="password != nul and password != '' ">
and password = #{password}
</if>
<if test="type != nul and type != '' ">
and type = #{type}
</if>
</where>
</select>
</mapper>
6. 编写业务逻辑层接口
代码语言:javascript
复制package cn.javabs.job.service;
import cn.javabs.job.entity.User;
import java.util.List;
public interface UserService {
List<User> getAllUserList();
User getUserByEmail(String email);
User getUserByUserId(int userId);
List<User> getUserByCondition(String condition);
User userLogin(User user);
int deleteUser(int userId);
int editUser(User user);
int addUser(User user);
}
7. 编写业务逻辑层接口的实现类
代码语言:javascript
复制package cn.javabs.job.service.impl;
import cn.javabs.job.entity.User;
import cn.javabs.job.mapper.UserMapper;
import cn.javabs.job.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
@Qualifier("userMapper")
private UserMapper userMapper;
@Override
public List<User> getAllUserList() {
return userMapper.findAllUserList();
}
@Override
public User getUserByEmail(String email) {
return userMapper.findUserByEmail(email);
}
@Override
public User getUserByUserId(int userId) {
return userMapper.findUserByUserId(userId);
}
@Override
public List<User> getUserByCondition(String condition) {
return userMapper.findUserByCondition(condition);
}
@Override
public User userLogin(User user) {
return userMapper.login(user);
}
@Override
public int deleteUser(int userId) {
return userMapper.delUser(userId);
}
@Override
public int editUser(User user) {
return userMapper.editUser(user);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
}
8. 编写控制器类
代码语言:javascript
复制package cn.javabs.job.controller;
import cn.javabs.job.entity.User;
import cn.javabs.job.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 后端用户管理的控制器
*/
@Controller
@RequestMapping("/admin/user")
public class UserController {
@Resource
@Qualifier("userService")
private UserService userService;
@Value("${pagehelper.pageSize}")
private int pageSize;
@RequestMapping("userList")
public ModelAndView userList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
PageHelper.startPage(pageNum,pageSize);
List<User> userList = userService.getAllUserList();
PageInfo<User> pageInfo = new PageInfo<>(userList);
ModelAndView mv = new ModelAndView();
mv.addObject("userList",userList);
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user");
return mv;
}
}
9. 编写页面,模板引擎
代码语言:javascript
复制<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户管理</title>
<link rel="stylesheet" th:href="@{../static/component/style/components.css}" href="../static/component/style/components.css">
<link rel="stylesheet" th:href="@{../static/css/bootstrap.css}" href="../static/css/bootstrap.css">
<link rel="stylesheet" th:href="@{../static/css/plugins.css}" href="../static/css/plugins.css">
<link rel="stylesheet" th:href="@{../static/css/main.css}" href="../static/css/main.css">
<link rel="stylesheet" th:href="@{../static/css/themes.css}" href="../static/css/themes.css">
<script th:src="@{../static/component/js/JQuery2.1.4.js}"></script>
<script th:src="@{../static/component/js/bootstrap.min.js}"></script>
</head>
<body>
<div id="body">
<ol class="breadcrumb">
<li class="active"><a href="#">系统</a></li>
<li>用户列表</li>
</ol>
<div class="barboxs">
<button class="btn btn-success pull-left " data-toggle="modal" data-target="#myModal" title="" data-placement="right" data-original-title="添加用户">
<i class="fa fa-pencil-square-o"></i>
</button>
<button class="btn btn-danger pull-left ml10" data-toggle="tooltip" title="" data-placement="right" data-original-title="删除用户"><i class="fa fa-trash-o"></i></button>
<div class="leftbox">
<div class="liselect w300">
<div class="input-group">
<input type="text" id="example-input-typeahead" class="form-control example-typeahead" placeholder="请输入关键词">
<span class="input-group-btn">
<button class="btn btn-success"><i class="fa fa-search"></i></button>
</span>
</div>
</div>
</div>
</div>
<div class="tablebox">
<table class="table table-bordered">
<thead>
<tr>
<th class="text-center" width="50"><input type="checkbox" id="check5-all" name="check5-all"></th>
<th class="text-center">序号</th>
<th>用户账号</th>
<th>用户密码</th>
<th>电子邮件</th>
<th>手机号码</th>
<th>用户学历</th>
<th>用户性别</th>
<th>用户类别</th>
<th>创建时间</th>
<th class="text-center" width="85"><i class="fa fa-bolt"></i> 操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user,userStatus:${userList}">
<td class="text-center"><input type="checkbox" id="check5-td1" name="check5-td1"></td>
<td class="cell-small text-center"><span th:text="${userStatus.index}">序号</span></td>
<td th:text="${user.username}">custom_admins</td>
<td th:text="${user.password}">custom_admins</td>
<td th:text="${user.email}">custom_admins</td>
<td th:text="${user.mobile}">custom_admins</td>
<td th:text="${user.degree}">custom_admins</td>
<span th:if="${user.sex} == 0">
<td >未知</td>
</span>
<span th:if="${user.sex} == 1">
<td >男</td>
</span>
<span th:if="${user.sex} == 2">
<td >女</td>
</span>
<td th:text="${user.type}">custom_admins</td>
<td>2021-3-22 11:04:49</td>
<td class="text-center">
<div class="btn-group">
<a href="javascript:void(0)" class="btn btn-xs btn-info"><i class="fa fa-globe"></i></a>
<a href="javascript:void(0)" class="btn btn-xs btn-success"><i class="fa fa-pencil"></i></a>
<a href="javascript:void(0)" class="btn btn-xs btn-danger"><i class="fa fa-trash-o"></i></a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>