1.通过在Mapper类上加@Mapper注解
package tju.SpringBootMyBatisProject.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import tju.SpringBootMyBatisProject.domain.User;
@Mapper
public interface UserMapper {
int insertUser(User user);
User selectUserById(int id);
}
2.也可以在启动类上加 MapperScan
package tju.SpringBootMyBatisProject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("tju.SpringBootMyBatisProject.mapper")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
3.SpringBoot 中,如果Mapper xml文件和接口文件不在同一文件夹,可以通过如下配置指定xml文件的位置
mybatis.mapper-locations=classpath:tju/SpringBootMyBatisProject/mapper2/*.xml