mybatis-guice mysql 未能加载 jdbc 驱动程序
mybatis-guice mysql failed to load jdbc driver
我正在尝试将 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();
抛出异常
想法???
您需要驱动程序 mysql-connector-java 或验证驱动程序的版本和您的 put mysql-connector-java-bin.jar 在库中
我正在尝试将 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();
想法???
您需要驱动程序 mysql-connector-java 或验证驱动程序的版本和您的 put mysql-connector-java-bin.jar 在库中