的MyBatis-mysql的吉斯无法加载JDBC驱动程序
问题描述:
我试图使用的MyBatis-吉斯与MySQL的但我守捉下一个异常:的MyBatis-mysql的吉斯无法加载JDBC驱动程序
错误查询数据库。原因: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或在lib验证驱动程序的版本,您是把MySQL连接器的Java-bin.jar
感谢您的回答,我会尝试,让你知道 – Igor
工作就像一个魅力:) – Igor