1.导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
编写配置文件:
- 连接数据库
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///air
password: root
username: root
type: com.alibaba.druid.pool.DruidDataSource
MyBatis扫描映射文件
mybatis:
configuration:
#驼峰映射
map-underscore-to-camel-case: true
#设置别名
type-aliases-package: com.fanlan.entity
#扫描mapper
mapper-locations: classpath:mapper/*.xml
在启动类添加注解:
- 扫描Mapper接口
@MapperScan(basePackages = "com.fanlan.mapper")
测试:
@RunWith(SpringRunner.class)
@SpringBootTest 在ssm中是扫描xml文件
public class DistrictMapperTest {
@Resource
private DistrictMapper mapper;
@Test
public void findAll() {
System.out.println(mapper.findAll());
}
}