基于SpringBoot整合Mybatis
1.导入依赖
<!--Boot工程父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<!--Boot工程打包的时候可以用到-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.编写配置信息
#数据源配置
spring:
datasource:
#springboot集成的mysql驱动版本是8,所以Driver要选cj包
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF8&useUnicode=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
#mybatis配置
mybatis:
#别名
type-aliases-package: com.ryan.domain
#mapper映射文件的路径
mapper-locations: classpath:mapper/*.xml
#全局配置
configuration:
#开启驼峰映射
map-underscore-to-camel-case: true
3.引导类标记扫描Mapper接口
//在启动类中指定扫描Mapper接口所在的包,生成代理,注入到容器中
@MapperScan(basePackages = "com.ryan.mapper")