mybatis连接数据库出错获取不到SQLsession
采用mybatis连接数据库时候出现的问题描述:
数据库连接配置正确,mybatis-config数据库等部分配置均正确,连接数据库是OK的
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
此时通过MybatisUtil测试可以获取到sqlsession,并打印出来;
然后配置jopo及对应的xml文件正确,此时发现将jopo对应的xml文件注册到mybatis-config时,添加如下信息后
<mappers>
<mapper resource="com/yusys/entity/StudentMapper.xml"/>
</mappers>
反而获取不到数据库连接,得不到sqlsession,打印出来为null;
最终对比检查发现问题出在jopo对应配置文件中,如下标黄部分
<mapper namespace="com.yusys.entity.StudentMapper">
<select id="selectById" parameterType="java.lang.Integer" resultType="Student">
select * from student where id=#{id}
</select>
</mapper>
该属性resultType应该对应完全限定名,否则找不到对应的类,不能形成映射,故而,在通过代码测试
InputStream is = MybatisUtil.class.getClassLoader()
.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
session = sf.openSession();
的时候,加载mybatis-config.xml到mappers的注册信息时候不能正确加载,故而导致上面is流错误,从而后续session也就不能获取。
更改如下,在mybatis-config.xml中添加别名与Student对应上,如下:
<typeAliases>
<typeAlias type="com.yusys.entity.Student" alias="Student"/>
<package name="com.yusys.entity"/>
</typeAliases>
此时问题解决。
另一种办法是将属性resultType更改为其完全限定名
同时需要注意的是:
mybatis-config.xml配置文件配置时,要注意节点顺序
顺序同错误提示信息一致:
元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?
,databaseIdProvider?,mappers?)"
例如:将typeAliase写在properties前面打印session同样会是null
<configuration>
<typeAliases>
<typeAlias type="com.yusys.entity.Student" alias="Student"/>
<package name="com.yusys.entity"/>
</typeAliases>
<properties resource="db.properties"></properties>
有时eclipse还会提示错误,configuration下面会出现红色曲线进行提示
尤其要注意的是,在mybatis编写的pojo.xml和mybatis-config.xml的编写过程中一定要细心对应起来,不能多也不能少,否则均会报空,也就是session均会为null,比如别名里面配置一个Emp,但是实际src的POJO包下和pojo.xml中均没有体现,则就会报null,在这里一定要关联考虑,要删删干净,要加加全面,不得马虎。
比如
<typeAliases>
<typeAlias type="com.yusys.entity.Student" alias="Student"/>
<typeAlias type="com.yusys.entity.udent" alias="udent"/> ============多余添加一个
<package name="com.yusys.entity"/>
</typeAliases>
运行结果会是
开启的+++session:null
Exception in thread "main" java.lang.NullPointerException ===========空指针异常,因为多余的POJO不存在,无法编译通过
at com.yusys.test.Test.getStuById(Test.java:16)
at com.yusys.test.Test.main(Test.java:31)
在注册表中也不能多,如下:
<mappers>
<mapper resource="com/yusys/dao/StudentMapper.xml"/>
<mapper/> 这个地方多出的一个空的映射注册表也不行,同样运行时候获取不到session
</mappers>
mybatis连接数据库出错获取不到SQLsession的更多相关文章
- mybatis根据property获取column
mybatis根据property获取column mybatis根据类的属性获取xml文件中对应的column mybatis获取xml文件中property对应的column >>&g ...
- Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法
一.Mybatis中用#{}和${}获取输入参数的区别 1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值.例如 <mapper namespace=&q ...
- Mybatis抛出 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@f54509]异常
今天在做Springmvc和spring 时 mybatis 是抛出异常 Closing non transactional SqlSession [org.apache.ibatis.session ...
- SpringBoot+MyBatis连接数据库
SpringBoot通过MyBatis连接数据库有2种方法: 1.注解 2.XML文件 1.注解 1.构建项目 2.添加依赖: <dependencies> <dependency& ...
- mybatis整合spring获取配置文件信息出错
描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...
- Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
- (转) Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
- mybatis与spring的整合(使用sqlSession进行crud)
上次介绍了用接口的方法极大的节省了dao层,只需通过 配置文件和接口就可以实现,这次介绍的是通过splsession来实现dao,这种方法比较灵活: 先不说,上配置文件: 1.web.xml < ...
- JDBC与Druid简单介绍及Druid与MyBatis连接数据库
序言 java程序与数据建立连接,首先要从jdbc说起,然后直接上阿里认为宇宙最好的数据库连接池druid,然后再说上层程序对象与数据源映射关联关系的orm-mybatis. JDBC介绍 JDBC( ...
随机推荐
- 07 DTFT
DTFT 连续时间傅里叶变换(CTFT) 连续时间傅里叶变换的定义为: \[ X(j\Omega)=\int_{-\infty}^{\infty}x_a(t)e^{-j\Omega t}dt \] 其 ...
- pip使用技巧
1. pip install 'easydict==1.6' --force-reinstall 强制安装制定version 2. pip install git+https://github.com ...
- Vue中全局监听键盘事件
全局监听enter键,是把监听事件绑定到document上 常用的keyCode键盘编码在这里:https://www.cnblogs.com/wbyixx/p/12029508.html creat ...
- sublime3常用环境配置
如何设置侧边栏颜色 Ctrl+Shift+P -> install -> 搜索安装包SyncedSidebarBg,自动同步侧边栏底色为编辑窗口底色. 设置快捷键让html文件在浏览器窗口 ...
- Codeforces Global Round 4E(字符串,思维)
#include<bits/stdc++.h>using namespace std;string s,a,b;int main(){ cin>>s; int n=s.size ...
- Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上
原文:http://daikainan.iteye.com/blog/1407355 实现Button悬浮于与SurfaceView之上实现 先看效果: 注意:你实现的SurfaceView和andr ...
- ubunut18.04 下安装 gitlab ce版,使用清华源
gitlab官方的ubuntu安装说明 https://about.gitlab.com/install/#ubuntu 该安装说明介绍的是gitlab-ee版本 按照该说明也能安装gitlab-ce ...
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- Python入门之元组
一.什么是元祖 元祖是不可变类型(列表是可变类型) 为什么要设计元祖这样不可变类型?因为一旦创建了不可变类型的对象,对象内部的所有数据就不能被修改了,这样避免了 由于修改数据导致的错误.此外,对于不可 ...
- 中山普及Day13——普及
又是迷之自信的说...估的230,考的50整,我欲上天呐!!! T1:深渊(怕不是黑暗种族聚集地???) 思路:动归.而且是简单动归.转移方程:Fi,j=max(Fi-1,j,Fi,j,Fi-1,(j ...