SpringBoot 整合其他框架 -- SpringBoot整合Mybatis

Posted CodeJiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 整合其他框架 -- SpringBoot整合Mybatis相关的知识,希望对你有一定的参考价值。

1. SpringBoot整合Mybatis

需求:

SpringBoot整合MyBatis。

实现步骤:

  1. 搭建SpringBoot工程
  2. 引入mybatis起步依赖,添加mysql驱动
  3. 编写DataSource和MyBatis相关配置
  4. 定义表和实体类
  5. 编写dao和mapper文件/纯注解开发
  6. 测试

1.0 公共步骤


1.0.1 搭建SpringBoot工程


1.0.2 引入mybatis起步依赖 添加mysql驱动

pom.xml

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

1.0.3 搭建数据库环境

navicat运行下面的sql语句即可

CREATE DATABASE `springboot`;

USE `springboot`;

/*Table structure for table `t_user` */

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

/*Data for the table `t_user` */

insert  into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');


1.0.4 配置数据库连接信息

# datasource
spring:
  datasource:
    #    /// 表示省略了127.0.0.0.1
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: 317525
    driver-class-name: com.mysql.cj.jdbc.Driver

1.0.5 创建实体类

package com.tian.pojo;

public class User {
    private int id;
    private String username;
    private String password;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\\'' +
                ", password='" + password + '\\'' +
                '}';
    }
}

1.1 注解开发


1.1.1 创建Mapper

package com.tian.mapper;

import com.tian.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

// Mapper表示在Mybatis中注册
@Mapper
// Repository表示当前类是持久层
@Repository
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();
}

1.1.2 测试代码:

package com.tian;

import com.tian.mapper.UserMapper;
import com.tian.pojo.User;
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 {

    @Test
    void contextLoads() {
    }

    @Autowired
    private UserMapper userMapper;


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

运行结果:


1.2 传统xml映射文件开发


1.2.1 创建Mapper

package com.tian.mapper;

import com.tian.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

// Mapper表示在Mybatis中注册
@Mapper
// Repository表示当前类是持久层
@Repository
public interface UserXmlMapper {
    public List<User> findAll();
}

1.2.2 创建Mapper映射文件

<?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.tian.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select *
        from t_user
    </select>
</mapper>

1.2.3 配置文件中注册xml文件

# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: 317525
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
  type-aliases-package: com.tian.pojo # 配置别名

  # config-location:  # 指定mybatis的核心配置文件

1.2.4 测试代码:

package com.tian;

import com.tian.mapper.UserXmlMapper;
import com.tian.pojo.User;
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 {

    @Test
    void contextLoads() {
    }

    @Autowired
    private UserXmlMapper userXmlMapper;


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

运行结果:



以上是关于SpringBoot 整合其他框架 -- SpringBoot整合Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

(超详解)SpringBoot初级部分-整合其他框架-04

SpringBoot 介绍快速入门配置文件整合其他框架

Java闭关修炼SpringBoot-SpringBoot整合其他框架

SpringBoot与Nacos整合

SpringBoot 整合其他框架 -- SpringBoot整合Junit

SpringBoot 整合其他框架 -- SpringBoot整合Redis