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

  • 相关阅读:
    GIL
    CRM2Stark组件
    Django图书管理系统(单表操作)
    Python(ATM机low版)
    Python(9-18天总结)
    Python(1-8天总结)
    Python习题(分页显示)
    Python文本操作2
    Python递归二分法
    Python文本操作
  • 原文地址:https://www.cnblogs.com/ziwang520/p/15861766.html
Copyright © 2020-2023  润新知