springboot集成mybatis
1、导包,导入数据源和mybatis包,数据连接池使用springboot自带的
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2、配置文件
mybatis:
mapper-locations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.hua.entity
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/fk?characterEncoding=UTF-8
username: root
password: root
3、在启动类上添加扫描mapper接口注解
@SpringBootApplication
@MapperScan(basePackages = "com.hua.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4、新建mapper接口
@Mapper
public interface TeacherMapper {
}
5、新建xml文件
在resources目录下新建mapper目录,在mapper目录下创建xml文件
注意:namespace是mapper接口包名+类名
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hua.mapper.TeacherMapper">
</mapper>