栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatis入门案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis入门案例

概述

建表

创建spring boot工程

创建susert实体类

编写mapper接口

编写mapper配置文件

编写Service接口以及实现类

编写Controller测试

在application.properties配置数据库等信息

运行测试


概述

我们开发项目时经常用到spring boot框架,而spring boot框架经常与第三方框架整合。因此学习如何整合第三方框架变得很重要。本篇介绍spring boot如何整合mybatis。接下来我们就一起来看吧!

建表

创建springboot工程

使用idea创建spring boot工程并加入相关依赖

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

创建user实体类

package com.yss.entity;


public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

编写mapper接口

package com.yss.mapper;

import com.yss.entity.User;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper {
    //添加一个根据id查询对象的方法
    User selectUserById(Integer id);
}

编写mapper配置文件




    

mapper文件的位置

  1.  编写service接口及其实现类
package com.yss.service;

import com.yss.entity.User;


public interface UserService {
    User queryUserById(Integer id);
}
package com.yss.service;

import com.yss.entity.User;
import com.yss.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryUserById(Integer id) {
        return userMapper.selectUserById(id);
    }
}

编写controller测试

package com.yss.controller;

import com.yss.entity.User;
import com.yss.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public String getUser(Integer id){
        User user = userService.queryUserById(id);
        return user.toString();
    }
}

在application.yaml文件配置数据库信息

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
mybatis:
  #指定映射文件的路径
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.yss.entity
  #添加日志输出
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

运行测试

 如果要看日志输出的话,就配置日志信息,如下图所示。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号