1. 回顾 Spring 整合 Mybatis
Spring
整合 Mybatis
需要定义很多配置类
-
SpringConfig
配置类-
导入
JdbcConfig
配置类 -
导入
MybatisConfig
配置类@Configuration @ComponentScan("com.itheima") @PropertySource("classpath:jdbc.properties") @Import({JdbcConfig.class,MyBatisConfig.class}) public class SpringConfig { }
-
-
JdbcConfig
配置类 -
定义数据源(加载 properties 配置项:driver、url、username、password)
public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Bean public DataSource getDataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }
-
MybatisConfig
配置类-
定义
SqlSessionFactoryBean
-
定义映射配置
@Bean public MapperScannerConfigurer getMapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; } @Bean public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(dataSource); return ssfb; }
-
2. SpringBoot 整合 mybatis
-
创建模块
-
定义实体类
public class Book { private Integer id; private String name; private String type; private String description; public Book() { } public Book(Integer id, String name, String type, String description) { this.id = id; this.name = name; this.type = type; this.description = description; } @Override public 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; } }
-
定义 Dao 接口
public interface BookDao { @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); }
-
定义测试类
@SpringBootTest class Springboot03MybatisTestApplicationTests { @Autowired private BookDao bookDao; @Test void contextLoads() { } @Test public void testMybatis() { Book book = bookDao.getById(1); System.out.println(book); } }
-
编写配置
-
在
application.yml
中指定数据库spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC username: root password: 123456
-
-
测试
运行测试方法,我们会看到如下错误信息
错误信息显示在
Spring
容器中没有BookDao
类型的bean
。为什么会出现这种情况呢?原因是
Mybatis
会扫描接口并创建接口的代码对象交给Spring
管理,但是现在并没有告诉Mybatis
哪个是dao
接口。而我们要解决这个问题需要在BookDao
接口上使用@Mapper
,BookDao
接口改进为
注意:
SpringBoot
版本低于 2.4.3(不含),Mysql 驱动版本大于 8.0 时,需要在 url 连接串中配置时区jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
,或在 MySQL 数据库端配置时区解决此问题
-
使用 Druid 数据源
-
导入
Druid
依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>
-
在
application.yml
配置文件配置
可以通过spring.datasource.type
来配置使用什么数据源。配置文件内容可以改进为spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC username: root password: root type: com.alibaba.druid.pool.DruidDataSource
-