欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料
  • 首页
  • |
  • 旅游知识
  • 电影知识
  • 数码知识
  • 生活百科
  • IT知识
  • 热门排行榜
  • 十大品牌榜
  • 产品推荐
  • 产品排行榜
365答案网 > IT知识 > 正文

SpringBoot模拟数据库开发

时间:2023-07-31
Spring boot模拟数据库开发 准备工作

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

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

导所需要的依赖

org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test

然后把网页模板都导入到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层创建

@Repositorypublic class DevelopmentDao { //模拟数据库管理数据 public static Map developments=null; static { developments=new HashMap(); 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 getDevelopmentAll(){ return developments.values(); } //通过获取id获得部门的信息 public Development getDevelopmentById(Integer id){ return developments.get(id); }}

@Repositorypublic class EmployeeDao { //模拟数据管理员工表 public static Map employees=null; static { employees=new HashMap(); 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 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格式

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

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

国际化

首先需要修改File Encodings

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

把网页修改成国际化

th:text:#{}来配置国际化信息

自定义一个组件LocaleResolver来控制语言的国际化

//解析请求@Overridepublic Locale resolveLocale(HttpServletRequest request) { //获取语言的请求 String language = request.getParameter("l"); Locale locale = Locale.getDefault();//如果没有所选的语言就是默认的 //如果获取的链接携带了国际化的参数 //如果选择的语言不为空 if(!StringUtils.isEmpty(language)){ //zh_CN String[] split = language.split("_"); //国家,地区 locale = new Locale(split[0], split[1]); } return locale;}

然后将自定义组件配置到spring容器中,也就是@Bean

//这个是为了实现国际化 public LocaleResolver localResolver(){ return new MyLocalResolver(); }

登录功能实现

因为数据库是伪造的,所以登录的时候无论什么都能登录进去

写一个登录的控制器LoginController

@Controllerpublic class LoginController { @RequestMapping("/user/login") public String login(@RequestParam("username")String username, @RequestParam("password")String pwd, Model model, HttpSession session) { System.out.println("debug==>"+username); if (!StringUtils.isEmpty(username)&&"123456".equals(pwd)) { session.setAttribute("loginUser", username); return "redirect:/main.html"; } else { model.addAttribute("msg", "密码或者用户名输入错误,请重新登录!"); return "login"; } }}

由于没有提示,所以需要在前端加一个标签来提示

展示登录页面

登录拦截器

创建一个拦截器方法LoginHandlerInterceptor,为了拦截那些没有登录就进入主界面的操作

public class LoginHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //登录成功后,应该有用户的session Object loginUser = request.getSession().getAttribute("loginUser"); if (loginUser==null){//没有登录 request.setAttribute("msg","没有权限,请先登录"); request.getRequestDispatcher("/index.html").forward(request,response); return false; }else { return true; } }}

把LoginHandlerInterceptor配置到spring容器中,@Bean.

//添加一个拦截器,为了拦截那些没有登录就进入主界面的操作 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("-->

增加员工展示

修改员工信息

添加修改员工信息功能

@GetMapping("/updateEmp/{id}")public String toUpdateEmp(@PathVariable("id") Integer id, Model model){ Employee employeeById = employeeDao.queryEmployeeById(id); System.out.println(employeeById); model.addAttribute("emp",employeeById); Collection developments = developmentDao.getDevelopments(); model.addAttribute("developments",developments); return "emp/update";}@PostMapping("/updateEmp")public String updateEmp(Employee employee){ System.out.println("update==>" + employee); employeeDao.updateEmplyee(employee); return "redirect:/emps";}

创建一个update.html

修改页面展示

删除及404处理

添加删除功能

@RequestMapping("/delete/{id}")public String Deleteemp(@PathVariable("id") Integer id){ employeeDao.deleteEmplyee(id); return "redirect:/emps";}

404处理页面只要放入到/templates/error文件夹下面,然后spring就会自动识别,如果跳转的页面不存在,就会自动跳转至此。


好了,一个springboot模拟数据库开发的网站就到此结束了,如果有什么不对的地方,请及时说出,我也会即使改正的。

上一篇:AndroidSocket
下一篇:【Java-泛型】泛型入门
相关推荐
  • 运算器的'组成部分不包括什么
  • MySQL数据库的安装与账户注册登陆说明
相关文章
  • 运算器的'组成部分不包括什么
  • 计算机中的所有信息都是以二进制方做什么
  • 电子数字计算机特点的是有哪些?
  • 数字计算机、模拟计算机和混合计算机,这种分类是依据是什么
  • 无网络安装python包
  • 基于kubeadm方式搭建k8s集群
  • 使用pipreqs导出python项目所需的环境
  • Vim编辑器(二)
  • Python基础练习--输出9*9乘法口诀表
  • 【硬件】问题:WDElements硬盘显示无法格式化——处理过程
  • 在Arduino上运行“脚本语言解释器”的几种方案
  • flask基础
  • [LinuxIMXRK]UART用户层与驱动层调用关系解析|CSDN创作打卡
  • 蓝桥杯Python练习题分解质因数
  • 保姆级VMware安装Ubuntu20.04.3虚拟机教程

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。