因为练手的原因吧,我选择用springboot去完成一个项目,就选择了学生管理系统。
感觉springboot的项目入手要比框架的项目要更加的简便下面说说这两者的不同吧。
springboot项目
首先我认为这两者的不同在于SSM框架是最基础的,而springboot使用来管理三大框架的,在三大框架的基础上进行应用的,最能体现这一点的就是配置文件,对于三大框架,配置文件是四个,mybatis,spring,springmvc,log4j,每个人的习惯不一样,整合的也就不一样,我在后面会对于框架进行整理。但是我们在书写springboot的时候我们配置文件只有一个application.yml,在里面去直接整合spring,springMVC,mybatis,还有就是直接去将数据库的配置文件也整合了进去。
这就是我的application.yml的配置文件,里面存了我的数据库的url,是因为用户名密码,还有就是我的mybatis的包的读取的地址,还有就是前端显示的时候的前缀和后缀,还有就是springboot不需要tomcat,他是自己有一个tomcat,所以会有一个显示端口号的地方。
springboot项目的具体制作
首先进行配置稳健的设置,配置好配置文件,
spring:
datasource:
url: jdbc:mysql://localhost:3306/schoolweb?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true
username: uuuu
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
typeAliasesPackage: com.test.pojo
mapperLocations: classpath:mappers/*.xml
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
server:
port: 8080
其次是将我们的依赖全部导入,只是导入我们能够使用的到的就可以了,数据库连接池,jstl,mysql,等等一些这里我就不粘了。
然后就是逆向工程,使用逆向工程生成自己所用的表,逆向工程是用来生成pojo和mapper。pojo里面存放的是我们的的Javabean,其实就是我们的一个个对象,mapper就是mybatis生成的sql语句,用来将我们常用的sql语句表示出来,我们在调用的时候可以直接去使用这个方法。
逆向工程的话也是上来需要先去修改配置文件
在下面需要去填写声粗鞥的mapper和pojo所在的包
写好自己需要的路径就好了,然后就是最后面的生成的表的名字
这是我需要去使用的表,他会自动生成选定的表,
这个就是生成pojo和mapper的实现类,我们只需要去实现这个类就可以生成pojo和mapper了,这样我们的配置文件就完成了。
然后就是我们如何去完成这个项目了,首先我们需要去先完成一个这样子的目录结构
各种的分包我就不讲了,毕竟都有基础,我讲讲springboot不一样的吧
首先项目名.Application.Java这个文件是我们springboot的启动类,我们要在这里面让他去扫描我们的mapper,
controller
首先需要在我们controller层的文件上面添加注释表名这个是controller,然后使用spring的控制反转IOC主动地去生成service层的对象的实现类,
@RequestMapping(value="adminslogin.action")
这一句是我们的路径用来进行controller操作的,然后就是我们主动去生成一个ModelAndView,modelAndView.setViewName("alogin");
这一句是用来标识jsp的路径的但是要注意这个是在你前面写的配置文件的基础之上的,如果你在前边配置的基础文件上有不同的路径,在这里是需要去更改的,然后返回这个ModelAndView,就可以跳转到你设定好的页面了。
这是一个登录检查的页面,在前端设置上一个表单,给定input以一个指定的表中数据的名字就会在跳转的时候主动去生成了一个相应的对象,就像我上面标出来的一样,然后如果这个不对的话可以去给前端一个返回的对象就是使用了modelAndView.addObject("Error", "用户名或密码错误");
将我们的字符串给了Error并且这个变量在切换页面的时候就会消失。
service
下面说一说service层,这个层主要是进行一个业务的操作,使用sql语句从数据库中进行查询,我们类的上方标注出一个Service表示是一个service的类,然后就是AdminsExample adminsExample=new AdminsExample();
生成一个AdminsExample的对象,
adminsExample.createCriteria().andAdminidEqualTo(admins.getAdminid()).andAdminpasswordEqualTo(admins.getAdminpassword());
这个对象.createCriteria()就是相当于一个where条件,后面的意思就说是adminid和我们传入的相等并且adminpassword也是和我们的传入的相等,然后使用adminsMapper.selectByExample(adminsExample);
生成了一集合对象,其实这个mapper还有很多的方法,增删改差都有,我们只不过是举了个例子。
前端
在这里我主要是说一下我们进行前端的页面跳转的东西
<a href="http://localhost:8080/teacherLogin.action"> <span class="fa fa-sign-out" aria-hidden="true"></span>Teacher</a>
这一句就是实现的超链接,直接连接到我们的action路径,我们在浏览器输入的也是类类似的东西,http://localhost:8080/teacherLogin.action
只是更换我们写的action而已。
<form action="${pageContext.request.contextPath}/logincheck.action" method="post">
<div class="">
<p>User Name </p>
<input type="text" class="name" name="studentid" required="" />
</div>
<div class="">
<p>Password</p>
<input type="password" class="password" name="studentpassword" required="" />
</div>
<div>${Error}
</div>
<input type="submit" value="Login">
</form>
这就是一个表单的提交,然后就是这个表单也是提交到指定的action,请注意的是input的属性中的name属性要和你在数据库中表的属性一致。
出现的bug
问题
运行了逆向工程的文件并没有出现应该出现的包和pojo还有mapper,但是路径没有问题。
处理办法
这个是因为在配置文件中没有选择导入的表
出现问题
一切的配置都没有问题。但是运行出现问题
处理办法
在pom.xml文件添加以下代码,再运行就OK:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
具体实现还没有弄明白,不过在此记录一下。
出现问题
引入service出现问题项目直接停止
2019-11-06 08:38:29.165 INFO 8436 — [ main] com.test.MyfirstSpringbootApplication : Starting MyfirstSpringbootApplication on DESKTOP-ORGL9HU with PID 8436 (F:\eclipse201903\Myfirst-springboot\target\classes started by zwd in F:\eclipse201903\Myfirst-springboot)
2019-11-06 08:38:29.169 INFO 8436 — [ main] com.test.MyfirstSpringbootApplication : No active profile set, falling back to default profiles: default
2019-11-06 08:38:30.963 INFO 8436 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-06 08:38:30.975 INFO 8436 — [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-06 08:38:30.976 INFO 8436 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-06 08:38:31.281 INFO 8436 — [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2019-11-06 08:38:31.290 INFO 8436 — [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-06 08:38:31.290 INFO 8436 — [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2042 ms
2019-11-06 08:38:31.360 WARN 8436 — [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userCon’: Unsatisfied dependency expressed through field ‘userser’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.test.service.UserSer’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-11-06 08:38:31.364 INFO 8436 — [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-11-06 08:38:31.387 INFO 8436 — [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2019-11-06 08:38:31.519 ERROR 8436 — [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
Field userser in com.test.controller.UserCon required a bean of type ‘com.test.service.UserSer’ that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ‘com.test.service.UserSer’ in your configuration.
问题解决
解决办法加上@service
剩下的一些问题几=就是一些空指针的问题,这些问题往往是在删除和修改的时候没有考虑到数据是否存在,所以在进行删除和修改数据的时候要去考虑对于数据存在与否的一个验证,如果大家遇到了别的问题,希望一起讨论。