本文主要是介绍Type interface com.mapper.UserMapper is not known to the MapperRegistry,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在使用spring整合mybaties的时候,想测试自己写的sql语句是否生效。结果报错:类型接口com.xyz.mapper.UserMapper不为MapperRegistry所知
代码结构及测试如下展示:
mybaties的配置文件:mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><!-- 使用jdbc事务管理 --><transactionManager type="JDBC"/><!-- 数据库连接池 --><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url"value="jdbc:mysql://192.168.192.192:3306/test?characterEncoding=utf-8&useSSL=false"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments></configuration>
UserMapper.java
package com.xyz.mapper;import org.apache.ibatis.annotations.Select;/*** @author 周瑜*/
public interface UserMapper {@Select("select 'user'")String selectById();
}
配置类文件代码:AppConfig.java
package com.xyz;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;@ComponentScan("com.xyz.service")
@MapperScan("com.xyz.mapper")
public class AppConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setUrl("jdbc:mysql://192.168.192.192:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource());return sessionFactoryBean.getObject();}@Beanpublic SqlSessionTemplate sqlSession() throws Exception {return new SqlSessionTemplate(sqlSessionFactory());}}
Test.java文件
package com.xyz;import com.xyz.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.io.IOException;
import java.io.InputStream;public class Test {public static void main(String[] args) throws IOException {// 整合mybatiesAnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);SqlSession session = sqlSessionFactory.openSession();UserMapper mapper = session.getMapper(UserMapper.class);String s = mapper.selectById();System.out.println(s);}
}
执行测试类之后,代码报错如上图所示 。
解决方法:在得到一个sqlsession之前将UserMapper类加到sqlSessionFactory中即可解决
sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);
添加此行后允许结果如下
这篇关于Type interface com.mapper.UserMapper is not known to the MapperRegistry的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!