通用Mapper Spring Boot

項(xiàng)目地址:https://github.com/mybatis/mybatis-3.git
添加Maven依賴

<!-- Spring Boot 使用 -->
<!--mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.1.4</version>
</dependency>
<!--pagehelper-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.1</version>
</dependency>

TestExampleMapper

public interface TestExampleMapper extends Mapper<TestExample> {
}

實(shí)體類注解

public class TestExample {
    @Id
    private Integer id;
    @Column
    private String  name;
    private String logo;
    //省略setter和getter方法
}

簡單用法示例

@Resource
private TestExampleMapper  mapper;

Condition condition = new Condition(TestExample .class);
condition .createCriteria().andGreaterThan("id", 100);
List<TestExample > exampleList = mapper.selectByCondition(condition );

application.properties 配置

#mybatis
mybatis.type-aliases-package=tk.mybatis.springboot.model
mybatis.mapper-locations=classpath:mapper/*.xml

#mapper
#mappers 多個(gè)接口時(shí)逗號隔開
mapper.mappers=tk.mybatis.springboot.util.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

application.yml 配置

mybatis:
    type-aliases-package: tk.mybatis.springboot.model
    mapper-locations: classpath:mapper/*.xml

mapper:
    mappers:
        - tk.mybatis.springboot.util.MyMapper
    not-empty: false
    identity: MYSQL

pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

mybatis-spring方式配置

@Configuration
public class MybatisConfigurer {

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage("com.duia.home.model,com.duia.home.common.dto");

        //配置分頁插件,詳情請查閱官方文檔
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("pageSizeZero", "true");//分頁尺寸為0時(shí)查詢所有紀(jì)錄不再執(zhí)行分頁
        properties.setProperty("reasonable", "true");//頁碼<=0 查詢第一頁,頁碼>=總頁數(shù)查詢最后一頁
        properties.setProperty("supportMethodsArguments", "true");//支持通過 Mapper 接口參數(shù)來傳遞分頁參數(shù)
        pageHelper.setProperties(properties);


        //添加插件
        factory.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return factory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        mapperScannerConfigurer.setBasePackage("com.duia.home.dao");

        //配置通用Mapper,詳情請查閱官方文檔
        Properties properties = new Properties();
        properties.setProperty("mappers", "com.duia.home.core.Mapper");
        //insert、update是否判斷字符串類型!='' 即 test="str != null"表達(dá)式內(nèi)是否追加 and str != ''
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);

        return mapperScannerConfigurer;
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容