SpringBoot整合Mybatis

1.使用逆向工作获得资源
根据数据库的设计,获得entity mapper接口 映射文件
2.在springboot添加mybatis和mysql驱动的依赖

	<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3.编写ssm业务

4.在springboot的主程序上加上mapperscan注解,指明mapper接口所在的路径。
让springboot去扫描到这些mapper:
在这里插入图片描述
5.springboot的application.yml进行配置
1)数据库的连接信息

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_xx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver
    username: xxxx
    password: xxxxx

2)为mybatis做配置

mybatis:
  type-aliases-package: com.qf.myspringbootdemo3.entity
  mapper-locations: classpath:mapper/*.xml

这一切完成以后你就可以启动你的ssm来做你想做的事啦!

### Spring Boot 整合 MyBatis 示例教程 #### 一、简介 在现代 Java 后端开发中,Spring Boot 和 MyBatis 是两个非常受欢迎的框架。前者提供了快速构建基于 Spring 的应用程序的能力,大大简化了配置过程;后者则是一款优秀的持久层框架,能够方便地与数据库进行交互[^1]。 #### 二、环境准备 为了顺利整合这两个强大的工具,在开始之前需确保已安装好 JDK (建议版本8及以上),Maven 构建工具以及 MySQL 数据库服务器。此外还需准备好 IDE 开发环境如 IntelliJ IDEA 或 Eclipse 等支持 Maven 工程的新版编辑器。 #### 三、创建 Spring Boot 项目并引入依赖项 可以通过 Spring Initializr 创建一个新的 Spring Boot 应用程序,并添加 `MyBatis Framework` 及其他必要的 Starter POM 文件中的依赖关系: ```xml <dependencies> <!-- Other dependencies --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` #### 四、配置数据源和其他设置 接下来要做的就是在项目的 resources 目录下找到名为 `application.properties` 的文件来定义连接池参数以及其他一些选项。这里给出一个简单的例子作为参考[^3]: ```properties spring.application.name=springboot-demo2 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=p@ssw0rd ``` 请注意 URL 中包含了 `useSSL=false` 参数用于关闭 SSL 加密通信,这通常是在本地测试环境中使用的做法; 而对于生产部署来说,则应该开启此功能以保障安全性。 #### 五、编写 Mapper 接口和实体类 现在可以着手设计业务逻辑部分了——即 CRUD 操作对应的 DAO 层接口及其 XML 映射文档。假设有一个 User 表结构如下所示: | id | name | |----|----------| | INT| VARCHAR(50)| 那么相应的 POJO 类型就应该是这样的样子: ```java public class User { private Integer id; private String name; // Getters and Setters... } ``` 与此同时还需要建立一个映射接口用来声明 SQL 查询语句的方法签名: ```java import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface IUserDao { List<User> findAll(); void insert(User user); } ``` 最后一步就是为上述方法提供具体的实现细节了, 即通过 XML 形式的 mapper 文件完成实际的数据访问操作描述. #### 六、启动服务验证成果 当所有准备工作都完成后就可以运行 Application.java 来检验整个流程是否正常工作了。如果一切顺利的话应当可以在控制台看到类似下面的日志输出表示成功建立了与目标表之间的关联[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值