国内html网站欣赏,网站建设协议书怎么写,阿里巴巴能拿货在家里做的网站,智能网站建设加工解决Mybatis报Type interface *.*Mapper is not known to the MapperRegis 问题发现问题解决方法一#xff1a;检查Mapper文件的namespace路径是否正确方法二#xff1a;使用其他方法是否正确 问题发现
在学习MyBatis框架的时候#xff0c;不使用 XML 构建 SqlSessionFacto… 解决Mybatis报Type interface *.*Mapper is not known to the MapperRegis 问题发现问题解决方法一检查Mapper文件的namespace路径是否正确方法二使用其他方法是否正确 问题发现
在学习MyBatis框架的时候不使用 XML 构建 SqlSessionFactory调用Mapper的接口报类型接口没有注册。
示例代码如下
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource new PooledDataSource(com.mysql.jdbc.Driver, jdbc:mysql://localhost:3306/spring_data?characterEncodingutf-8useSSLfalse, root, 123456);TransactionFactory transactionFactory new JdbcTransactionFactory();Environment environment new Environment(development, transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration new org.apache.ibatis.session.Configuration(environment);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession sqlSessionFactory.openSession();UserMapper userMapper sqlSession.getMapper(UserMapper.class);ListUser users userMapper.selectByUser();// 关闭 SqlSessionsqlSession.close();}
}
public interface UserMapper {ListUser selectByUser();
}?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.mybatisstudy.dao.UserMapperselect idselectByUser resultTypecom.example.mybatisstudy.Userselect * from user;/select
/mapper网上说都是命名空间的错误发现命名空间也没问题。然后进行一系列的问题排查。
问题解决
方法一检查Mapper文件的namespace路径是否正确
这也是网上解决方法最多的一种貌似大部分人都是遇到这种错误。
先确认映射的类名是否正确。在确认包路径使用的.点com.*.*.Object而不是/左斜杠com/*/*/Object或\右斜杠com\*\*\Object如果错误请修改为.点。如果鼠标悬浮到路径上显示下划线说明配置正确。
如图所示
方法二使用其他方法是否正确
后面切换到使用XML 中构建 SqlSessionFactory方法示例代码如下
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource new PooledDataSource(com.mysql.jdbc.Driver, jdbc:mysql://localhost:3306/spring_data?characterEncodingutf-8useSSLfalse, root, 123456);TransactionFactory transactionFactory new JdbcTransactionFactory();Environment environment new Environment(development, transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration new org.apache.ibatis.session.Configuration(environment);configuration.addMapper(UserMapper.class);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession sqlSessionFactory.openSession();ListUser selectByUser sqlSession.selectList(selectByUser);// 关闭 SqlSessionsqlSession.close();}
}运行成功如图所示 然后逐个代码排除根据错误提示未注册发现少了configuration.addMapper()方法的代码加上后运行成功。
示例代码如下
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource new PooledDataSource(com.mysql.jdbc.Driver, jdbc:mysql://localhost:3306/spring_data?characterEncodingutf-8useSSLfalse, root, 123456);TransactionFactory transactionFactory new JdbcTransactionFactory();Environment environment new Environment(development, transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration new org.apache.ibatis.session.Configuration(environment);configuration.addMapper(UserMapper.class);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession sqlSessionFactory.openSession();UserMapper mapper sqlSession.getMapper(UserMapper.class);ListUser users mapper.selectByUser();// 关闭 SqlSessionsqlSession.close();}
}问题解决。
最开始看官网给的代码片段以为可以去掉addMapper的步骤后来才想起不注册的话怎么获取呢啧~还是太粗心了呀