12-SpringBoot整合mybatis

2022-03-23 15:47:51 浏览数 (1)

12-SpringBoot整合mybatis

SpringBoot整合mybatis

实现步骤

①搭建SpringBoot工程

②引入mybatis起步依赖,添加mysql驱动

③编写DataSource和MyBatis相关配置

④定义表和实体类

⑤编写dao和mapper文件/纯注解开发

实现案例

1. 搭建SpringBoot工程

创建一个maven的空项目:

2. 引入mybatis起步依赖,添加mysql驱动

配置 pom.xml 设置依赖:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--springboot工程需要继承的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>
3. 定义数据库表
代码语言:javascript复制
create database springboot charset=utf8;

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

-- 插入数据
insert into t_user VALUES(null, "李白", "123456");
insert into t_user VALUES(null, "任我肥", "123456");
insert into t_user VALUES(null, "刘德肉", "123456");

-- 查询数据
select * from t_user;
4.编写SpringBoot 启动引导类
代码语言:javascript复制
package com.lijw.springbootmybatis;

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

@SpringBootApplication
public class SpringbootMybatisApplication {

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

}
5. 编写实体类
代码语言:javascript复制
package com.lijw.springbootmybatis.domain;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{"  
                "id="   id  
                ", username='"   username   '''  
                ", password='"   password   '''  
                '}';
    }
}
5. 编写数据库连接配置以及mybatis配置

application.yml

代码语言:javascript复制
# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: Lijw**************
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
  type-aliases-package: com.lijw.springbootmybatis.domain

  # config-location:  # 指定mybatis的核心配置文件

“mybatis的部分用于XML的映射文件设置,不过也一起配置上,不然会有警告信息。 mybatis.config-location 用于配置 mybatis 的核心配置文件,目前没用到。 ”

6. 纯注解开发,编写UserMapper
代码语言:javascript复制
package com.lijw.springbootmybatis.mapper;


import com.lijw.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();
}
7.编写测试方法类,验证全注解的查询
代码语言:javascript复制
package com.lijw.springbootmybatis.mapper;

import com.lijw.springbootmybatis.SpringbootMybatisApplication;
import com.lijw.springbootmybatis.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class UserMapperTest {

    @Autowired
    UserMapper userMapper;

    @Test
    public void findAll() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }
}

执行查询测试如下:

8.XML数据查询开发
8.1 编写dao UserXmlMapper
代码语言:javascript复制
package com.lijw.springbootmybatis.mapper;


import com.lijw.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserXmlMapper {

    public List<User> findAll();
}
8.2 编写 mapper.xml
代码语言:javascript复制
<?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="com.lijw.springbootmybatis.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select * from t_user
    </select>
</mapper>
9.编写测试代码
代码语言:javascript复制
package com.lijw.springbootmybatis.mapper;

import com.lijw.springbootmybatis.SpringbootMybatisApplication;
import com.lijw.springbootmybatis.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class UserMapperTest {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserXmlMapper userXmlMapper;

    @Test
    public void findAll() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }

    @Test
    public void findAll2() {
        List<User> all = userXmlMapper.findAll();
        System.out.println(all);
    }
}

0 人点赞