mybatis介绍
jdbc较为繁琐,所以引用mybatis进行数据库整合。
mybatis不提供对象和关系模型的直接映射,半ORM(ORM: object relational mapping对象关系映射)。
集成mybatis
- 引入依赖
<!-- starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 加入配置文件
#一般可以自动识别
#spring.datasource.driver-class-name = com.mysql.jdbc.Driver
#本地的mysql服务
spring.datasource.url = jdbc:mysql://localhost:3306/${具体的数据库名}?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123
springboot帮助开发者加载配置,注入sqlFactory等。
-
启动类添加mapper扫描@MapperScan(“mapper类路径”),或者在mapper接口添加@Mapper注解
-
开发mapper接口,写入sql脚本。 也可以通过mybatis官方的逆向工程,根据数据库生成mapper,model,这方面的内容会在后续的文章中介绍。