springboot 配置mybatis通用mapper

springboot 配置mybatis通用mapper声明:此处为springboot配置mybatis的通用mapper方一共步其他多余操作不要有1添加mapper依赖一定要有以下依赖的jar包注意jar包版本,太高会导致功能不可用<!–SpringBootMybatis依赖–><dependency><groupId>org…

大家好,又见面了,我是你们的朋友全栈君。

声明:

此处为springboot 配置mybatis的通用mapper方

一共步其他多余操作不要有

1添加mapper依赖

一定要有以下依赖的jar包

注意jar包版本,太高会导致功能不可用

 <!-- Spring Boot Mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

2application.yml.文件

配置数据源,myabtis  logging和elastic-job可忽略

spring:
  datasource:
    jdbc-url: jdbc:mysql://172.20.94.39:3306/marketing?characterEncoding=utf-8&verifyServerCertificate=false&useSSL=false&requireSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: marketing
    password: marketing
    type: com.zaxxer.hikari.HikariDataSource
mybatis:
  type-aliases-package: com.renrenche.marketing.ads_scheduler.domain
#  mapper-locations: classpath:mapper/*.xml


logging:
  config: classpath:log4j2-development.yml

elastic-job:
  zookeeper-servers: 172.20.94.39:2181
  namespace: marketing-ads-scheduler


3编写MyMapper通用工具类 放在util包下,不要与正常的dao层mapper放到一起!!!

package com.renrenche.marketing.ads_scheduler.common.utils;

/**
 * Created by Administrator on 2017/8/4.
 */

import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;

/**
 * 继承自己的MyMapper
 *
 * @author junwen
 * @since 2017年8月22日11:50:27
 */
public interface MyMapper<T> extends Mapper<T>,
        MySqlMapper<T>,
        ConditionMapper<T>,
        InsertListMapper<T> {

    //TODO
    //FIXME 特别注意,该接口不能被扫描到,否则会出错
}

4配置mapper配置文件

package com.renrenche.marketing.ads_scheduler.config;

import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

@Configuration
public class MapperConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.renrenche.marketing.ads_scheduler.dao");//扫描该路径下的dao
        Properties properties = new Properties();
        properties.setProperty("mappers", "com.renrenche.marketing.ads_scheduler.common.utils.MyMapper");//通用dao
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }

}

 

5不需要在启动类上添加@MapperScan注解
6不需要在application.yml文件添加mapper配置

配好之后使用方法    dao.select()无报错并有返回值就代表已经配好了。
通用mapper的好处,可以省区增删改查的代码开发,加快开发速度,有很好的通用方法。省去一些值需要写简单增删改查的xml文件。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152674.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.

    The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.ThemethodsetPositiveButton(int,DialogInterface.OnClickListener)inthetypeAlertDialog.Builderisnotapplicableforthearguments(String,new  View.OnClickListener(){})Hereis

    2022年6月22日
    29
  • C语言逗号运算符_c语言逗号运算符优先级最低

    C语言逗号运算符_c语言逗号运算符优先级最低逗号也是运算符?是的,但是其实我更愿意说它是分隔符。在C语言中我们就经常使用逗号,看似逗号是非常平凡的分隔符,但是它关联到一个你必须知道但可能未曾思考的小知识:理论上,每条语句(分号结束),最终都会转换成一个值。例子1:#include<stdio.h>intmain(){ 3,4,5;//这是一条语句 //把上面这条语句的值赋值给变量a inta=(3,4,5); printf(“a=%d\n”,a);}输出结果:a=5因为a的值是整条语句的值,

    2025年7月15日
    3
  • spring boot拦截器和过滤器_过滤器的实现

    spring boot拦截器和过滤器_过滤器的实现一、过滤器和拦截器的区别1、过滤器和拦截器触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的。请求结束返回也是,是在servlet处理完后,返回给前端之前。2、拦截器可以获取IOC容器中的各个bean,而过滤器就不行,因为拦截器是spring提供并管理的,spring的功能可以被拦截器使用,在拦截器里注入一个service,可以调用业务逻辑。而过滤器是JavaEE标准,只需依赖servletapi,不需要依赖spring。3、过滤器的实现基于回调函数.

    2022年8月23日
    11
  • 使用多重循环打印平行四边形「建议收藏」

    packagecom.qfedu.test1;/*** 使用多重循环打印平行四边形* 当我们打印三角形的时候:* 1.观察第一行符号的个数,第一行符号的个数决定了循环计数器的初始值* 2.观察形状符号的个数是越来越多的话就++越来越少就–* 3.当计数器变化为++的时候判断条件一定要小于或者小于等于某个值相当于设定一个上限* 4.当计数器变化为–的时候判断条件一定要大于或者大于等于某个值相当于设定一个下限*/publicclassT

    2022年4月7日
    75
  • SSM整合(1)

    SSM整合(1)一、web.xml&amp;lt;?xmlversion=&quot;1.0&quot;encoding=&quot;UTF-8&quot;?&amp;gt;&amp;lt;web-appversion=&quot;2.5&quot;xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;xmlns:xsi=&quot;http://www.w

    2022年5月11日
    26
  • ABAP调用外部接口

    ABAP调用外部接口DATA:lc_http_clientTYPEREFTOif_http_client,LENTYPEI,”发送报文长度l_json_dataTYPEstring,l_json_stringTYPEstring,l_result_dataTYPEstring,l_urlTYPEstring.DATAlc_jsonTYP..

    2022年5月10日
    76

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号