SpringBoot整合--Mybatis

Posted 闲言博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合--Mybatis相关的知识,希望对你有一定的参考价值。

1.导入依赖

mybatis-spring-boot-starter 不是springboot官方的,自研的

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombox插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
    </dependencies>

2.在启动类配置包扫描

@SpringBootApplication
@MapperScan(basePackages = "cn.bloghut.mapper")
public class MybatisApplication 

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



3.配置yml文件

# 配置数据库
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    password: 123
    username: root
# 配置Mybatis
mybatis:
  # 配置实体类别名
  type-aliases-package: cn.bloghut.pojo
  # 配置映射文件路径
  mapper-locations: classpath:cn/bloghut/mapper/*.Mapper.xml

4.编写用户类,用于封装查询出来的数据

@Data
public class User 
    private int id;
    private String username;
    private String pwd;


5.编写Mapper接口

@Repository
public interface UserMapper 

    List<User> findAll();



6.编写Mapper映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bloghut.mapper.UserMapper">

    <select id="findAll" resultType="User">
        select * from users
    </select>
</mapper>

7.测试

@SpringBootTest
class SpringbootBootDemo03MybatisApplicationTests 

    @Autowired
    private UserMapper userMapper;

    @Test
    void contextLoads() 
        List<User> users = userMapper.findAll();
        for (User user : users)
            System.out.println(user);
        
    



以上是关于SpringBoot整合--Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

SpringBoot整合Mybatis方式2:使用注解方式整合Mybatis

springboot.springboot用最简单的方式整合mybatis

SpringBoot整合Mybatis

基于SpringBoot的完成mybatis整合

SpringBoot:Mybatis整合PostgreSQL