SpringBoot创建项目
首先创建好项目,详情见:https://www.cnblogs.com/expiator/p/15844275.html
SpringBoot引入mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
项目结构
Application启动类
@SpringBootApplication(scanBasePackages = {"com.example.ademo"})
@MapperScan(basePackages = { "com.example.ademo.dao" })
public class AdemoApplication {
public static void main(String[] args) {
SpringApplication.run(AdemoApplication.class, args);
}
}
-
@SpringBootApplication(scanBasePackages = {"com.example.ademo"}) 指定要扫描的Component的文件夹.
注意:Application.java必须与Controller所在的包、Dao所在的包等包位于同一层,否则会报错. -
@MapperScan是Mybatis的注解,@MapperScan(basePackages = { "com.example.ademo.dao" }) 指定要扫描的dao文件的位置.
注意:Mybatis的dao文件与对应的xml文件必须同名,否则会报错.
application.properties 配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:端口号/库名?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=用户名
spring.datasource.password=密码
# 指定mybatis的xml文件
mybatis.mapper-locations= dao/*Mapper.xml
mybatis自动生成文件
详情见:https://www.cnblogs.com/expiator/p/8652094.html
标签:集成,SpringBoot,spring,dao,datasource,mysql,mybatis,com From: https://www.cnblogs.com/expiator/p/17100173.html