Spring MyBatis Mapper Interface annotation Method injection

Source: Internet
Author: User

The most recent project uses Mybatis-generator to generate MyBatis mapper.xml mapping files and mapper interfaces.
Originally used Spring-bean way to display injection, the code is as follows:

<bean id= "Articlemapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
    <property name= " Mapperinterface "value=" Com.kuanrf.gs.article.dao.ArticleMapper "/> <property name=" Sqlsessionfactory "
    ref= "Sqlsessionfactory"/>
</bean>

It's too much trouble to find that each MyBatis Mapper interface is so configured. It is optimized to adopt spring unified scanning automatic injection.
Application_spring_db.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x Si= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" Xsi:schema location= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bean S-4.2.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spri Ng-tx-4.2.xsd "> <!--Configure DBCP data source--> <bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDa Tasource "destroy-method=" Close "> <property name=" driverclass "value=" ${jdbc.driver} "/> <  
        Property Name= "Jdbcurl" value= "${jdbc.url}"/> <property name= "user" value= "${jdbc.username}"/> <property name= "Password" value= "${jdbc.password}"/> <property name= "Initialpoolsize" value= Nection_pools.initial_poOl_size} "/> <property name= minpoolsize" value= "${connection_pools.min_pool_size}"/> Ty name= "maxpoolsize" value= "${connection_pools.max_pool_size}"/> <property name= "maxIdleTime" value= "${con" Nection_pools.max_idle_time} "/> <property name=" acquireincrement "value=" ${connection_pools.acquire_increme NT} "/> <property name=" checkouttimeout "value=" ${connection_pools.checkout_timeout} "/> </bean&gt  

    ; <!--configuration Mybatissqlsessionfactorybean--> <bean id= "Sqlsessionfactory" class= Actorybean "> <property name=" dataSource ref= "DataSource"/> <property name= "Configlocatio N "value=" Classpath:mybatis/mybatis.xml "/> <property name=" mapperlocations "value=" Classpath*:com/itclj/ma Gpie/**/*mapper.xml "/> </bean> <!--configuration sqlsessiontemplate--> <bean id=" Sqlsessiontempla TE "class=" org.mYbatis.spring.SqlSessionTemplate "> <constructor-arg name=" sqlsessionfactory "ref=" Sqlsessionfactory "/>" </bean> <!--Configure MyBatis Mapper interface--> <bean class= "Org.mybatis.spring.mapper.MapperScannerC Onfigurer "> <property name=" basepackage "value=" Com.itclj.magpie.**.dao "/> <property-name=" sql Sessiontemplatebeanname "value=" Sqlsessiontemplate "/> </bean> <!--transaction configuration--> <bean id=" tra Nsactionmanager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name= "DataSource" ref= "DataSource"/> </bean> <!--Configure transactions using the annotation annotation method-->   -driven transaction-manager= "TransactionManager"/> </beans>

The following code is for spring to self scan all DAO packages and assemble all the MyBatis interface files under it into the container.

<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name= "Basepackage" value= "Com.itclj.magpie.**.dao"/>
        <property name= "Sqlsessiontemplatebeanname" value= "SqlSessionTemplate" >
</bean>

MyBatis interface Mapper need to add @repository annotation, can directly in the service layer directly automatic assembly injection.
MyBatis Interface Mapper examples are as follows:

@Repository public
Interface Userbeanmapper extends basemapper<userbean,userbeanexample,long> {

}

When the service layer injects the MyBatis interface mapper, it needs to be injected into the construction method so that the injected mapper instance is not empty.

@Service (value = "UserService") public
class Userserviceimpl extends Baseserviceimpl<userbean, userbeanexample , Long> implements UserService {

    @Autowired () public
    Userserviceimpl (Userbeanmapper userbeanmapper) {
        This.setmapper (Userbeanmapper);
    }

The

Engineering directory structure is shown in the following illustration

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.