一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
二、添加application.properties配置
# mybatis的别名包扫描
mybatis.type-aliases-package=packages.bean
spring.datasource.url=jdbc:mysql://locahost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=*****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#新增
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
三、添加mybaitis的配置文件、虽然为空、保留下来以后可能会添加其他配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
四、添加dao层接口、既可使用注解、亦可使用mapper配置文件
public interface StudentDao {
public List<Student> getAll();
@Select("select * from student")
public List<Student> getAll2();
}
五、添加dao层的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="packages.StudentDao">
<select id="getAll" resultType="student">
SELECT * FROM student
</select>
</mapper>
六、给启动类添加dao成mapper扫描
@SpringBootApplication(scanBasePackages = "packages")
@MapperScan("packages.dao")
public class Springboot002Application {
public static void main(String[] args) {
SpringApplication.run(Springboot002Application.class, args);
}
}