Springboot Inherits Tkmapper General Mapper

Source: Internet
Author: User
Tags static class system log

MyBatis General Mapper provides a basic CRUD API that omits time to write a large amount of basic code or even advanced code, which is convenient and efficient. You can refer to the Generic mapper documentation

Springboot Integrated Universal Mapper, first of all, adding dependencies

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactid>mybatis-spring</ artifactid>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version >3.4.0</version>
        </dependency>
        <dependency>
            <groupid>tk.mybatis</ groupid>
            <artifactId>mapper</artifactId>
            <version>3.4.5</version>
        </dependency>

Then write the Mybatismapper configuration

Package com.xbz.web.common.config;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.mybatis.spring.SqlSessionFactoryBean;
Import Org.springframework.boot.autoconfigure.AutoConfigureAfter;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.core.io.support.PathMatchingResourcePatternResolver;
Import Org.springframework.core.io.support.ResourcePatternResolver;

Import Tk.mybatis.spring.mapper.MapperScannerConfigurer;
Import Javax.annotation.Resource;
Import Javax.sql.DataSource;

Import java.util.Properties;

    @Configuration public class Mybatisconfigurer {@Resource private DataSource DataSource; @Bean public Sqlsessionfactory Sqlsessionfactorybean () throws Exception {Sqlsessionfactorybean Bean = new SQL
        Sessionfactorybean ();
        Bean.setdatasource (DataSource);

        Bean.settypealiasespackage ("Com.xbz.domain"); Add XML directory ResourcepatternresOlver resolver = new Pathmatchingresourcepatternresolver ();
        Bean.setmapperlocations (Resolver.getresources ("Classpath:mapper/*.xml"));
    return Bean.getobject (); @Configuration @AutoConfigureAfter (mybatisconfigurer.class) public static class Mybatismapperscannerconfigu RER {@Bean public mapperscannerconfigurer mapperscannerconfigurer () {mapperscannerconfigure
            R mapperscannerconfigurer = new Mapperscannerconfigurer ();
            Mapperscannerconfigurer.setsqlsessionfactorybeanname ("Sqlsessionfactorybean");
            Mapperscannerconfigurer.setbasepackage ("com.xbz.dao.*");
            Configuration General Mappers Properties Properties = new properties ();
            Properties.setproperty ("Mappers", "Com.xbz.common.MyMapper");
            Properties.setproperty ("Notempty", "false");
            Properties.setproperty ("IDENTITY", "MYSQL");

            Mapperscannerconfigurer.setproperties (properties); Return mappersCannerconfigurer; }

    }
}


Finally, write your own Universal Mapper interface (you can also use your own mapper<t> interface directly)

Package Com.xbz.common;

Import Tk.mybatis.mapper.common.Mapper;
Import Tk.mybatis.mapper.common.MySqlMapper;

/**
 * @title General Mapper
 * @description *
 @author xingbz *
 @createDate 2017/12/20
 * @version 1.0
 * /Public
Interface mymapper<t> extends Mapper<t>, mysqlmapper<t> {
}


And then write the mapper, basically no more methods are available.

Package com.baobeitou.dao.master;

Import Com.xbz.common.MyMapper;
Import Com.xbz.domain.LogDO;
Import Org.apache.ibatis.annotations.Mapper;

/**
 * System log
 /
@Mapper public
interface Logdao extends mymapper<logdo> {}


In addition, I encountered a situation in the actual use of the project, because our project needs to do long data source read and write separation, like the tkmapper in the reading and operation of the interface extracted into two general mapper, found Tkmapper configuration can only configure one, Would you like to know if you have any good solutions?


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.