在整合MyBatis之前,我们要准备哪些工作:
-
导入mysql场景:
<mysql.version>8.0.28mysql.version> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <mysql.version>8.0.28mysql.version> dependency> 想要修改版本 1、直接依赖引入具体版本(maven的就近原则) 2、重新声明版本(maven的属性的就近原则) <properties> <java.version>1.8java.version> <mysql.version>8.0.28mysql.version> properties>
-
配置文件修改:
spring: datasource: url: jdbc:mysql://localhost:3306/user username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver # 如果是mysql8以上版本,请使用下面配置: spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/hunnu?characterEncoding=utf-8&serverTimezone=UTC username: root password: 123456
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jdbcartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
注:如果是使用 Spring Initilizr(项目初始化向导)来搭建的话,可在创建项目时就可以选中Mybatis的开发场景以及jdbc场景,系统会自动依赖引入。
导入了Mybatis的场景之后,我们会发现它给我们自动导入了:
如果是以前使用SSM框架整合MyBatis时,避免不了要写一大推的代码编写Mybatis的核心配置文件啊,SqlSessionFactory以及SqlSession等等之类的东西。总体来说,相当地复杂。但是现在有了SpringBoot (博主的SpringBoot版本:2.6.6)的话,很多的配置都由SpringBoot已经封装好了,节省了很多编写程序的时间。
SpringBoot自动配置好了:
- 全局配置文件 application.yml
- 自动配置好了SqlSessionFactory
- 底层自动配置了SqlSessionTemplate,SqlSessionTemplate组合了 SqlSession
- 主要我们写的Mybatis的接口标注了@Mapper注解就会被SpringBoot自动扫描进来。
我们可以CTRL+N 查找类:MybatisAutoConfiguration (mybatis的自动配置类)可以看到以下内容:
@EnableConfigurationProperties({MybatisProperties.class}) //mybatis配置项绑定类
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})
public class MybatisAutoConfiguration implements InitializingBean {
//然后CTRL+左键进入到MybatisProperties.class中发现以下内容:
@ConfigurationProperties(
prefix = "mybatis"
)
public class MybatisProperties {}
看到这里,我们发现了 prefix=“mybatis”,然后MybatisProperties又是mybatis配置项绑定类,也就是说我们在application.yml(配置文件)中写了以**“mybatis”**为前缀的配置项都是属于mybatis的配置!!!
3.XMl配置模式 (方式一)我们可以在配置文件中修改mybatis的配置,来定义mybatis的全局配置文件的位置和sql映射文件位置:
# 配置mybatis规则
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
然后在resource目录下创建mybatis/mapper/UserPapper.xml 并写入:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.admin.mapper.AccountMapper">
mapper>
注:这里有个很重要问题,就是为什么博主没有写mybatis的全局配置文件呢?
其实在上面 Mybatis的自动配置 里面说的MybatisProperties类中有一个属性叫private Configuration configuration:
也就是说我们只需修改application.yml(配置文件)中以"mybatis.configuration"为前缀的下面的所有,就是相当于修改mybatis的全局配置文件中的值!!
# 配置mybatis规则
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
可以不写全局配置文件,所有全局配置文件的配置都放在mybatis.configuration配置项中即可
总体流程:
-
导入mybatis官方starter
-
编写mapper接口。标准@Mapper注解
-
编写sql映射文件并绑定mapper接口
-
在application.yml中指定Mapper配置文件的位置,以及指定全局配置文件的信息(推荐配置在mybatis.configuration)
使用注解可以省去mybatis的全局配置文件 mybatis-config.xml 和xml的sql映射文件。
示例代码如下:
pojo:
@Data
public class User {
private int id;
private String name;
private int age;
private String email;
}
mapper(dao):
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
public Singer getUser(int id);
}
controller:
@ResponseBody
@RequestMapping("/getUser")
public User getUser(@RequestParam("id")int id){
User user = userMapper.getUser(id);
return user;
}
结果:
在实际生产开发中,最佳方式是xml配置方式和注解方式混合使用。
最佳实战:
- 引入mybatis-starter的场景启动器
- 配置application.yml中,指定mapper-location位置即可
- 编写Mapper接口并标注 @Mapper 注解 (如果觉得每次写接口都要标注 @Mapper 这样很麻烦,在springboot主程序启动类中配置 @MapperScan(“指定要扫描的位置”) )
- 简单方法直接注解方式
- 复杂方法编写mapper.xml进行绑定映射
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)