MyBatis通用 Mapper4使用小结

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

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

   <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;

项目中如果还需要自己来手写sql的,则在springboot的yml添加,然后向以前一样手写sql就OK了。

mybatis:
 mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
 type-aliases-package: com.example.testmybatis.testmapper.entity.*  # 注意:对应实体类的路径
 configuration:
  map-underscore-to-camel-case: true  #开启下划线与实体类的驼峰映射

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值