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 2022-02-03 17:51  ziwang520  阅读(36)  评论(0编辑  收藏  举报