Configure next previous post
New Usermapper.java and Usermapper.xml
Where Usermapper.xml's namespace and filename are consistent with Usermapper.java
<mapper namespace= "Com.alibaba.mapper.UserMapper" >
Spring Add configuration
<BeanID= "Usermapper"class= "Org.mybatis.spring.mapper.MapperFactoryBean"> < Propertyname= "Mapperinterface"value= "Com.alibaba.mapper.UserMapper"/> < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory"/> </Bean>
Test class
PackageCom.alibaba.dao;Import Staticorg.junit.assert.*;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.alibaba.mapper.UserMapper;ImportCom.alibaba.po.User; Public classUsermappertest {PrivateApplicationContext ApplicationContext; @Before Public voidSetup ()throwsexception{ApplicationContext=NewClasspathxmlapplicationcontext ("Classpath:spring/applicationcontext.xml"); } @Test Public voidTest ()throwsexception{usermapper Usermapper= (usermapper) applicationcontext.getbean ("Usermapper"); User User= Usermapper.getuser (1); SYSTEM.OUT.PRINTLN (user); }}
Configuration Mode 2
Enable the spring scanner to comment out the original usermapper bean
<!--Mapper Configuration Mapperfactorybean: Generate proxy objects based on mapper interface - <!--<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property name= "mapperint Erface "value=" Com.alibaba.mapper.UserMapper "/> <property name=" sqlsessionfactory "ref=" Sqlsessionfactory "/& Gt </bean> - <!--mapper batch scan, scan out the Mapper interface from the Mapper package, automatically create proxy objects and register compliance with the Spring container: Keep Mapper.java and mapper.xml mapping file names consistent and automatically scanned in one directory The ID of the mapper bean is the Mapper class name (lowercase first letter) - <!--Specifies the package name to scan if multiple packages are scanned, the middle of each packet is separated by a comma with a half-width - <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer"> < Propertyname= "Basepackage"value= "Com.alibaba.mapper"/> < Propertyname= "Sqlsessionfactorybeanname"value= "Sqlsessionfactory"/> </Bean>
Comment out the scan package in the Sqlmapconfig.xml
<!--<package name= "Com.alibaba.mapper"/>--
Test file Ditto ....
The realization of Sping+maven+mybatis+ehcache Mapper