当我尝试使用MyBatis执行简单查询时,这是堆栈跟踪:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.my.package.persistence.BrandMapper.getBrand
org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189)
org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43)
org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51)
com.sun.proxy.$Proxy25.getBrand(Unknown Source)
com.my.package.service.BrandService.getBrand(BrandService.java:18)
com.my.package.service.BrandService$$FastClassBySpringCGLIB$$1140c60a.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
com.my.package.service.BrandService$$EnhancerBySpringCGLIB$$ea6f89cd.getBrand(<generated>)
com.my.package.controller.HomeController.getBrands(HomeController.java:28)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我正在使用Javaconfig语法而不是XML配置。这是我的PersistenceConfig:
@Configuration
@EnableTransactionManagement
@MapperScan("com.my.package.persistence")
public class PersistenceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try {
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql//localhost:3306/db");
dataSource.setUsername("dbuser");
dataSource.setPassword("dbpassword");
} catch (Exception e) {
System.out.print(e);
}
return dataSource;
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.my.package.domain");
return sessionFactory;
}
}
这是我的控制器:
@Controller
public class HomeController {
private static Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
private BrandService brandService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/brands", method = RequestMethod.GET)
public String getBrands(Model model) {
model.addAttribute("brands",brandService.getBrand(1));
return "brands";
}
}
这是我的品牌映射器界面:
public interface BrandMapper {
Brand getBrand(int id);
Brand getBrandByName(String name);
List<Brand> getBrandList();
void addBrand(Brand brand);
void updateBrand(Brand brand);
void deleteBrand(int id);
}
这是我的BrandMapper 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">
<mapper namespace="com.my.package.persistence.BrandMapper">
<select id="getBrand" resultType="Brand" parameterType="int">
SELECT id, name
FROM brand
WHERE id = #{id};
</select>
<select id="getBrandByName" resultType="Brand" parameterType="String">
SELECT id, name
FROM brand
WHERE name = #{name};
</select>
<select id="getBrandList" resultType="Brand">
SELECT id, name
FROM brand;
</select>
<insert id="addBrand" parameterType="Brand">
INSERT INTO brand (id, name)
VALUE (#{id}, #{name})
</insert>
<update id="updateBrand" parameterType="Brand">
UPDATE brand
SET
name = #{name}
where id = #{id}
</update>
<delete id="deleteBrand" parameterType="int">
DELETE FROM brand
WHERE id = #{id}
</delete>
</mapper>
我做了一些研究,但没有一个解决方案对我有用。我的XML Mapper文件位于名为“com.my.package.persistence”
的包中的资源下有没有人知道这里有什么问题?
提前致谢
答案 0 :(得分:5)
尝试检查mybatis-conf.xml(您调用此文件的名称)文件,看看您是否有这样的xml映射器:
<mappers>
<mapper resource="BrandMapper.xml">
<mappers>
答案 1 :(得分:1)
如果你已经运行了数据库服务代码的JUNIT,它就可以工作了。因此我相信它是生成WAR文件时的问题,从您的配置* Mapper.java和* Mapper.xml指向同一位置,因此当war为gerenated时,java和xml文件的文件夹路径将为WEB-INF/classes/com/my/package/persistence
。但是* .xml文件不会复制到该位置。一种方法是配置构建脚本以复制xml文件或其他方式(我更喜欢)在资源目录中创建sqlmap目录并将所有xml映射文件复制到目录并使用{{1}指向mapperfile位置生成war文件后,确保sessionFactory.setMapperLocations
目录
答案 2 :(得分:1)
错误消息:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
很可能是由于错误的映射器查询语法。我有很多次这个问题,每次错误是由Mapper xml和接口文件中写入的查询的错误语法引起的。我建议你,
答案 3 :(得分:0)
我想也许你需要一个 @Alias 注释:
@Alias(&#34;品牌&#34) class Brand { ... }
这是我想到的第一件事,希望它有所帮助!
答案 4 :(得分:0)
从查询中删除;
:
<select id="getBrand" resultType="Brand" parameterType="int">
SELECT id, name
FROM brand
WHERE id = #{id}
</select>
答案 5 :(得分:0)
初始化sqlSessionFactory时应该提供映射器位置:
@Autowired
private ResourceLoader resourceLoader;
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
//sessionFactory.setTypeAliasesPackage("com.my.package.domain");
sessionFactory.setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).
getResources("classpath:path/to/mappers/*.xml"));
return sessionFactory;
}
在您的情况下,将&#34; classpath:path / to / mappers / * .xml&#34; 替换为 classpath:com / my / package / persistence / * .xml 强>
希望这会对你有所帮助。
答案 6 :(得分:0)
我有这个确切的错误消息,原来是mapper xml文件中的命名空间路径,我在java接口文件中更改了包名,但忘了更新maper xml中的包名,有点奇怪一。更新了包命名空间,嘿presto!
答案 7 :(得分:0)
您的映射器文件应与* Repo.java文件位于同一个包中。请检查mapper名称空间。
答案 8 :(得分:0)
只需用gradle clean build
固定。