mybatis-guice mysql无法加载jdbc驱动程序

时间:2016-08-04 16:55:06

标签: java mysql guice mybatis

我正在尝试将 mybatis-guice mysql 一起使用,但我一直在抓住下一个例外:

查询数据库时出错。原因:java.sql.SQLException:在UnpooledDataSource上设置驱动程序时出错。原因:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

原因:java.sql.SQLException:在UnpooledDataSource上设置驱动程序时出错。原因:java.lang.ClassNotFoundException:org.hsqldb.jdbcDriver

以下是我尝试运行的代码:

public class DaoImpl implements Dao {
    @Inject
    private MyMapper mapper;

    public String getSomething() {
        return mapper.getSomething();
    }
}

public interface Dao {
    String getSomething();
}

public interface MyMapper {
    @Select("SELECT DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') as time")
    String getSomething();
}

Injector injector = Guice.createInjector(
        new MyBatisModule() {
            @Override
            protected void initialize() {
                install(JdbcHelper.MySQL);
                environmentId("development");

                bindDataSourceProviderType(PooledDataSourceProvider.class);
                bindTransactionFactoryType(JdbcTransactionFactory.class);
                addMapperClass(MyMapper.class);

                bindProperties(binder(), getMybatisProperties());
                bind(Dao.class).to(DaoImpl.class);
            }
        }
);

private Properties getMybatisProperties() {
    Properties myBatisProperties = new Properties();
    myBatisProperties.setProperty("JDBC.host", "127.0.0.1");
    myBatisProperties.setProperty("JDBC.port", "3306");
    myBatisProperties.setProperty("JDBC.schema", "my_schema");
    myBatisProperties.setProperty("JDBC.driver", "com.mysql.jdbc.Driver"));
    myBatisProperties.setProperty("JDBC.username", "root");
    myBatisProperties.setProperty("JDBC.password", "");
    myBatisProperties.setProperty("JDBC.autoCommit", "false");
    return myBatisProperties;
}

而不是我尝试运行: injector.getInstance(Dao.class).getSomething()

我尝试删除myBatisProperties.setProperty("JDBC.driver", "com.mysql.jdbc.Driver"));,但结果是一样的。

此外,在调试代码几个小时后,install(JdbcHelper.MySQL);应自行添加驱动程序。这个假设是否正确?

mapper.getSomething();

上抛出异常

思想???

1 个答案:

答案 0 :(得分:1)

你需要一个驱动程序mysql-connector-java或者验证驱动程序的版本以及lib中的put mysql-connector-java-bin.jar