《放棄Mybatis使用tkMybatis》

一、初始tkMybatis

(1)簡化sql語句,取消使用mapper.xml文件。

(2)方便統一管理,簡化代碼,取消臃腫的代碼。

二、springboot集成tkMybatis步驟

(1)、引入TkMybatis的Maven依賴。

(2)、配置對應實體類(表對應的實體)。

(3)、Mapper集成tkMybatis的Mapper接口。

(4)、啟動類註解@MapperScan掃描Mapper接口。

(5)、在application.yml配置文件,指定mapper.xml文件路徑。(可省略)

(6)、使用TkMybatis提供的sql執行方法。

三、接入TkMybatis

(1)、導入maven

<code>
            tk.mybatis
            mapper-spring-boot-starter
            2.1.0

 
            tk.mybatis
            mapper
            4.0.3
  /<code>

(2)、表對應的實體類

<code>package com.cloud.user.entity;

import lombok.Builder;
import lombok.Data;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Data
@Builder
/**
 * 表名稱和實體類名駝峰名稱一直時可以不適用@Table
 * 如:表面 user_info --->  實體類 UserInfo
 */
@Table(name = "user_info")
public class UserInfo implements Serializable {
    /**
     *  表主鍵   @Id :表示該字段是表主鍵
     * @GeneratedValue : 主鍵生成策略
     * generator="jdbc" :會自動將主鍵id填充到實體類中.類似普通mapper.xml中配置的selectKey標籤
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "jdbc")
    private Long uid;
    private String account;
    private String password;
    private String salt;
    private Integer accountType;
    private Integer sex;
    private  String phone;
}/<code>

注:

1.1、@GeneratedValue:strategy主鍵生成策略。

1.1.1、GenerationType.TABLE:使用表生產主鍵,即使用另外一張表單獨維護主鍵。使用如下:

<code>@Table(name = "user_info")
public class UserInfo implements Serializable {
    /**
     *  表主鍵   @Id :表示該字段是表主鍵
     * @GeneratedValue : 主鍵生成策略
     * strategy = GenerationType.TABLE : 自定義主鍵表,必須結合@TableGenerator使用
     * TableGenerator : 主鍵表相關參數配置
     */
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE,generator = "userSeq")
    @TableGenerator(name = "userSeq" ,allocationSize = 1,table = "user_seq_table",pkColumnName = "user_seq_id",valueColumnName = "seq_value")
    private Long uid;/<code>

1.1.2、GenerationType.SEQUENCE:序列化對象成主鍵,多用於不支持主鍵自增或者自動生成主鍵的數據庫(Oracle)。使用如下:

<code>@Table(name = "user_info")
public class UserInfo implements Serializable {
    /**
     *  表主鍵   @Id :表示該字段是表主鍵
     * @GeneratedValue : 主鍵生成策略
     * strategy = GenerationType.SEQUENCE : 序列化生成主鍵,必須結合@SequenceGenerator使用
     * SequenceGenerator : 序列化生成主鍵方式
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "userSeq")
    @SequenceGenerator(name = "userSeq" ,initialValue = 1,allocationSize = 1,sequenceName = "user_seq")
    private Long uid;/<code>

1.1.3、GenerationType.IDENTITY: 主鍵自增

<code>@Table(name = "user_info")
public class UserInfo implements Serializable {
    /**
     *  表主鍵   @Id :表示該字段是表主鍵
     *  strategy = GenerationType.IDENTITY : 主鍵自增 mysql 中的 auto_increment
     */
    @Id
    @GeneratedValue(strategy =GenerationType.IDENTITY  )
    private Long uid;/<code>

1.1.4、GenerationType.AUTO:自動創建主鍵(以上三種策略任選一種)

<code>@Table(name = "user_info")
public class UserInfo implements Serializable {
    /**
     *  表主鍵   @Id :表示該字段是表主鍵
     *  strategy = GenerationType.AUTO : 主鍵生成策略交給持久化引擎自動處理,
     *  持久化引擎會根據數據庫在以上三種生成策略中任選一個
     */
    @Id
    @GeneratedValue(strategy =GenerationType.AUTO  )
    private Long uid;/<code>

(3)、集成tkMybatis的Mapper接口

<code>import com.cloud.user.entity.UserInfo;
import tk.mybatis.mapper.common.Mapper;

/**
 * 接入 tk-mybatis
 *
 */
public interface UserInfoMapper extends Mapper {

}/<code>

(4)、啟動類註解@MapperScan掃描Mapper接口

<code>import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * 
 * 2020-05-06
 *
 */
@EnableDiscoveryClient
@EnableFeignClients
@MapperScan(basePackages = {"com.cloud.user.mapper"})
@SpringBootApplication
public class UserApplication {

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

}/<code>

(5)、在application.yml配置文件,指定mapper.xml文件路徑。(可省略)

<code>    # mybatis 
mybatis:
  configLocation: classpath:mybatis-config.xml
  mapperLocations: classpath:mappers/*.xml/<code>

(6)、業務層調用

<code>/**
 * 業務層
 * 2020-05-06
 *
 */
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,timeout = 10)
    @Override
    public UserInfo getUserInfo(Long uid) {
        return userInfoMapper.selectByPrimaryKey(uid);
    }
}/<code>

總結:

(1)接入比較方便,常見CRUD 都已經封裝完成。接入即可使用。

(2)無需再去維護xml 。不需要關注sql的使用。

(3)提升開發效率。


分享到:


相關文章: