SpringBoot模拟数据库开发

Spring boot模拟数据库开发

准备工作

  • 把准备的后台模板准备好,地址:

  • 链接:https://pan.baidu.com/s/13mNCQ18_nl6DHpxfKl4ZFw
    提取码:love

  • 导所需要的依赖

    <!--thymleaf引擎导入-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--官方导入-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--lombok导入-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
  1. 然后把网页模板都导入到templates文件夹下
    在这里插入图片描述

2.把静态资源导入到static文件夹下

在这里插入图片描述

3.模拟数据库操作

  1. pojo层创建

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    //部门表
    public class Development {
         
        private Integer id;
        private String developmentName;
    
    }
    
    @Data
    @NoArgsConstructor
    //员工表
    public class Employee {
         
        private Integer id;
        private String lastName;
        private String email;
        private Integer gender;
        private Development development;
        private Date birth;
    
        public Employee(Integer id, String lastName, String email, Integer gender, Development development) {
         
            this.id = id;
            this.lastName = lastName;
            this.email = email;
            this.gender = gender;
            this.development = development;
            this.birth=new Date();
        }
    }
    
  2. dao层创建

    @Repository
    public class DevelopmentDao {
         
        //模拟数据库管理数据
        public static Map<Integer,Development> developments=null;
        static {
         
            developments=new HashMap<Integer, Development>();
    
            developments.put(101,(new Development(101,"教育部")));
            developments.put(102,(new Development(102,"人事部")));
            developments.put(103,(new Development(103,"运营部")));
            developments.put(104,(new Development(104,"技术部")));
            developments.put(105,(new Development(105,"后勤部")));
        }
        //获取部门表的所有信息
        public Collection<Development> getDevelopmentAll(){
         
            return developments.values();
        }
    
        //通过获取id获得部门的信息
        public Development getDevelopmentById(Integer id){
         
            return developments.get(id);
        }
    }
    
    
    @Repository
    public class EmployeeDao {
         
        //模拟数据管理员工表
        public static Map<Integer, Employee> employees=null;
        static {
         
            employees=new HashMap<Integer, Employee>();
    
            employees.put(1001,new Employee(1001,"Aa","A1157627585@qq.com",0,new Development(101,"教育部")));
            employees.put(1002,new Employee(1002,"Bb","B1157627585@qq.com",1,new Development(102,"人事部")));
            employees.put(1003,new Employee(1003,"Cc","C1157627585@qq.com",0,new Development(103,"运营部")));
            employees.put(1004,new Employee(1004,"Dd","D1157627585@qq.com",1,new Development(104,"技术部")));
            employees.put(1005,new Employee(1005,"Ee","E1157627585@qq.com",0,new Development(105,"后勤部")));
        }
    
        //获得所有员工的信息
        public Collection<Employee> getEmployeeAll(){
         
            return employees.values();
        }
    
    
        //根据id获取员工的信息
        public Employee getEmployeeById(Integer id){
         
            return employees.get(id);
        }
    
        //主键自增
        public static Integer initEmployeeid=1006;
        //增加一个员工
        public void addEmployee(Employee employee){
         
            //如果添加的员工id为空
            if (employee.getId()==null){
         
                //那么就自动+1
                employee.setId(initEmployeeid++);
            }
    
            //把所添加的信息添加到数据库中
            employees.put(employee.getId(),employee);
        }
    
        //根据id删除一个员工
        public void deleteEmployee(Integer id){
         
            employees.remove(id);
        }
    
    }
    
    

首页实现

  1. 扩展首页的mvc配置

     //添加一个视图控制器,来控制跳转的方式
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
         
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/index.html").setViewName("login");
            registry.addViewController("/main.html").setViewName("index");
        }
    
  2. 需要关闭thymleaf引擎的缓存机制

    #关闭thymleaf缓存机制
    spring.thymeleaf.cache=false
    
  3. 网页表头需要添加thymleaf的命名空间

    xmlns:th="http://www.thymeleaf.org"
    
  4. 需要把网页改成thymleaf格式

    <!-- 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">
    

    所有页面的静态资源都需要使用thymleaf接管,

    其他也都是需要改,在线的连接不需要改

国际化

  • 首先需要修改File Encodings

在这里插入图片描述

  • 创建i18n文件夹,并且创建login.properties

在这里插入图片描述

在这里插入图片描述

  • 把网页修改成国际化

    <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>
    
    			<p style="color: red;" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
    			<label class="sr-only">[[#{login.username}]]</label>
    			<input name="username" type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    			<label class="sr-only">[[#{login.password}]]</label>
    			<input name="password" type="password" class="form-control" 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')}">中文<
  • 20
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值