首页 > 其他分享 >IDEA 中 SpringBoot2 整合 Mybatis 实例实例

IDEA 中 SpringBoot2 整合 Mybatis 实例实例

时间:2023-12-30 10:34:17浏览次数:35  
标签:name syrdbt mybatis 实例 SpringBoot2 Mybatis import public String


记录在 IDEA 中 使用SpringBoot2 整合 Mybatis的 实例,环境:Java8 + Maven + MySQL8。

1. 添加依赖 

添加 MyBatis 依赖,MySQL 连接依赖,,数据库用的MySQL8。

<!-- MyBatis 依赖 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

    <!-- MySQL 连接依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

pom.xml 全部如下所示:

<?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 http://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.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.syrdbt</groupId>
  <artifactId>mybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mybatis</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <!-- MyBatis 依赖 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

    <!-- MySQL 连接依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

  </dependencies>

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

</project>

2. 创建数据库的 SQL语句

 创建数据库的 SQL语句如下所示:注意 ID 是自增的,而且是主键。

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `name` varchar(255) DEFAULT NULL,
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

数据库 User 表结构 :

IDEA 中 SpringBoot2 整合 Mybatis 实例实例_java

3.  工程实例

项目的结构如图所示:有些用不到,红框中的本工程实例使用到的。

IDEA 中 SpringBoot2 整合 Mybatis 实例实例_spring_02

实体类 User.java :

package com.syrdbt.mybatis.entity;

public class User {
    private String ID;
    private String name;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

UserDAO.java ,DAO 层。

package com.syrdbt.mybatis.dao;

import com.syrdbt.mybatis.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserDAO {
    /**
     * 查询一个用户
     * @param name 用户姓名
     * @return 该用户
     */
    @Select("SELECT * FROM USER WHERE NAME = #{name}")
    User getUser(@Param("name") String name);

    /**
     * 插入一个用户
     * @param name 用户姓名
     */
    @Insert("INSERT INTO USER(NAME) VALUES(#{name})")
    int insert(@Param("name") String name);
}

UserService.java, Service 层 接口:

package com.syrdbt.mybatis.service;

import com.syrdbt.mybatis.entity.User;

public interface UserService {
    public User getUser(String name);
    public void insertUser(String name);
}

UserServiceImpl.java,Service 层 调用 DAO 层。

package com.syrdbt.mybatis.service.impl;

import com.syrdbt.mybatis.dao.UserDAO;
import com.syrdbt.mybatis.entity.User;
import com.syrdbt.mybatis.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDAO userDAO;

    @Override
    public User getUser(String name) {
        return userDAO.getUser(name);
    }

    @Override
    public void insertUser(String name) {
        userDAO.insert(name);
    }
}

TestMybatisController.java,控制器。

package com.syrdbt.mybatis.controller;

import com.syrdbt.mybatis.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestMybatisController {
    @Resource
    UserService userService;

    @RequestMapping("/insertUser")
    public String insertUser(@RequestParam(value = "name") String name) {
        System.out.println(name);
        userService.insertUser(name);
        return userService.getUser(name).getID();
    }

    @RequestMapping("/getUser")
    public String getUser(@RequestParam(value = "name") String name) {
        return userService.getUser(name).toString();
    }
}

MybatisApplication.java,启动类。

扫描 Mapper使用 @MapperScan("com.syrdbt.mybatis.dao") 。

package com.syrdbt.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.syrdbt.mybatis.dao")
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

}

application.properties ,数据库配置文件,记得修改用户名和密码。

spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

4. 工程测试

插入一个用户 :访问 http://localhost:8080/insertUser?name=syrdbt。

IDEA 中 SpringBoot2 整合 Mybatis 实例实例_java_03

查询用户:访问 http://localhost:8080/getUser?name=syrdbt。

IDEA 中 SpringBoot2 整合 Mybatis 实例实例_spring_04

标签:name,syrdbt,mybatis,实例,SpringBoot2,Mybatis,import,public,String
From: https://blog.51cto.com/xuxiangyang/9038510

相关文章

  • Spring Boot学习随笔- 集成MyBatis-Plus,第一个MP程序(环境搭建、@TableName、@TableId
    学习视频:【编程不良人】Mybatis-Plus整合SpringBoot实战教程,提高的你开发效率,后端人员必备!引言MyBatis-Plus是一个基于MyBatis的增强工具,旨在简化开发,提高效率。它扩展了MyBatis的功能,提供了许多实用的特性,包括强大的CRUD操作、条件构造器、分页插件、代码生成器等。MyBati......
  • 开放网络+私有云=?星融元的私有云承载网络解决方案实例
    在全世界范围内的云服务市场上,开放网络一直是一个备受关注的话题。相比于传统供应商的网络设备,开放网络具备软硬件解耦、云原生、可选组件丰富等优势,对云服务商和超大型企业有足够的吸引力。SONiC作为开源的网络操作系统,使得新一代网络中的高级可编程性成为现实。在Gartner2023年......
  • 华为云耀云服务器L实例-大数据学习-Hive的部署-1
     华为云耀云服务器L实例-大数据学习-Hive的部署-1  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,介绍Hive的部署 Hive 是建立在 Hado......
  • 华为云耀云服务器L实例--Hive的部署
     华为云耀云服务器L实例--Hive的部署  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,继续Hive的部署 Hive 是建立在 Hadoop 上的一个......
  • 华为云耀云服务器L实例-大数据学习-MapReduce&Yarn的部署
      华为云耀云服务器L实例-大数据学习-MapReduce&Yarn的部署 产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,介绍MapReduce 和 YARN(Yet Ano......
  • 华为云耀云服务器L实例-大数据学习-MapReduce&Yarn的实操
     华为云耀云服务器L实例-大数据学习-MapReduce&Yarn的实操  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,继续介绍MapReduce和YARN的实操......
  • 华为云耀云服务器L实例-大数据学习-单台服务器配置伪分布式模式hadoop集群
     华为云耀云服务器L实例-大数据学习-单台服务器配置伪分布式模式hadoop集群 产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,演示单台服务器配......
  • 华为云耀云服务器L实例-大数据学习-hadoop前置准备1-主机名映射与SSH免密登录
     华为云耀云服务器L实例-大数据学习-hadoop前置准备1-主机名映射与SSH免密登录  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,演示单台服......
  • 华为云耀云服务器L实例-大数据学习-hadoop前置准备2-JDK环境部署
     华为云耀云服务器L实例-大数据学习-hadoop前置准备2-JDK环境部署  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实例为例,演示单台服务器配置伪分......
  • 华为云耀云服务器L实例-大数据学习-hadoop前置准备3-防火墙、 SElinux 、时间同步等系
     华为云耀云服务器L实例-大数据学习-hadoop前置准备3-防火墙、 SElinux 、时间同步等系统设置  产品官网:https://www.huaweicloud.com/product/hecs-light.html  今天我们采用可靠更安全、智能不卡顿、价优随心用、上手更简单、管理特省心的华为云耀云服务器L实......