The realization of Sping+maven+mybatis+ehcache Mapper

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.