刚刚开始用spring boot和mybatis整合,有点粗糙。遇到这个问题记录一下。
步骤:
1.首先pom引入依赖
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.然后写mappper接口(这里有两种方法【1.使用注解 2.配置文件】)
2.1.注解:
@Mapper
public interface UserMapper {
@Delete("drop table user if exists")
void dropTable();
@Insert("insert into user(name,age) values(#{name},#{age})")
void insert(User user);
@Select("select id,name,age from user where name like #{name}")
List<User> findByNameLike(String name);
@Delete("delete from user")
void deleteAll();
}
2.2.配置(注意mapper.xml中namespace)
3.扫包,在spring主程序上面添加这个注解
@MapperScan(basePackages = "com.xxx.mapper")
4.读取配置文件(在application.xxx中写)
mybatis:
config-location: classpath:config/mybatis-config.xml
mapper-locations: classpath:mapper/*.xml
这几部做好整个mybatis就整合在spring boot里了。