我是Mybatis的新手并尝试使用Spring实现mybatis但在启动tomcat时遇到以下运行时错误.
这是我的 pom.xml
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和应用环境:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="uactivityDS"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
错误是:
the resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionFactory
Run Code Online (Sandbox Code Playgroud)
但是该mybatis-spring-1.0.0.jar文件存在于war/web-inf/lib.
知道这种情况下发生了什么吗?
我正在使用Spring JDBC进行注释驱动的事务管理.
我想让Spring抛出异常,因为我错误地用@Transactional注释了一个插入/更新/删除的服务方法.
默认情况下,即使不在事务中,也可以插入/更新/删除数据.
我正在使用和的Spring JDBC示例.当我运行我的代码时,我看到以下错误即将发生.花了几个小时后,我找不到它的解决方案.请在这里指导什么错误.BeanPropertySqlParameterSourceSqlParameterSource
org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'employeeId': Invalid property 'employeeId' of bean class [com.spring.jdbc.model.Order]: Bean property 'employeeId' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray(NamedParameterUtils.java:342)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:348)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:211)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:226)
at com.spring.jdbc.dao.OrderDAOImpl.countOfOrders(OrderDAOImpl.java:51)
at com.spring.batch.OrderTest.testCountByEmployeeIdAndShipperId(OrderTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at …Run Code Online (Sandbox Code Playgroud) String SQL = "INSERT INTO Employee (name, age, salary) VALUES (:name,:age,:salary)";
Map namedParameters = new HashMap();
namedParameters.put("name", name);
namedParameters.put("age", age);
namedParameters.put("salary", salary);
namedParameterJdbcTemplate.update(SQL, namedParameters);
String SQL = "UPDATE Employee SET age = :age WHERE empid = :empid";
SqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("age", age);
namedParameters.addValue("empid", empid);
namedParameterJdbcTemplate.update(SQL, namedParameters);
Run Code Online (Sandbox Code Playgroud)
似乎Map和SqlParameterSource都是相同的。但是,为什么API开发人员添加了这些API?有没有使用Map或SqlParameterSource的特定方案,可以使执行速度更快?请给我解释清楚。提前致谢。
我有一个Spring Boot webapp连接到Postgres 9.6数据库.
我使用Spring JdbcTemplate来执行SQL语句.我的数据库中的每个表都有INSERT,CREATE和DELETE语句的触发器.这些触发器将受影响的行复制到历史记录表中.
我希望触发器还保存进行更改的用户的应用程序用户ID.
根据/sf/answers/922107511/,我可以通过让应用程序在每个事务开始时将当前用户id插入临时表并从临时表中读取触发器来实现我的目标.
在其他几个地方提到的类似方法是执行:
SET LOCAL application_name = "my_application_user",然后读取application_name触发器内部.同样,这必须在每笔交易开始时完成.
我正在寻找方法,它与业务代码正交(我不希望每个DAO显式设置用户ID),挂钩到每个事务的开始,以便在之前的任何其他语句之前运行特定的SQL语句相同的交易.
我需要这个用于隐式事务(单个调用JdbcTemplate)和使用Spring的@Transactional注释以声明方式划分的事务.
在 Spring 中,使用DriverManagerDataSource和SimpleDriverDataSource来创建一个新的数据源(给定它的 driverClassName、url、用户名和密码)有什么区别?
例如,使用 DriverManagerDataSource 您可以执行以下操作:
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Run Code Online (Sandbox Code Playgroud)
使用 SimpleDriverDataSource 您可以执行以下操作:
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
dataSource.setDriverClass(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Run Code Online (Sandbox Code Playgroud) 我对 Spring 世界很陌生。我使用application.properties来设置不同的端口值。如何注释 application.properties 文件中的某些代码?
我一直在尝试让 Quartz 在 Spring Boot 中使用 R2DBC 时工作。到目前为止,我还没有弄清楚如何做到这一点。这是因为当创建 JDBC 数据源时,它会尝试初始化我的 R2DBC 存储库,但它无法执行此操作,因为 R2DBC 本质上是反应性的,而 JDBC 本质上是阻塞的。
我考虑过的替代方案
相关 Gradle 依赖项
dependencies {
implementation("io.projectreactor.netty:reactor-netty:0.9.7.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
implementation("org.springframework.boot:spring-boot-starter-quartz")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
runtimeOnly("com.h2database:h2")
implementation("io.r2dbc:r2dbc-h2:0.8.3.RELEASE")
}
Run Code Online (Sandbox Code Playgroud)
我当前的石英配置
@Configuration
class QuartzConfig {
@Bean
@QuartzDataSource
fun dataSource(props: DataSourceProperties): DataSource {
return props.initializeDataSourceBuilder().type(HikariDataSource::class.java).build()
}
}
Run Code Online (Sandbox Code Playgroud)
相关R2DBC配置:
abstract class R2DbcConfiguration : AbstractR2dbcConfiguration() {
override fun getCustomConverters(): MutableList<Any> {
return mutableListOf(
// some custom converters
)
}
@Bean
fun connectionFactoryInitializer( …Run Code Online (Sandbox Code Playgroud) SQL 的一项简单优化是重用准备好的语句。您会产生一次解析成本,然后可以PreparedStatement在循环中重用该对象,只需根据需要更改参数即可。Oracle 的 JDBC 教程和许多其他地方都清楚地记录了这一点。
Spring 5 使用时JdbcTemplate似乎使这变得不可能。所有处理 s 的JdbcTemplate查询和更新方法都归结PreparedStatementCreator为一种execute方法。这是该方法的完整代码。
public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action)
throws DataAccessException {
Assert.notNull(psc, "PreparedStatementCreator must not be null");
Assert.notNull(action, "Callback object must not be null");
if (logger.isDebugEnabled()) {
String sql = getSql(psc);
logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
}
Connection con = DataSourceUtils.getConnection(obtainDataSource());
PreparedStatement ps = null;
try { …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Boot 应用程序 (v2.1.5),它使用 JPA (Hibernate) 连接到 Postgres DB。Spring Boot 使用 HikariCP 进行连接池。在我的生产环境中,我看到以下查询每隔几秒执行一次,无论数据库活动如何(几乎就像它们是某种运行状况检查?):
SET application_name = 'PostgreSQL JDBC Driver'
Run Code Online (Sandbox Code Playgroud)
我正在努力弄清楚为什么这些查询如此频繁地执行,以及是否可以避免它们,因为在我的本地环境中,仅在对数据库执行查询时才执行上述语句。我仍然不明白为什么,但与生产相比,它的频率较低且行为不同。
这些询问有必要吗?它们可以避免吗?谢谢。
更新:
以下是 Spring boot 应用程序使用 HikariCP 连接到的数据库收到的查询的屏幕截图。时间显示为“刚刚”,因为显示的所有查询仅相隔约 0.5 秒,并且都在“当前分钟”内。
spring-jdbc ×10
spring ×6
java ×5
jdbc ×3
postgresql ×2
datasource ×1
eclipse ×1
hikaricp ×1
kotlin ×1
mybatis ×1
quartz ×1
spring-aop ×1
spring-boot ×1
spring-data ×1