Springboot整合通用mapper

版本:tk.mapper-2.0.2
开发工具:IDEA2020.3

1.导入依赖

 

<!--数据库驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!--连接池-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
<!--</dependency>-->
<!--mybatis -->
<!--<dependency>-->
    <!--<groupId>org.mybatis.spring.boot</groupId>-->
    <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
    <!--<version>1.3.2</version>-->
<!--</dependency>-->
<!--通用mapper,依赖导入了以上两个启动器-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>

 

2.实体类  

 

 

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
@Data
@Table(name="tb_user")//指定表名
public class User {
    @Id//主键
    @KeySql(useGeneratedKeys = true)//自动增长
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    //1男,2女
    private Integer sex;
    private Date birthday;
    private String note;
    private Date created;
//    @Transient 忽略该字段
    private Date updated;
}

 

 

3.mapper

 

import tk.mybatis.mapper.common.Mapper;
//继承通用Mapper的Mapper接口,指定泛型为对应实体类
public interface UserMapper extends Mapper<User>{

}

 

4.启动类

import tk.mybatis.spring.annotation.MapperScan;
@MapperScan(basePackages="com.lamoun.leyou.mapper")

5.配置文件

 

#配置com.zaxxer:HikariCP:3.4.1连接池
  spring:
    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
      username: root
      password: root

#整合mybatis
  mybatis:
    #启用别名,配置实体类路径(也可以在实体类上加@Enity注解)
    type-aliases-package: com.company.pojo

 

当你引入:

spring-boot-starter-jdbc启动器的时候
SpringBoot就已经为我们配置好事务控制了,只需要在service中需要事务的方法上加上@Transactional注解即可

  

 

Springboot整合通用mapper

上一篇:uniapp——自定义input清除事件


下一篇:Jmeter如何把CSV文件的路径设置成一个变量,且变量的值是一个相对路径