从0开始用java+mysql+Thymeleaf+mybatis搭建一个简单的班级管理系统 历程记录
由于经常要收文件,再加上想要学习并练习一下java的框架,正好有空闲的服务器。于是我决定搭建一个简易的班级管理系统,到本地运行没问题后部署到服务器使用。
前几天先开坑,把基本的配置完善了一下。
-
创建springboot基本项目结构
由于过了好几天了,又是第一次自己写,还有用了一些框架,后面又引了一些依赖,分不太清了,贴一下结构和依赖吧。
解释一下:dao和pojo里面是mybatis生成的实体类以及接口,controller(控制流程)、service(接口以及实现)、util这些结构层次和工具包要自己新建一下。
layui是前端的框架,mybatis内有service里生成的接口的实现(MyUserMapper.xml),static和templates分别存放html的css、js和img等等,templates用于存放html网页
以下为配置文件
spring.datasource.username=root spring.datasource.password=1234567 spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.jpa.hibernate.ddl-auto=none # ?? spring.thymeleaf.prefix=classpath:/templates/ # ???? spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.suffix=.html # ???????,?????????? spring.thymeleaf.cache=falseapplication.properties
spring: datasource: username: root password: 1234567 url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: type-aliases-package: com.example.demo.pojo # mapper-locations: classpath:mybatis/mapper/*.xml # mapper映射文件/application.yaml
<?xml version="1.0" encoding="UTF-8"?>pom.xmlxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.example demo 0.0.1-SNAPSHOT demo demo 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 mysql mysql-connector-java 8.0.15 org.mybatis.generator mybatis-generator-core 1.3.7 org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 src/main/resources/mybatis/mybatis-generator.xml true true Generate MyBatis Artifacts generate org.mybatis.generator mybatis-generator-core 1.3.2 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok src/main/java **/*.xml true src/main/resources **/*.xml **/*.properties **/*.html **/*.css **/*.js **/*.jpg **/*.png **/*.ico **/*.gif true src/main/java **/*.xml **/*.yaml true
2022年5月18日
-
打包出错
在用postman测试注册请求的时候一直报错,说找不到方法。通过不断地查阅以及尝试,最后发现是因为mybatis文件夹里面的UserMapper.xml没有打包进项目内运行,打包后又进行了直接的页面访问测试,又提示找不到页面,最后发现也是没有打包html和css进项目。
在pom.xml中添加了以下代码用来打包UserMapper.xml以及html、css、js等等文件。
src/main/java **/*.xml true src/main/resources **/*.xml **/*.properties **/*.html **/*.css **/*.js **/*.jpg **/*.png **/*.ico **/*.gif true
-
html跳转格式
测试了一下login和register的跳转,发现跳转报错:
经过N次尝试,发现在html中转跳的路径应该是,而不是xxx.html。
比如我的login中转跳register:controller界面是,html文件名是,而Login.html中跳转Register,html写的是 注册。如果写成注册就会找不到页面了。
2022年5月19日
-
把前端和后端注册到数据库连接起来。
通过前端的表格+js监控按钮的点击发送post请求来进行后端的注册,注册到数据库里。有一个卡了很久的点就是写好后端的post接收之后,前端提交后端一直没反应。后面又去查资料,看别人的写法,原本我是直接submit提交的,但是没有反应。看到网上有分享的html样例是用js监控按钮提交的,照着模板也写了一个,成功完成注册。
雀食是吃了没文化的亏啊!!!一个js卡了这么久。现在是 2022年5月19日00:32:08 睡觉!
开工
-
资源目录配置
突然想起来字段表里面少了password, alter table myuser modify password varchar(20) not null;添加字段后mybatis重新生成实体类,启动后报错找不到接口,因为新生成的Mapper接口没有添加@Mapper注解,所以没有扫描进去,加上@Mapper注解后正常启动。开始测试注册,发送请求后报错password没有默认值。多次尝试后发现是target里面的xml没有更新进去,maven-clean、maven-compile重新构建并在Mapper重新加上注解,然后启动注册,成功。后面再研究一下密码加密存储。
发现打包的时候总是不能打包resources里面的文件,研究了一下pom.xml,网上查了一下,发现是因为 先定义了资源目录
2022年5月20日
-
登录、注册验证
准备再做一个登录验证,写了两个js来用于登录验证和注册验证,弄了一大堆之后发现js有失效的情况,原来html里只能引用一个