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>
- 然后把网页模板都导入到templates文件夹下
2.把静态资源导入到static文件夹下
3.模拟数据库操作
-
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(); } }
-
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); } }
首页实现
-
扩展首页的mvc配置
//添加一个视图控制器,来控制跳转的方式 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); registry.addViewController("/main.html").setViewName("index"); }
-
需要关闭thymleaf引擎的缓存机制
#关闭thymleaf缓存机制 spring.thymeleaf.cache=false
-
网页表头需要添加thymleaf的命名空间
xmlns:th="http://www.thymeleaf.org"
-
需要把网页改成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')}">中文</a> <a class<