MyBatis通用 Mapper4使用小结

官网地址:
http://www.mybatis.tk/
https://gitee.com/free

1.使用springboot,添加依赖:
使用tk的mybatis后不需要引用官方原生的mybatis。

   <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.1.5</version>
    </dependency>

2.yml中添加需要扫描的mapper接口

mapper:
 mappers:
  - com.example.testmybatis.testmapper.mapper.StuDescMapper
 notEmpty: true   

3.定义自己的mapper继承tk的Mapper

import com.example.testmybatis.testmapper.entity.StuDesc;
import tk.mybatis.mapper.common.Mapper;

@org.apache.ibatis.annotations.Mapper
public interface StuDescMapper extends Mapper<StuDesc> {
}

4.这些做好后就可以直接在服务层中使用基于单表的一些操作了。

注意事项:
定义与表对应的实体,重点说明在我们的实体字段中有mysql关键字时,如果是我们手写sql,则只需要在sql中使用`` 把desc引起来就好了,现在我们不是自己写的sql,则使用@Column(name = “需要转义的字段”),如下所示

import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "s_desc")
public class StuDesc {
    private String id;
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer userId;
    private String scoreLevel;
    @Column(name = "`desc`")
    private String desc;

注:
使用指导
https://github.com/abel533/Mapper/wiki

发布了25 篇原创文章 · 获赞 0 · 访问量 3864

猜你喜欢

转载自blog.csdn.net/InternetJava/article/details/104576445