随笔 - 35  文章 - 0  评论 - 0  阅读 - 9022

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测试

posted on   ziwang520  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示