SpringBoot+Thymeleaf配置员工管理系统

1.搭建环境

1.1创建一个Springboot项目(添加Spring WEB依赖)
1.2新建相关的实体类
  1. config层
  2. controller层
  3. pojo层
  4. dao层
1.3引入Thymeleaf的jar包
<!--Thymeleaf 2.x-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
1.4测试项目是否能够运行
  1. 在templates目录下新建一个index.html首页(测试完之后删除)

  2. 编写Controller类测试运行

    package com.ddf.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @Controller
    public class MyController {
         
    
        @RequestMapping("/index")
        public String index(){
         
            return "index";
        }
    }
    

2.模拟数据库在类中建表

  1. 新建实体类Department(部门表)、Employee(员工表)

    package com.ddf.pojo;
    
    //部门表
    public class Department {
         
    
        private Integer id;
        private String departmentName;
    
        public Department(){
         }
    
        public Department(Integer id, String departmentName) {
         
            this.id = id;
            this.departmentName = departmentName;
        }
    
        public Integer getId() {
         
            return id;
        }
    
        public void setId(Integer id) {
         
            this.id = id;
        }
    
        public String getDepartmentName() {
         
            return departmentName;
        }
    
        public void setDepartmentName(String departmentName) {
         
            this.departmentName = departmentName;
        }
    }
    
    package com.ddf.pojo;
    
    import java.util.Date;
    
    //员工表
    public class Employee {
    
        private Integer id;
        private String lastName;
        private String email;
        private Integer gender; //0:女  1:男
    
        private Department department;
        private Date birth;
    
        public Employee(){}
    
        public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
            this.id = id;
            this.lastName = lastName;
            this.email = email;
            this.gender = gender;
            this.department = department;
            //默认的创建日期
            this.birth = new Date();
        }
    
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Integer getGender() {
            return gender;
        }
    
        public void setGender(Integer gender) {
            this.gender = gender;
        }
    
        public Department getDepartment() {
            return department;
        }
    
        public void setDepartment(Department department) {
            this.department = department;
        }
    
        public Date getBirth() {
            return birth;
        }
    
        public void setBirth(Date birth) {
            this.birth = birth;
        }
    }
    
  2. 新建Dao层:DepartmentDao、EmployeeDao

    package com.ddf.dao;
    
    import com.ddf.pojo.Department;
    import org.springframework.stereotype.Repository;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    
    //部门Dao
    @Repository
    public class DepartmentDao {
         
    
        //模拟数据库中的数据
    
        private static Map<Integer, Department> departments = null;
    
        static {
         
            departments = new HashMap<Integer, Department>();//创建一个部门表
            departments.put(101,new Department(101,"教学部"));
            departments.put(102,new Department(102,"市场部"));
            departments.put(103,new Department(103,"教研部"));
            departments.put(104,new Department(104,"运营部"));
            departments.put(105,new Department(105,"后勤部"));
        }
    
        //获取所有部门信息
        public Collection<Department> getDepartment(){
         
            return departments.values();
        }
    
        //通过id得到部门
        public Department getDepartmentById(Integer id){
         
            return departments.get(id);
        }
    }
    
    package com.ddf.dao;
    
    import com.ddf.pojo.Department;
    import com.ddf.pojo.Employee;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    
    //员工Dao
    @Repository
    public class EmployeeDao {
         
        //模拟数据库中的数据
        private static Map<Integer, Employee> employees = null;
        //员工有所属的部门
        @Autowired
        private DepartmentDao departmentDao;
    
        static {
         
            employees = new HashMap<Integer, Employee>();//创建一个部门表
    
            employees.put(1001,new Employee(1001,"AA","A814655997@qq.com",0,new Department(101,"教学部")));
            employees.put(1002,new Employee(1002,"BB","B814655997@qq.com",1,new Department(102,"市场部")));
            employees.put(1003,new Employee(1003,"CC","C814655997@qq.com",0,new Department(103,"教研部")));
            employees.put(1004,new Employee(1004,"DD","D814655997@qq.com",1,new Department(104,"运营部")));
            employees.put(1005,new Employee(1005,"EE","E814655997@qq.com",0,new Department(105,"后勤部")));
        }
    
        //主键自增!
        private static Integer initId = 1006;
        //增加一个员工
        public void save(Employee employee){
         
            if(employee.getId() == null){
         
                employee.setId(initId++);
            }
    
            employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
            //把员工添加到数据库中
            employees.put(employee.getId(),employee);
        }
    
        //查询全部员工信息
        public Collection<Employee> getAll(){
         
            return employees.values();
        }
    
        //通过Id查询员工
        public Employee getEmployeeById(Integer id){
         
            return employees.get(id);
        }
        //删除员工通过id
        public void delete(Integer id){
         
            employees.remove(id);
        }
    }
    

3.首页的实现

3.1引入静态资源

