从0开始用java+mysql+Thymeleaf+mybatis搭建一个简单的班级管理系统 历程记录

 

已停更,新坑javaWeb项目中...

 

由于经常要收文件,再加上想要学习并练习一下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=false
application.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"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <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>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>




    </dependencies>


    <build>

        <plugins>

            <plugin>
                <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>

                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/mybatis/mybatis-generator.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>


                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>

                </configuration>
            </plugin>
        </plugins>



        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.html</include>
                    <include>**/*.css</include>
                    <include>**/*.js</include>
                    <include>**/*.jpg</include>
                    <include>**/*.png</include>
                    <include>**/*.ico</include>
                    <include>**/*.gif</include>
                </includes>
                <filtering>true</filtering>
            </resource>

            <resource>
                <!-- 设定主资源目录  -->
                <directory>src/main/java</directory>

                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
                <includes>

                    <include>**/*.xml</include>
                </includes>

                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
                <excludes>
                    <exclude>**/*.yaml</exclude>
                </excludes>

                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,指定处理后的资源文件输出目录,默认是${build.outputDirectory}指定的目录-->
                <!--<targetPath>${build.outputDirectory}</targetPath> -->

                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>

</project>
pom.xml

2022年5月18日

  • 打包出错

在用postman测试注册请求的时候一直报错,说找不到方法。通过不断地查阅以及尝试,最后发现是因为mybatis文件夹里面的UserMapper.xml没有打包进项目内运行,打包后又进行了直接的页面访问测试,又提示找不到页面,最后发现也是没有打包html和css进项目。

在pom.xml中添加了以下代码用来打包UserMapper.xml以及html、css、js等等文件。

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.html</include>
                    <include>**/*.css</include>
                    <include>**/*.js</include>
                    <include>**/*.jpg</include>
                    <include>**/*.png</include>
                    <include>**/*.ico</include>
                    <include>**/*.gif</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
View Code
  • html跳转格式 

测试了一下login和register的跳转,发现跳转报错:

 

经过N次尝试,发现在html中转跳的路径应该是<a href = "controller中@RequestMapping的路径">,而不是xxx.html。

比如我的login中转跳register:controller界面是,html文件名是,而Login.html中跳转Register,html写的是 <a href="register">注册</a>。如果写成<a href="Register.html">注册</a>就会找不到页面了。

 

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,网上查了一下,发现是因为 先定义了资源目录<directory>src/main/java</directory>,之后把目录改回<directory>src/main/resources</directory>,并且把UserMapping.xml迁移回resources/mapper/,这样每次更新只需要rebuild project就行了。这里要注意如果要重新生成mapper.xml,要先删除之前的xml,否则新生成的xml会追加到之前的文件,导致报错 Result Maps collection already contains value for com.example.demo.dao.MyUserMapper.BaseResultMap。

 2022年5月20日

  • 登录、注册验证

准备再做一个登录验证,写了两个js来用于登录验证和注册验证,弄了一大堆之后发现js有失效的情况,原来html里只能引用一个<script>标签。于是用了一个js引用全部的js。引入是引入了,但是好像没有效果。又调整结构,改变量尝试,最后发现是html的from表单的action没有改路径.....无语咯。再调整好之后测试了一下登录和注册,都能够正常使用了,也能验证。但是本来调用了登录的js函数发送请求后会有一个返回值回来,但是现在返回值不会再回到js的函数,而是直接以html的形式返回(比如我js函数本来是检测返回值是null或者是success再转跳其他页面,但是现在就直接网页显示success字符串了)。所以我直接把返回值的那个字符化去掉,让他转跳网页到登录了,登录转跳到主页也是一样。

 今天就这样吧,现在是 2022年5月20日00:31:22,睡觉。

 2022年5月20日

 开工

  • 处理font字体失效问题

在对登录成功后的index页面css设置的时候发现font失效,提示404no found,几番探索最后是加了下面的依赖解决了问题

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <nonFilteredFileExtensions><!--对字体进行加载-->
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
                <version>3.0.2</version>
            </plugin>
