springboot整合mybatis
注解整合
免配置
1导依赖 在创建Spring Initializr 的时候选择 mysql驱动 和 mybatis 框架 可以自动导入依赖
<dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <!-- runtime 指运行时 执行驱动--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2 yaml中配置数据源 datasource
#datesource
spring:
datasource:
#时区问题 url: jdbc:mysql:///springboot?serverTime=UTC 一个标准时区
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///springboot
username: root
password: "123456"
#注解↑
3 写好实体类domain 和 映射接口 mapper接口
package com.example.springboot_mybatis.mapper; import com.example.springboot_mybatis.domain.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper { @Select("select*from user") public User findall(); }
4测试
xml整合
1导依赖
2 yaml中配置数据源 datasource 以及myabtis配置
#datesource
spring:
datasource:
#时区问题 url: jdbc:mysql:///springboot?serverTime=UTC 一个标准时区
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///springboot
username: root
password: "123456"
#注解↑
#xml配置 ↓
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件地址
type-aliases-package: com.example.springboot_mybatis.domain #取别名
#config-location: 指定mybatis核心配置文件的位置 就是conig文件
3 写好实体类domain 和 映射接口 mapper接口
4测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了