绑定语句

时间:2017-03-07 10:14:49

标签: java spring configuration mybatis

我只是按照mybatis的官方文档编写了一个演示。我只使用注释时工作正常。但是当我尝试混合使用注释和xml映射器文件时,它会出现异常"无效的绑定语句"。好吧,虽然我可能做错了,所以我回到代码只是使用没有任何注释的xml映射器文件,但仍然出错。  我刚读过官方文档很多次仍然找不到任何有用的信息。你能不能帮助我分析一下到底发生了什么,非常感谢,因为它让我如此疯狂。

以下是代码:

@Configuration
@MapperScan("mapper")
@EnableTransactionManagement
public class MyBatisConfig
{
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception
    {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(MysqlDS());
        //sqlSessionFactory.setTypeAliasesPackage("entity");
        return sqlSessionFactory;
    }

    @Bean
    public DataSource MysqlDS()
    {
        //PooledDataSource ds = new PooledDataSource();
        UnpooledDataSource ds = new UnpooledDataSource();
        ds.setDriver(DBConfig.driverClass());
        ds.setUrl(DBConfig.Url());
        ds.setUsername(DBConfig.username());
        ds.setPassword(DBConfig.password());
        return ds;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager()
    {
        return new DataSourceTransactionManager(MysqlDS());
    }

映射器:

public interface MemberMapper
{
    @Select("SELECT * FROM r_test")
    @Results({
            @Result(column = "username", property = "username")
    })
    List<Tester> memberList();

    @Insert("INSERT into r_test (username, password) VALUES (#{username}, #{password})")
    void create(Tester tester);

    Tester getMember(Integer id);
}

实体

private int id;
private String username;
private String password;
private String time;

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="mapper.MemberMapper">

    <select id="getMember" resultType="Tester" parameterType="java.lang.Integer">
        select * from r_test where id=#{id}
    </select>

</mapper>

1 个答案:

答案 0 :(得分:0)

您可以尝试在XXMapper.java中添加@Mapper批注,如下所示:

@Mapper
public interface UserMapper {
    
    int deleteByPrimaryKey(Integer id);
    int insertSelective(User record);
    User selectByPrimaryKey(Integer id);
    int updateByPrimaryKeySelective(User record);
    int updateByPrimaryKeyWithBLOBs(User record);
    int updateByPrimaryKey(User record);
    
    @Select("select * from t_user where username=#{username}")
    User selectUserByUsername(String username);
    void insertUser(@Param("user") User user);
    User selectUserByUsernameAndPassword(HashMap<String, Object> hashMap);
    User selectUserById(int id);    
}