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?