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());
}
}