首页 > 其他分享 >SpringBoot07(springboot整合MyBatis)

SpringBoot07(springboot整合MyBatis)

时间:2022-11-10 21:22:54浏览次数:32  
标签:mapper springboot boot SpringBoot07 import MyBatis org com itheima

一、整体思路解析:

二、步骤分析:

1-搭建springboot的工程(在idea里面搭建,略)

2-引入MyBatis的起步依赖,添加msql驱动

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springboot-MyBatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-MyBatis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3-编写DataSource和MyBatis相关配置

  • DataSource配置

  • MyBatis配置

4-定义表和实体类

定义表
  • 表的结构

  • 表的字段

  • 表的信息

实体类(要有Get和Set方法)
  • 实体类的路径和信息

5-编写dao和mapper文件/纯注解开发(两种开发方式)

  • 第一种dao和mapper文件的开发(这里暂时没有用到dao,直接使用mapper)

  • 对"编写dao和mapper文件"进行测试

  • 第二种"用纯注解"来开发

1-写一个UserXmlMapper的接口(接口里面写需要做的方法)

2-编写一个mapper的映射文件

  • 对"纯注解开发"进行测试




文件的源代码;

路径:src/main/resources/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.itheima.springbootmybatis.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        -- 这个sql语句,用来对UserXmlMapper的方法进行,执行操作
        select *from t_user
    </select>
</mapper>
路径:src/main/resources/application.yml
#datasourse
spring:
  datasource:
    url: jdbc:mysql:///springboot
    username: root
    password:
    driver-class-name: com.mysql.cj.jdbc.Driver

#Mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml   #mapper映射文件的路径
  type-aliases-package: com/itheima/springbootmybatis/domain #实体类的包名
  #config-location: #指定MaBatis的核心配置文件
路径:src/test/java/com/itheima/springbootmybatis/SpringbootMyBatisApplicationTests.java
package com.itheima.springbootmybatis;

import com.itheima.springbootmybatis.domain.User;
import com.itheima.springbootmybatis.mapper.UserMapper;
import com.itheima.springbootmybatis.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootMyBatisApplicationTests {

    @Autowired
    private UserXmlMapper userXmlMapper;

    @Test
    public void testFindAll2() {
        List<User> all = userXmlMapper.findAll();
        System.out.println(all);
    }


    @Autowired
    private UserMapper userMapper;

    @Test
    public void testFindAll() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }

}

标签:mapper,springboot,boot,SpringBoot07,import,MyBatis,org,com,itheima
From: https://www.cnblogs.com/chen-zhou1027/p/16878832.html

相关文章

  • Spring整合Mybatis分析与编码
    Spring整合Mybatis分析与编码正文Mybatis在开发的过程中,必须要经过的步骤有数据表-实体类-实体类别名-mapper接口-mapper文件实现-mapper文件注册-mybatisAPI......
  • dubbo+zookeeper+springboot远程连接,虚拟机和主机分布式操作
    dubbo+zookeeper+springboot远程连接,虚拟机和主机分布式操作springboot版本:阿里云2.3.7实现目标在主机上的消费者可以调用虚拟机中生产者的接口方法项目目录pom.xml......
  • springboot整合项目-商城项目订单系统以及aop监测
    订单系统1.持久层1.将数据插入到订单表中insertintot_order(oid除外所有的字段)values(字段的值)2.将数据还要插入订单项的表中insertintot_order_item(oid除......
  • SpringBoot 整合mybatis-plus
    SpringBoot整合mybatis-plus1、导入Maven依赖<dependencies><!--web依赖--><dependency><groupId>org.springframework.boot</groupId>......
  • SpringBoot启用Https(二十六)
    死亡的日子终究会到来,在那一天到来之前,别忘记,来我坟前再看一眼上一章简单介绍了SpringBoot自定义日志Starter(二十五),如果没有看过,​​请观看上一章​​关于这一章节的内......
  • SpringBoot整合Redis(十九)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了多数据源配置MyBatisPlus(十八),如果没有看过,​​请观看上一章​​一.Redis的介绍和安装......
  • SpringBoot整合Velocity(十二)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合FreeMarker(十一),如果没有看过,​​请观看上一章​​学习整合之前,可以看一......
  • SpringBoot通过Cors解决跨域问题(三十一)
    上一章简单介绍了SpringBoot全局异常处理(三十),如果没有看过,​​请观看上一章​​本章节参考江南一点雨大神的文章:​​SpringBoot2系列教程(十四)CORS解决跨域问题......
  • SpringBoot自定义Starter(二十四)
    即使有一天,我放弃了自己的身体,也请你,不要放弃我,我亲爱的灵魂.上一章简单介绍了Spring_Session解决Session共享的问题(二十三),如果没有看过,​​请观看上一章​​一.自定义......
  • SpringBoot上传和下载文件(二十七)
    当死亡来临,每一个人都不会接受自己的命运,他们会反抗.上一章简单介绍了SpringBoot启用Https(二十六),如果没有看过,​​请观看上一章​​文件上传和下载,是常用的功能可以看老......