JAVA SpringBoot中使用mybatis

本文详细介绍了如何在SpringBoot项目中整合MyBatis框架,包括依赖引入、数据源配置、实体类创建、Mapper接口定义、控制器编写及启动类设置,为开发者提供了一个完整的示例。

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

1、在pom.xml中引用依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

 

2、在Application.yml中配置数据源

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=PRC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

 

3、创建Entify

package com.example.demo.entity;

public class UserEntity {
    private String user_name;
    private String tel;

    public String getTel() {
        return tel;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
}

 

4、创建Mapper

package com.example.demo.mapper;

import com.example.demo.entity.UserEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
    @Select("Select user_name,tel From user where tel =  #{tel}")
    List<UserEntity> findByName(@Param("tel") String tel);

    @Insert("Insert Into user (tel,user_name) Values(#{tel},#{user_Name})")
    int insert(@Param("tel") String tel, @Param("user_Name") String user_Name);
}

注:Mapper使用的是interface接口

 

5、写控制器

package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.UserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@MapperScan("com.example.demo.mapper")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/addUser")
    public String addUser(String userName,String tel){
        return userMapper.insert(tel,userName) >0?"success":"fail";
    }

    @RequestMapping("/getUser")
    public List<UserEntity> getUser(String tel){

        List<UserEntity> dataList = (List<UserEntity>) userMapper.findByName(tel);
        return dataList;
    }
}

说明:实现相应的逻辑是通过调用Mapper中对应的方法

 

6、创建SpringBootApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

注:@MapperScan("com.example.demo.mapper")此项需要添加,框架默认找不到Mapper

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kinber

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值