org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):db.UserMapper.xxx

时间:2016-12-30 06:01:47

标签: java xml orm mybatis dropwizard

在运行DROPWIZARD应用程序时面对异常

错误[2016-12-30 04:36:34,735] io.dropwizard.jersey.errors.LoggingExceptionMapper:处理请求时出错:6813de3aa499e307 ! org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):db.UserMapper。

@Path("/user/{username}")
 public class ExampleResource {

 private final SqlSessionFactory sessionFactory;

    public ExampleResource(SqlSessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @GET
    public User getUser(@PathParam("username") String username) {

        try (SqlSession session = sessionFactory.openSession()) 
        {
           UserMapper users = session.getMapper(UserMapper.class);
            //session.getConfiguration().addMapper(UserMapper.class);
            //UserMapper users = session.getMapper(UserMapper.class);
            return users.findByUsername(username);
        }

    }
}

UserMapper.xml

<mapper namespace="db.UserMapper" class="db.UserMapper">
<select id="findByUsername" resultType="User">
<![CDATA[
    select username,email
    from user
    where username = #{username}
]]>
</select>

<resultMap id="User" type="core.User">
    <id column="username" property="username" />
    <result column="email" property="email" />
</resultMap>

<insert id="addUser">
<![CDATA[
    insert into user (username, email)
    values (#{User.username}, #{User.email})
]]>
</insert>

user mapper.java

public interface UserMapper {

 User findByUsername(@Param("username") String username);

    void addUser(@Param("user") User user);
}

ConfigurationClass:

@Valid
@NotNull    

private DataSourceFactory datasourceFactory = new DataSourceFactory();

@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
        return this.datasourceFactory;
    }
public void setDatabase(DataSourceFactory database) {
    this.datasourceFactory = database;
}

2 个答案:

答案 0 :(得分:0)

当我们在主类中使用带有dropwizard的mybatis时,我们进行了初始化 mybatisbundle,当时提供mapper类所在的包名。 在下面的代码中突出显示。

Traceback (most recent call last):
  File "/usr/bin/anki", line 5, in <module>
    import aqt
  File "/usr/share/anki/aqt/__init__.py", line 12, in <module>
    from aqt.qt import *
  File "/usr/share/anki/aqt/qt.py", line 22, in <module>
    from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings
ImportError: No module named QtWebKit
“anki &” has ended`

答案 1 :(得分:-1)

我的问题是春天没有找到mapper.xml,但是以@Test可以找到。 我的XXXmapper.xml和XXXXmapper.java位于同一位置(src / main / java /..../)。 我将所有XXXmapper.xml移至“ src / main / resources / mapper”,然后在<property name="mapperLocations" value="classpath:mapper/*.xml" />中添加<bean id="sqlSessionFactory>
那就可以了!

也许您可以从Google搜索“有多少种方法配置有关mybatis的XXXmapper.xml”。 希望对您有所帮助:-)