Spring Boot通过Mybatis,使用mapper.xml配置sql,连接数据库

发现做后台的同事用的一般都是xml的形式,

首先在配置文件application.properties中,写上相关的信息

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=NewPassword

#mybatis.typeAliasesPackage:为实体对象所在的包,跟数据库表一一对应
#mybatis.mapperLocations:mapper文件的位置

#mybatis.typeAliasesPackage=com.example.demo.entity
#mybatis.mapperLocations=classpath:mapper/*Dao.xml
#or 写法不同,不过结果一样
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:mapper/*Dao.xml

在启动类Application.java中写上配置信息

@SpringBootApplication
@MapperScan("com.example.demo.dao")//  mybatis扫描路径,针对的是接口Mapper类
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

or