After learning Spring, I simply learned about MyBatis. Then, a simple integration is carried out, which meets the problem that MyBatista interface mapping beans can not be automatically injected.
Code exceptions:
Exceptions in thread "main" org. spring frame. bean. factory. Error creating a bean named "UserController": unsatisfactory dependencies expressed by the field "userdao"; nested exception is org.springframe.bean.factory. Bean Creation Exception: Error creating a bean named "userdao" in the file [C: Users Li Rui long eclipse-workspace MyBatis_Demo build classes com mybatis dao userdao]. Class]: When setting the bean property "sqlSessionFactory", the reference to the bean "sqlSessionFactory" cannot be resolved; the nested exception is org.springframe.bean.factory. Bean Creation Exception: Bean creation error defined as "sqlSessionFactory" in the Classpath Resource [Application Context]. When setting the bean property "dataSource", the reference to the bean "dataSource" cannot be resolved; the nested exception is org.springframe.bean.factory. BeanCreationException: Error creating a bean named "dataSource": Find method parsing failed; nested exception is java.lang. Illegal State Exception: Failed to inspect classes [org.apache.commons.dbcp2.Basic Data Source] from ClassLoader [jdk.internal.loader.ClassLoader. $AppClassLoader@77a567e1]
Exceptions indicate that Bean s in the controller layer cannot be created because the mapping interface corresponding to MyBatis can not complete the mapping and can not generate beans in the DAO layer.
So the problem should be in the XML file.
Test class, 13 lines error reporting:
1 package com.mybatis.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.mybatis.controller.UserController; 7 8 9 public class Test_Controller { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 ApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 14 UserController coll = (UserController) app.getBean("UserController"); 15 coll.test(); 16 } 17 18 }
Step by step, check the ApplicationContext.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Specify the package to be scanned to make the annotation effective --> <context:component-scan base-package="com.mybatis.po"/> <context:component-scan base-package="com.mybatis.dao"/> <context:component-scan base-package="com.mybatis.Controller"/> <!-- Configuring data sources --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value = "com.mysql.jdbc.Driver"/> <property name="url" value ="jdbc:mysql://localhost:3306/Springtest?characterEncoding=utf8"/> <property name="username" value = "root"/> <property name="password" value ="mysql" /> <!-- Maximum number of simultaneous connections --> <property name="maxActive" value="60" /> <!-- Maximum number of free connections --> <property name="maxTotal" value="60" /> <!-- The minimum number of free connections, below which new connections are created, defaults to 0 --> <property name="maxIdle" value="5" /> <!-- Number of initialized connections created at connection pool startup, default 0 --> <property name="initialSize" value="5" /> </bean> <!-- Adding Transaction Support --> <bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name = "dataSource" ref = "dataSource"/> </bean> <!-- Open transaction annotations --> <tx:annotation-driven transaction-manager ="txManager"/> <!-- To configure Mybatis Factory, specify data source at the same time, and MyBatista Perfect combination --> <bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref ="dataSource"/> <!-- configLocation Attribute is Mybatis Core Profile --> <property name = "configLocation" value = "classpath:mybatis-config.xml"></property> </bean> <!-- Mapper Agent development, use Spring Automatic scanning MyBatista Interface and assembly --> <!-- Spring All of the bytes in the specified package will be specified@Mapper The interface of annotation is automatically assembled as MyBatatis Mapping interface of --> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- MyBatis-spring Component scanner --> <property name="basePackage" value = "com.mybatis.dao"/> <property name="sqlSessionFactoryBeanName" value = "sqlSessionFactory"/> </bean> </beans>
- Check the package name of the scan for errors or understatements.
-
To make sure that the configuration of the data source is normal, my problem is here, modify the database configuration information (password, etc.) to see if there will be different errors reported, when the original error, that the configuration file is not loaded or the data source error. I used the DBCP data source (need to import two packages DBCP + connection pool packages), changed the password or reported the same error, so I tried to use Spring's own data source to solve the problem, correct code:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd"> 13 <!-- Specify the package to be scanned to make the annotation effective --> 14 15 <context:component-scan base-package="com.mybatis.po"/> 16 <context:component-scan base-package="com.mybatis.dao"/> 17 <context:component-scan base-package="com.mybatis.Controller"/> 18 <!-- Configuring data sources --> 19 <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> 20 <property name="driverClassName" value = "com.mysql.jdbc.Driver"/> 21 <property name="url" value ="jdbc:mysql://localhost:3306/Springtest?characterEncoding=utf8"/> 22 <property name="username" value = "root"/> 23 <property name="password" value ="mysql" /> 24 </bean> 25 26 <!-- Adding Transaction Support --> 27 <bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> 28 <property name = "dataSource" ref = "dataSource"/> 29 </bean> 30 <!-- Open transaction annotations --> 31 <tx:annotation-driven transaction-manager ="txManager"/> 32 <!-- To configure Mybatis Factory, specify data source at the same time, and MyBatista Perfect combination --> 33 <bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean"> 34 <property name="dataSource" ref ="dataSource"/> 35 <!-- configLocation Attribute is Mybatis Core Profile --> 36 <property name = "configLocation" value = "classpath:mybatis-config.xml"></property> 37 </bean> 38 <!-- Mapper Agent development, use Spring Automatic scanning MyBatista Interface and assembly --> 39 <!-- Spring All of the bytes in the specified package will be specified@Mapper The interface of annotation is automatically assembled as MyBatatis Mapping interface of --> 40 <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer"> 41 <!-- MyBatis-spring Component scanner --> 42 <property name="basePackage" value = "com.mybatis.dao"/> 43 <property name="sqlSessionFactoryBeanName" value = "sqlSessionFactory"/> 44 </bean> 45 46 </beans>
- Check the corresponding dependency class to see if the configuration file path can be Ctrl in. Whether MyBatis's core file and mapping file paths are correct or not. Here is my code:
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <!-- Mapping file--> <mapper resource = "com/mybatis/dao/UserMapper.xml"/> </mappers> </configuration