在这里插入图片描述

  1. index.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    		<meta name="description" content="">
    		<meta name="author" content="">
    		<title>Signin Template for Bootstrap</title>
    		<!-- Bootstrap core CSS -->
    		<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    		<!-- Custom styles for this template -->
    		<link th:href="@{/css/signin.css}" rel="stylesheet">
    	</head>
    
    	<body class="text-center">
    		<form class="form-signin" th:action="@{/user/login}">
    			<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
    			<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
    			<!--如果msg的值为空就不显示-->
    			<p style="color:red" th:text="${msg}" th:if="${not #strings.isEmpty(msg) }"></p>
    
    			<label class="sr-only" th:text="#{login.username}">Username</label>
    			<input type="text" class="form-control" name="username" th:placeholder="#{login.username}" required="" autofocus="">
    			<label class="sr-only" th:text="#{login.password}">Password</label>
    			<input type="password" class="form-control" name="password" th:placeholder="#{login.password}" required="">
    			<div class="checkbox mb-3">
    				<label>
              <input type="checkbox" value="remember-me" > [[#{login.remember}]]
            </label>
    			</div>
    			<button class="btn btn-lg btn-primary btn-block" type="submit" >[[#{login.btn}]]</button>
    			<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
    			<!--连接请求-->
    			<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
    			<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
    		</form>
    	</body>
    </html>
    
  2. list.html

    <!DOCTYPE html>
    <!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
    <html lang="en" xmlns:th="http://www.themeleaf.org">
    
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    		<meta name="description" content="">
    		<meta name="author" content="">
    
    		<title>Dashboard Template for Bootstrap</title>
    		<!-- Bootstrap core CSS -->
    		<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    
    		<!-- Custom styles for this template -->
    		<link th:href="@{/css/dashboard.css}" rel="stylesheet">
    		<style type="text/css">
    			/* Chart.js */
    			
    			@-webkit-keyframes chartjs-render-animation {
          
    				from {
          
    					opacity: 0.99
    				}
    				to {
          
    					opacity: 1
    				}
    			}
    			
    			@keyframes chartjs-render-animation {
          
    				from {
          
    					opacity: 0.99
    				}
    				to {
          
    					opacity: 1
    				}
    			}
    			
    			.chartjs-render-monitor {
          
    				-webkit-animation: chartjs-render-animation 0.001s;
    				animation: chartjs-render-animation 0.001s;
    			}
    		</style>
    	</head>
    
    	<body>
    	<div th:replace="~{commons/commons::topbar}"></div>
    <!--	<div th:insert="~{commons/commons::topbar}"></div>-->
    		<div class="container-fluid">
    			<div class="row">
    
    				<div th:replace="~{commons/commons::sidebar(active='list.html')}"></div>
    
    				<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    					<h2><a class="btn btn-sm btn-success" th:href="@{/emp}">添加员工</a></h2>
    					<div class="table-responsive">
    						<table class="table table-striped table-sm">
    							<thead>
    								<tr>
    									<th>id</th>
    									<th>lastName</th>
    									<th>email</th>
    									<th>gender</th>
    									<th>department</th>
    									<th>birth</th>
    									<th>操作</th>
    								</tr>
    							</thead>
    							<tbody>
    								<tr th:each="emp:${emps}">
    									<td th:text="${emp.getId()}"></td>
    									<td th:text="${emp.getLastName()}"></td>
    									<td th:text="${emp.getEmail()}"></td>
    									<td th:text="${emp.getGender()==0?'':''}"></td>
    									<td th:text="${emp.department.getDepartmentName()}"></td>
    									<!--#dates.format可以转换成日期格式-->
    									<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}"></td>
    									<td>
    <!--										<a class="btn btn-sm btn-primary" th:href="@{/upemp(id=${emp.getId()})}">编辑</a>-->
    										<a class="btn btn-sm btn-primary" th:href="@{/upemp/} + ${emp.getId()}">编辑</a>
    										<a class="btn btn-sm btn-danger" th:href="@{/deleteEmp/} + ${emp.getId()}">删除</a>
    									</td>
    								</tr>
    
    							</tbody>
    						</table>
    					</div>
    				</main>
    			</div>
    		</div>
    
    		<!-- Bootstrap core JavaScript
        ================================================== -->
    		<!-- Placed at the end of the document so the pages load faster -->
    		<script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js"></script>
    		<script type="text/javascript" src="asserts/js/popper.min.js"></script>
    		<script type="text/javascript" src="asserts/js/bootstrap.min.js"></script>
    
    		<!-- Icons -->
    		<script type="text/javascript" src="asserts/js/feather.min.js"></script>
    		<script>
    			feather.replace()
    		</script>
    
    		<!-- Graphs -->
    		<script type="text/javascript" src="asserts/js/Chart.min.js"></script>
    		<script>
    			var ctx = document.getElementById("myChart");
    			var myChart = new Chart(ctx, {
          
    				type: 'line',
    				data: {
          
    					labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    					datasets: [{
          
    						data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
    						lineTension: 0,
    						backgroundColor: 'transparent',
    						borderColor: '#007bff',
    						borderWidth: 4,
    						pointBackgroundColor: '#007bff'
    					}]
    				},
    				options: {
          
    					scales: {
          
    						yAxes: [{
          
    							ticks: {
          
    								beginAtZero: false
    							}
    						}]
    					},
    					legend: {
          
    						display: false,
    					}
    				}
    			});
    		</script>
    	</body>
    </html>
    
  3. dashboard.html

    <!DOCTYPE html>
    <!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
    <html lang="en" xmlns:th="http://www.themeleaf.org">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    		<meta name="description" content="">
    		<meta name="author" content="">
    
    		<title>Dashboard Template for Bootstrap</title>
    		<!-- Bootstrap core CSS -->
    		<link th:href="@{/css/bootstrap.min.css}" rel="
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值