1.SpringBoot整合Mybatis

本文详细介绍如何在SpringBoot项目中整合Mybatis,包括配置Maven依赖,设置application.yml,创建数据库表,编写POJO、Mapper、Service和Controller类,以及启动类的编写。通过访问特定URL,成功展示数据库查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.SpringBoot整合Mybatis

  • File| New|Project |Spring Initializr|
    在这里插入图片描述
  • 选择需要的组件:
    在这里插入图片描述
  • 配置Maven:
    在这里插入图片描述
  • 修改配置文件

application.yml

spring:
  profiles:
    active: dev

application.yml-dev

server:
  port: 8080
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
#  让mybatis自动扫描到自定义POJO
    type-aliases-package: com.example.pojo
  • test数据库建立user表并添加数据测试:
id int 20 primarykey
name varchar 20
  • 在com.example下建立pojo、mapper、service、controller包
    pojo->User.java
package com.example.pojo;

public class User {
    int id;
    String name;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

mapper->UserMapper.java

package com.example.mapper;

import com.example.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
    @Select("select * from user where id=#{id}")
    User getUserById(@Param("id") int id);
}

service->UserService.java

package com.example.service;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getUserById(int id){
        return userMapper.getUserById(id);
    }
}

controller->UserController.java

package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("get/{id}")
    public User getUserById(@PathVariable int id){ return this.userService.getUserById(id);}
}

  • 编写启动类:Springbootdemo1Application.java
package com.example;

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

@SpringBootApplication
//扫描mapper
@MapperScan("com.example.mapper")
public class Springbootdemo1Application {

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

此时,启动服务器,访问

http://localhost:8080/user/get/1

结果如下:
在这里插入图片描述
关于SpringBoot整合Mybatis的其他方法后面会继续补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值