Springboot-整合mybatis 之 通用Mapper使用

通用Mapper

简述 :

             它是由国内一个大神针对mybatis 单表操作写的一个组件。如果多表操作的话,还是需要编写.xml映射文件并自己手写sql语句

 

使用操作: 引入通用mapper 启动器,在通用github上可以找到,相应的使用方式,已经pom文件内容。

 

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>x.x.x</version>
</dependency>

 

使用方式:

 

       编写一个接口,并且extends Mapper <实体类> 

        

package com.caicia.mapper;

import com.caicia.VO.User;
import tk.mybatis.mapper.common.Mapper;

//使用通用mapper 继承Mapper 并且声明对象类型为你的实体类的类型
public interface UserMapper  extends Mapper<User> {
}

 

 

编写一个service 使用注解,把

UserMapper  

自动注入

package com.caicia.Service;

import com.caicia.VO.User;
import com.caicia.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User queryByID( Long id){
        return userMapper.selectByPrimaryKey(id);

    }

}

 

编写好mapper 文件之后,配置相关属性,application.properties / application.yaml 

 

1、配置datasource 

 

2、配置相关实体类在哪

mysql6 以上的版本,都需要添加以下操作

 新的数据库连接中,要新增时区  sererTimeZone=UTC

#端口配置
server:
  port: 8088
#jdbc配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver //新版本,驱动多了一个cj
    url: jdbc:mysql://localhost:3306/数据库名称?serverTimezone=UTC
    username: xxxx
    password: xxxxxx
#mybatis配置
mybatis:
  configuration:
    map-underscore-to-camel-case: true //配置驼峰
  # mapper-locations: mapper/*.xml  配置映射配置文件位置
  type-aliases-package: com.caicia.VO #配置别名包路径实体类路径

 

在SpringbootApplication 启动应用中,添加@MapperScan 注解

示例:

 

package com.caicia;

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

@SpringBootApplication
@MapperScan("com.caicia.mapper")//配置扫描注解,用来扫描mapper接口
public class springbootApplication {

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

 

调用测试类,更具ID 能否查询数据库

 

package com.caicia.web;

import com.caicia.Service.UserService;
import com.caicia.VO.User;
import com.caicia.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;

//@Controller
@RestController
@RequestMapping("user")
public class hellconroll {

//    @Autowired
//    private DataSource dataSource;

//    @GetMapping("hello")
//    //@ResponseBody
//     public String hello(){
//
//         return "this is sprintboot comming!!!";
//
//
//     }
    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    //@ResponseBody
    public User hello(@PathVariable("id") Long id){

//        return "this is sprintboot comming!!!";
          return userService.queryByID(id);

    }

}

 

 

posted @ 2021-07-05 09:31  菜菜920  阅读(281)  评论(0编辑  收藏  举报