SpringBoot整合通用Mapper

SpringBoot整合Mybatis通用Mapper

    • 项目目录结构
    • 依赖添加
    • 配置文件信息
    • 新建一个通用的Mapper接口
    • 扫描Mapper
    • 测试

项目目录结构

SpringBoot整合通用Mapper_第1张图片

依赖添加

POM.xml,具体依赖如下

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.1version>
        dependency>
        
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.1.5version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.26version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

配置文件信息

Application.yml 文件内容:


spring:
  datasource:
    url: jdbc:mysql://ip:3306/数据库名?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&&serverTimezone=Asia/Shanghai
    username: 用户名
    password: 数据库连接密码
    hikari:
          connection-test-query: SELECT 1 FROM DUAL
          connection-timeout: 30000
          maximum-pool-size: 20
          max-lifetime: 1800000
          minimum-idle: 5
# mybatis 分页插件
#pagehelper:
#  # 数据库类型
#  helper-dialect: mysql
#  reasonable: true
#  support-methods-arguments: true
#  params: count=countSql
#  # 默认从0
#  page-size-zero: true
mybatis:
  configuration:
    # 开启Mybatis下滑线转驼峰命名
    map-underscore-to-camel-case: true
  type-aliases-package: com.example.pojo

mapper:
  mappers:
  # 这里是你自己的通用Mapper接口
   - com.example.base.BaseMapper
  identity: MYSQL
  mapper.not-empty: false

新建一个通用的Mapper接口

这里BaseMapper接口要继承:
tk.mybatis.mapper.common.Mapper
tk.mybatis.mapper.common.MySqlMapper
注意:
  这个接口不能被  @MapperScan  扫描
具体代码如下:

package com.example.base;

import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 通用Mapper
 * @param 
 */
@Component
public interface BaseMapper<T> extends Mapper<T>,MySqlMapper<T> {

}

BrandMapper (具体业务接口) 去继承我们的通用Mapper:

package com.example.dao;

import com.example.base.BaseMapper;

public interface BrandMapper extends BaseMapper<TbBrand> {

}

扫描Mapper

在SpringBoot的启动类中进行扫描:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@MapperScan("com.example.dao")
@SpringBootApplication
public class SpringbootPagehelperApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootPagehelperApplication.class, args);
	}

}

Controller类:

@RestController
public class HomeController {

	@Autowired
	private BrandMapper brandMapper;

	@GetMapping("/get")
	public TbBrand getTbbrand( Long id){
		try {
			TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
			System.out.println(tbBrand.getName());
			return tbBrand;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
}

测试

SpringBoot整合通用Mapper_第2张图片

你可能感兴趣的:(SpringBoot)