6.3 Springboot整合Mybatis框架

一.整合Mybatis框架

1.步骤:

  • 在整合Druid的基础上继续(略)

  • pom.xml中添加mybatis依赖

  • 编写实体类

  • 编写mapper层(dao层)

  • 编写mapper映射文件

  • 在application.yml文件中添加配置

  • 编写控制器

2.pom.xml中添加mybatis依赖

<!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot 的,而不是Spring Boot自己的-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

3.编写实体类

  • 新建pojo目录,建立User.java类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private int id;
    private String name;
    private String pwd;

}

4.编写mapper层(dao层)

  • 新建mapper目录,添加UserMapper.java接口

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import ustc.wzh.pojo.User;

import java.util.List;

//@Mapper : 表示本类是一个 MyBatis 的 Mapper,等价于以前 Spring 整合 MyBatis 时的 Mapper 接口
@Mapper
@Repository
public interface UserMapper {

    //选择全部用户
    List<User> selectUser();
    //根据id选择用户
    User selectUserById(int id);
    //添加一个用户
    int addUser(User user);
    //修改一个用户
    int updateUser(User user);
    //根据id删除用户
    int deleteUser(int id);

}

5.编写mapper映射文件

  • 在resources目录下建立mybatis目录,在mybatis下建立mapper目录,在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="ustc.wzh.mapper.UserMapper">

    <select id="selectUser" resultType="User">
      select * from user
    </select>

    <select id="selectUserById" resultType="User">
        select * from user where id = #{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>

    <update id="updateUser" parameterType="User">
        update user set name=#{name},pwd=#{pwd} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </delete>
</mapper>

6.在application.yml文件中添加配置

  • 在application.yml文件末尾追加mybatis配置的映射以及实体类的路径

  • myBatis 与 spring 整合后,配置数据源、事务、连接数据库的账号、密码等就交由 spring 管理。

#指定myBatis的核心配置文件与Mapper映射文件
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  # 注意:对应实体类的路径
  type-aliases-package: ustc.wzh.pojo

7.编写控制器

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ustc.wzh.mapper.UserMapper;
import ustc.wzh.pojo.User;

import java.util.List;

@RestController
@RequestMapping("/mybatis")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //选择全部用户
    @GetMapping("/selectUser")
    public String selectUser(){
        List<User> users = userMapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }

        return "mybatis-selectUser:"+JSON.toJSONString(users);
    }

    //根据id选择用户
    @GetMapping("/selectUserById")
    public String selectUserById(){
        User user = userMapper.selectUserById(1);
        System.out.println(user);
        return "mybatis-selectUserById:"+JSON.toJSONString(user);
    }

    //添加一个用户
    @GetMapping("/addUser")
    public String addUser(){
        User user = new User(5,"阿毛","456789");
        userMapper.addUser(user);
        return "mybatis-addUser:"+JSON.toJSONString(user);
    }

    //修改一个用户
    @GetMapping("/updateUser")
    public String updateUser(){
        User user = new User(5,"阿毛","421319");
        userMapper.updateUser(user);
        return "mybatis-updateUser:"+JSON.toJSONString(user);
    }

    //根据id删除用户
    @GetMapping("/deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(5);
        return "mybatis-deleteUser:ok";
    }

}

8.项目架构

原文地址:https://www.cnblogs.com/zhihaospace/p/12406051.html

时间: 2024-08-28 14:39:50

6.3 Springboot整合Mybatis框架的相关文章

SpringBoot 整合MyBatis案例详解

SpringBoot约定大于配置的特点,让框架的配置非常简洁,与传统的SSM框架相比,简化了大量的XML配置,使得程序员可以从繁琐的配置中脱离出来,将精力集中在业务代码层面,提高了开发效率,并且后期的维护成本也大大降低. 从源码中可以看到,每一个springboot集成的jar包也是一个maven的子module,springboot已经将相关依赖的jar包自动添加到工程中,不需要开发人员手动去添加了,这也是springboot能够简化配置的一个重要原因. 下面开始说明springboot是如何

练习小记2:SpringBoot整合MyBatis

本文主要介绍SpringBoot整合MyBatis的初步过程以及需要注意的细节. SpringBoot简介 官网https://spring.io/projects/spring-boot/ ? MyBatis简介 官网:https://mybatis.org/mybatis-3/zh/index.html MyBatis的优势: 历史发展 MyBatis是由Ibatis发展而来的,Ibatis1.x和Ibatis2.x,都称为Ibatis,在Ibatis3.x版本及以后都称为:MyBatis

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?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:s

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.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="ht

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合Mybatis【非注解版】

接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ? 选择Spring Initializr,配置JDK版本 ? 输入项目名 ? 选择构建web项目所需的staters(启动器) ? 选择与数据库相关的组件 ? 分析:Spring Boot基本上将我们实际项目开发中所遇到的所有场景都做了封装.它将所有的功能场景都抽取出来,做成了一个个的staters(启动器),只需要在项目的pom.xml配置文件里面引入这些starter相关场景的所有依赖都会导入进来.需要什

springboot整合mybatis+oracle

第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包. 第二步 打开项目的结构,发现有了有趣的部分 在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件