专栏前言
作为一名前端开发,为了扩展自身技能,准备入门级的学习一下java后端相关知识。虽然岗位是前端,但我觉得不能算是卷。
因为我们日常开发中几乎每天都要和后端同学打交道,如果能够掌握后端基本知识的话,我相信在前端开发工作中也会有很大作用。 技多不压身,如果这个过程让你感到痛苦那大可不必;反之,学习新事物的过程中能让自己更加自信、有成就感时,我觉得是有意义的!
记得是2年前学习过一个入门级的java项目,但现在可以说是忘记的差不多了。最近打算从0开始学习,并输出文章,方便自己日后复习查阅,也希望帮助到其他同学。
专栏往期推荐:
前端入门JAVA:基础开发环境配置
前端入门JAVA:创建第一个java项目
前端入门JAVA:Spring-web项目实现第一个本地接口
前端入门JAVA:Spring-web项目配置数据库连接
前端入门JAVA:Spring-web项目实现登录、注册接口
前言
在从0到1入门Spring-web
项目时遇到了一些报错问题,踩了很久的坑,最终得以解决。谨以此篇记录自己的学习过程,也希望可以帮助到其他同学。
报错 Resolyed [org. springframework. web.HttpMediaTypeNotSupportedException: Content type " text/plain; charset=UTF-8' not supported]
在测试注册接口时报错如下:
解决: 将 raw 切换为 json 方式进行请求
cj.jdbc.Driver
相关报错
查看 application.properties
文件中配置的数据库连接驱动类的值为:spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
原因:pom.xml
文件中的 mysql
不同版本需要对应不同的 driver class
写法
解决:
-
mysql版本8.X 对应:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
mysql版本5.X 对应:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
报错 Error creating bean with name 'sqlSessionFactory' defined in class path resource
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.IOException: Failed to parse mapping resource: 'file [/Users/sisi/Desktop/myWeb/my-serve-project/my-java-project/springboot-project/demo-maven/target/classes/mapper/UserMapper.xml]'
解决:
pom.xml
文件中的 org.mybatis.spring.boot
修改版本为 3.0.2
required a bean of type 'xxxx' that could not be found.
原因:配置了 mybatis
,但没有指定扫描的包
- 解决方案1
确保 Mapper
接口被扫描,在启动类中加注解:@MapperScan("com.xxx.xxx.mapper")
来扫描它
@SpringBootApplication
@MapperScan("com.example.demomaven.mapper")
public class DemoMavenApplication {
public static void main(String[] args) {
System.out.println("主应用类,启动文件...");
SpringApplication.run(DemoMavenApplication.class, args);
}
}
- 解决方案2(推荐)
确保UserMapper
接口上有正确的注解。 项目中使用了 MyBatis
,所以这里使用 @Mapper
注解。 直接在 xxxMapper.java
类上加 @Mapper
即可:
A component required a bean of type 'com.example.demomaven.service.UserService' that could not be found.
ps:组件需要类型为“com.example.demomaven.service”的bean。找不到UserService。
解决:
application.properties
配置文件中加入 mybatis
配置引入所有的 mapper/.xml文件
# mybatis 引入 mapper
#mybatis.mapper-locations=classpath:mapper/*.xml
java.lang.NoClassDefFoundError: org/springframework/aot/AotDetector
解决: org.mybatis.spring.boot
之前用的3.x版本,降级 2.x版本
Error creating bean with name 'userController'
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.demomaven.service.UserService' available: expected single matching bean but found 2: userServiceImpl,userService
这个报错的原因可能有多种,而我遇到这个问题就卡了很长时间。
- 原因1
xml中使用的
resultType
、 type等包路径错误
错误 xml 配置:
而我的包名为:
com.example.demomaven.xxx
- 解决方式1:
直接修改 xml内parameterType
、 type
对应类的正确目录路径即可。
- 解决方式2:
application.properties
配置文件中加入mybatis
配置别名
# mybatis 配置引入所有 mapper
mybatis.mapper-locations=classpath:mapper/*.xml
# 设置 mybatis 对应 User的别名
mybatis.type-aliases-package=com.example.demomaven.entity
我的实体类包路径为: com.example.demomaven.entity
这里设置包别名后,就可以在 MyBatis XML
映射文件中简化使用。
此时 UserMapper.xml
写法如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--映射 UserMapper 类-->
<mapper namespace="com.example.demomaven.mapper.UserMapper">
<resultMap id="BaseResultMap" type="User">
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="INTEGER" property="password" />
</resultMap>
<!-- 查询用户-->
<select id="queryByUsername" resultType="User">
SELECT * FROM `user` WHERE username = #{username}
</select >
<!-- 插入用户-->
<insert id="addUser" parameterType="User">
INSERT INTO `user` VALUES(#{username},#{password})
</insert>
</mapper>
此时 resultType
的值相当于: com.example.demomaven.entity.User
总结
后续遇到新的问题会持续更新,总结到这里,方便复习。