一篇文章带你掌握主流办公框架——SpringBoot( 七 )

整合MyBatis我们如果想要采用SpringBoot整合SSM,那么就需要同时整合以下三门技术:

  • Spring
  • SpringMVC
  • MyBatis
但SpringBoot本身就是为了简化Spring,SpringMVC而存在,所以这两部分整合实际上我们已经完成了
所以我们将MyBatis单列出来提前进行整合学习,为后续的SSM整合打下基础##
Spring整合MyBatisSpring对MyBatis的整合主要从三部分进行:
  • SpringConfig
    • 导入JdbcConfig
    • 导入MyBatisConfig
  • JdbcConfig
    • 定义数据源(加载properties项:driver,url,username,password)
  • MyBatisConfig
    • 定义sqlSessionFactoryBean
    • 定义映射配置
我们在这里就不做赘述了,如果遗忘可以查看之前的MyBatis文章
SpringBoot整合MyBatis我们同样从头开始整合:
  1. 创建项目(这次我们需要MyBatis和Mysql两门技术栈)

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
  1. 查看pom.xml并稍作讲解
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version> </parent> <groupId>com.itheima</groupId> <artifactId>springboot_08_mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <properties><java.version>1.8</java.version> </properties> <dependencies><!--自动添加MyBatis相关依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><!--自动添加mysql相关依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--手动添加druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency> </dependencies> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build></project>
  1. 创建与数据库相同的实体类
package com.itheima.domain;public class Book {private Integer id;private String name;private String type;private String description;@Overridepublic String toString() {return "Book{" +"id=" + id +", name='" + name + '\'' +", type='" + type + '\'' +", description='" + description + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}
  1. 数据层实现
package com.itheima.dao;import com.itheima.domain.Book;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;// 注意:我们SpringBoot整合中的SpringConfig已经被省略 , 所以我们的JdbcConfig和MyBatisConfig配置类不用配置// JdbcConfig主要用于配置DataSource,我们将会在yaml配置文件中配置// MyBatisConfig配置sqlSessionFactoryBean , 大部分属于固定代码,唯一的变量setTypeAliasesPackage我们选择设置整个代码包// MyBatisConfig配置MapperScannerConfigurer映射地址,我们选择在dao数据层采用@Mapper来代替操作@Mapperpublic interface BookDao {@Select("select * from tbl_book where id = #{id}")public Book getById(Integer id);}
  1. 配置数据库关联
# 直接配置datasource即可spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTCusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
  1. 启动服务器即可
package com.itheima;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Springboot08MybatisApplication { public static void main(String[] args) {SpringApplication.run(Springboot08MybatisApplication.class, args); }}

推荐阅读