Configure the data source through the properties configuration file. The code is as follows:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="3" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:config/db.properties</value> <!-- Note the path to write, the above write method to add classpath, path before can not have /, the following write method properties file to be in the WEB-INF directory, path before to have/ <value>/WEB-INF/test.properties</value> --> </list> </property> </bean> <bean id="dataSource" name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${db.jdbc.driverClassName}" /> <property name="url" value="${db.jdbc.url}" /> <property name="username" value="${db.jdbc.username}" /> <property name="password" value="${db.jdbc.password}" /> <property name="initialSize" value="${db.initialSize}"/> <property name="maxActive" value="${db.maxActive}"/> <property name="maxWait" value="${db.maxWait}" /> <property name="poolPreparedStatements" value="true" /> <property name="defaultAutoCommit" value="true" /> <property name="maxIdle" value="${db.maxIdle}" /> <property name="minIdle" value="${db.minIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="select 1 from dual"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="false"/> <property name="filters" value="stat"/> </bean>
Project startup error:
org.springframework.beans.TypeMismatchException…
java.sql.SQLException: ${db.jdbc.driverClassName}
After consulting the data, it is found that this configuration is caused by:
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xiaomu.**.dao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
Reason:
Mappercannerconfigurer has customized the initialization interface of the bean. During its initialization, the placeholder has not been initialized, resulting in an error when the properties value has not been injected
Solution:
Mapperscannerconfigurer has two options when injecting sqlsessionfactory. One is to directly inject sqlsessionfactory objects during spring configuration, and the other is to inject beans through names
So < property name=”sqlSessionFactory” ref=”sqlSessionFactory” /> Change to < property name=”sqlSessionFactoryBeanName” value=”mybatisSqlSessionFactory” /> Note that after the change, the original sqlsessionfactory name cannot be used for the value value, because it is injected through the name, so using sqlsessionfactory will still report an error
Note: sqlsessionfactorybeanname is a non mandatory label and can be configured without a single data source
Description:
Mappercannerconfigurer is similar to the mappers tag in that it scans mappers to determine the mapping relationship. The mapper configuration form is:
<mappers> <!-- sql file configuration way one: specify the file path location, sql.xml file namespace can be directly with dao name, note that at this time sql.xml file to be in the resource directory --> <! -- <mapper resource="mybatis/sqlmap/user/brokerinfoMapper.xml"/> --> <! -- sql file configuration way two: sql file and dao interface in the same directory, only need to configure the sql.xml file and the interface package path , but the sql.xml file and the interface path are in the same directory. But the namespace of sql.xml should be the full path of the dao interface--> <!-- <package name="com.test.web.dao"/> --> </mappers>
Similar Posts:
- mybatis: Property ‘configuration’ and ‘configLocation’ can not specified with together
- Failed to bind properties under
- [Solved] Failed to bind properties under ” to com.zaxxer.hikari.Hikari DataSource Spring Boot
- MyBatis Error: Error building SqlSession [How to Solve]
- [Solved] Springboot startup error: org.springframework.beans.factory.unsatisfieddependenceexception…
- [Solved] Mybatis-config Error: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: failed to parse the connection string near ‘;useUnicode=true&
- SSM Project Error: java.sql.SQLException: Access denied for user ‘????…§’@’localhost’ (…
- [Solved] Mybatis: the binding mapper cannot be found Error: Org.apache.ibatis.binding.bindingexception: invalid bound statement (not found)
- [Solved] ShardingJDBC Error: The bean ‘dataSource’, defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered.
- [Solved] Error creating bean with name ‘orderController’: Unsatisfied dependency expressed through field ‘orderService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: