springboot整合mybatis

418 阅读1分钟

一、基于配置模式

1、引入starter依赖

2、编写mapper接口(记得一定要标注@Mapper)

@Mapper
//在编程中遇到自己解决不了的bug欢迎来杰凡it问答平台上提问——https://www.jf3q.com
public interface  UserMapper {
    public User getUser(Integer id);

}

3、编写sql映射文件并绑定mapper接口

4、在application.yaml中指定Mapper配置文件的位置

mybatis:
  mapper-locations: classpath:mapper/*.xml

当然也可以指定全局配置文件的信息 (建议;配置在mybatis.configuration)

5、写测试control和service调用后的效果:

二、基于注解的方式

把上面的接口写法改成:

@Select(" select * from  userinfo where  id=#{id}")
public User getUser(Integer id);

三、混合写法

接口层里有的用基于配置的方式有的用基于注解的方式