SpringBoot07(springboot整合MyBatis)

一、整体思路解析:

二、步骤分析:

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);
    }

}

posted on   陈嘻嘻-  阅读(43)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)

导航

< 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
点击右上角即可分享
微信分享提示