记录一下学习spring -mybatis -spring mvc- spring boot 过程入坑时刻
资源导出问题
org.apache.ibatis.binding.BindingException: Type interface com.kuang.pojo.UseDao is not known to the MapperRegistry.
<!--每一个Mapper.xml都需要在mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/xxx/pojo/UserMapper.xml"></mapper>
</mappers>
maven由于它的约定大于配置,我们之后可以能遇到我们写的配置文件,无被导出或者生效的问题。
解决方案:(在pom.xml文件中添加)
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
方法名不对
Invaild bound statement (not found):com.xxx.dao.UserMapper.getUserList
解决方法:xml文件中的方法名ID需要和mapper文件中的方法名保持一致。
==返回类型错误 ==
Result Maps collection doees not contain value for vom.xxx.dao.TeacaherMapeer.Student…
找不到文件报错
.java.io.FileNotFoundException: src\jdbc.properties (系统找不到指定的文件。)
properties.load(new FileReader(new File("src\\jdbc.properties")));
解决方案:
法一:使用绝对路径
//方法一:使用绝对路径
properties.load(new FileReader(new File("E:\\JDBCDemo\\01-demo\\src\\jdbc.properties")));
方法二:文件读取
1.读取路径
//读取路径
ClassLoader classLoader = DBuntil.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path =res.getPath();
properties.load(new FileReader(path));
//读取文件流
InputStream inputStream = DBuntil.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(inputStream);
找不到驱动报错
java.sql.SQLException: No suitable driver found for “jdbc:mysql://localhost:3306/jdbcdemo”
错误代码:
driver=com.mysql.jdbc.Driver
url="jdbc:mysql://localhost:3306/demo"
username="XXXX"
password="XXXX"
//读取驱动
Class.forName("driver");
正确代码:
//!!!!不能加双引号
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=XXXX
password=XXXX
//读取驱动
Class.forName(driver);
错误分析:因为url、username、password加了“”符合
总结:不能加双引号
The injection point has the following annotations: - @org.springframework.beans.factory…==
错误分析:Autowired 根据类型去spring容器找,找不到那个类,就会报错
解决方案:
在相应的mapper类中加@Mapper标注让springboot根据标注去将mapper注入
@Mapper
public interface XXXMapper {
......
}
启动类加@MapperScan(value = “xx.xx.xx.dao”)
@SpringBootApplication
@MapperScan(value = "xx.xx.xx.dao")
public class XXXApplication {
public static void main(String[] args) {
SpringApplication.run(XXXApplication.class, args);
}
}
###The error may exist in sqlMapper/UsersMapper.xml
###The error may involve test.findUserById
###The error occurred while executing a query==
错误代码
#数据源类型
#spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
##数据源驱动名称
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##数据源路径
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/XXX?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
##数据源用户名
##spring.datasource.username: XXX
#spring.datasource.username=XXX
##数据源密码
#spring.datasource.password=XXX
正确代码
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/xx?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username =xxx
spring.datasource.password =XXXX
//错误原因:==数据库驱动类写错了==
错误分析:因为mysql驱动连接5版本和8版本有些区别
总结:
连接驱动地址不一样:
5版本com.mysql.jdbc.Driver
8版本的com.mysql.cj.jdbc.Driver
时区要求不一样:
5版本不要求时区设置
8版本必须要时区设置
Parameter ‘ids’ not found. Available parameters are [array]
接收数组
int deleteByids(@Param(“ids”) Long[] ids);
错误代码
<!--错误写法-->
<update id="deleteCoinvoiceInfoByCoids" >
update base set del_flag='1'
where del_flag='0'
and coid in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
${
item}
</foreach>
</update>
正确代码
<!--正确写法-->
<update id="deleteCoinvoiceInfoByCoids" >
update base set del_flag='1'
where del_flag='0'
and coid in
<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
${
item}
</foreach>
</update>
接收集合list
int deleteByids(@Param("ids") List<long> ids);
<!--错误写法-->
<update id="deleteCoinvoiceInfoByCoids" >
update base set del_flag='1'
where del_flag='0'
and coid in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
${
item}
</foreach>
</update>
<!--正确写法-->
<update id="delete" >
update base set del_flag='1'
where del_flag='0'
and coid in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
${
item}
</foreach>
</update>
错误分析:
当mybatis传入参数为list集合的时候;mybatis会自动把其封装为一个map;会以“list”作为key;每个元素的值作为value;格式为 Map<“list”,value>
当mybatis传入参数为数组的时候mybatis会自动把其封装为一个map;会以“array”作为key;每个元素的值作为value;格式为Map<“array”,value>
总结:mabaties在批量删除,接收数组或者集合时,写法是不同的
integer类型打印错误
[Ljava.lang.Integer;@4f19c297
错误分析:
“[” 表示一维数组
"[["表示二维数组
"L"表示一个对象
"java.lang.Interger"表示对象的类型
"@"后面表示该对象的HashCode
错误代码:
void deleteByCoids(@Param("ids") Long[] ids){
System.out.println(ids);
}
解决方案:
一、直接使用Arrays.toString()打印
System.out.println(Arrays.toString(ids));
二、变量循环
for(int i=0;i<ids.length;i++){
System.out.println(ids[i]);
}
警醒自我:踩坑天天有,写代码一定要仔细严谨