取消对font的加载忽略

 这是调好之后的index界面(登录进去后)。先去吃饭,下午对数据进行关联。2022年5月20日13:29:50

5.20下午做完核酸就玩去了(?)

然后晚上撸串 美滋滋嘿

2022年5月21日

  • 把数据库的用户名在前端显示。

上网找了一下相关的案例,尝试了好几个方法,修改了几十遍最后在2022年5月21日13:32:44成功!

总结一下,需要使用的关键语句:

     modelAndView = new ModelAndView("index");
        modelAndView.addObject("userName",findUser.getName() );
        return modelAndView;

解释一下,第一句创建视图并且指定为index的视图模型;第二句加上一个名字叫userName的key,value是我的controller里的对象(findUser)的名字(finUser.getName);第三句,返回该视图,也就是跳转到登录成功的页面。

成功登录后的显示效果。数据库

另外中间突然font字体又不加载了,上午查了一下 excludes 和  include 以及 <filtering>true</filtering>标签的用法,大概了解了一下是 除..外包含(excludes) 和包含(include)以及替换(filtering)的作用 ,但还不是很清楚。后面再补吧!

 

最近要准备一下期末考试,补一下课,会减慢项目进度了。不得不感叹一下,明明很简单的问题,但是自学因为太多不明白的地方就要走很多弯路,效率很低啊!但是最后解决的时候能达到自己想要的效果确实开心~实验课了,溜2022年5月21日13:46:02。

 2022年6月24日

考完试了,放假了。继续开整。

  • 添加拦截器,防止未登录的用户访问页面。

要写两个类,一个继承 HandlerInterceptor(配置拦截器要拦截或放行的页面),一个实现 WebMvcConfigurer(添加拦截器)。试了几次发现没用,最后是因为没有在实现 WebMvcConfigurer的类加@Configuration注解而导致的,加上后成功拦截。

 

18.34 出去溜达会。

2022年6月25日

今天本来准备加一个文件上传的功能,没想到文件上传的页面被拦截了,一看发现除了不能被拦截的页面全部都被拦截了。但是如果直接开放那么就会造成可以直接访问的效果。说到底就是要验证非登录用户不能登录,几番查找调试,最后选择的是通过session将登录用户的信息储存,如果直接访问是没有这个信息的。访问的时候检查session有没有这个信息即可。完美解决。(前面是登录后访问,后面是直接访问)

 

 2022年6月25日20:46:00 芜湖下班

2022年6月26日

下午打算把文件上传的功能写一下的,但是无奈上传页面没有写过,调js调了半天才好。而且后端的文件上传也没写过,不知道要怎么入手。今天暂时把页面加上了,明天再写好上传的功能。 2022年6月26日21:21:43

2022年6月27日

把文件上传的后端完成了,一波三四五六七八九折啊。刚刚开始去找各类模板,看怎么实现,找到一个跟我的比较契合的。改好之后报 java.lang.NoClassDefFoundError: org/apache/commons/fileupload/disk/DiskFileItemFactory 上网查找后得知还需要引入如下两个依赖,不然文件无法上传。但是第二个依赖报no found,试了很多方法都没有解决,直接去下载了jar包导入。

<!--                    文件上传-->
<!--                    此jar已下载到/demo/commons-fileupload.jar 正常引入报 no found-->
<!--                    &lt;!&ndash; https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload &ndash;&gt;-->
<!--                    <dependency>-->
<!--                        <groupId>commons-fileupload</groupId>-->
<!--                        <artifactId>commons-fileupload</artifactId>-->
<!--                        <version>1.4</version>-->
<!--                    </dependency>-->

                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>1.3.2</version>
                    </dependency>
View Code

 之后发现前端报端口异常,原因是返回需要 JSON {"code":"0"},但是没有找到JSONObject,只有JSONPObject,直接 import org.json.JSONObject;解决。

 

2022年6月27日11:36:13 下班

 2022年6月28日

今天没有想要做的板块了,大概想了一下,后两天应该做一个公告板块和后台板块出来吧。等后台板块出来了就可以写更多的内容了。

posted @ 2022-05-18 21:38  m2on  阅读(234)  评论(0编辑  收藏  举报