SpringBoot 整合 Mybatis

maven :

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.0</version>
    </dependency>
    
    // 结合自己 mysql 版本
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>

mapper 直接写即可,可以配置映射
1 、 直接写mapper , 不配置映射

package com.hxg.mapper;

import com.hxg.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from student")
    List<Student> selectAllStudent();

    Student selectById(Integer id);
}

2 、 配置mapper.xml 映射,需要在springboot 参数中设置 mapper 位置
application.yml

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml

测试:

import com.hxg.Application;
import com.hxg.domain.Student;
import com.hxg.mapper.UserMapper;
import com.hxg.service.UserService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test {

    @Autowired
    private UserMapper userMapper;



    @org.junit.Test
    public void Mybatis(){
        List<Student> studentList = userMapper.selectAllStudent();
        for(Student student: studentList){
            System.out.println(student.toString());
        }
    }

    @org.junit.Test
    public void sss(){
        Student student = userMapper.selectById(1);
        System.out.println(student.toString());
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值