前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12-SpringBoot整合mybatis

12-SpringBoot整合mybatis

作者头像
Devops海洋的渔夫
发布2022-03-23 15:47:51
1600
发布2022-03-23 15:47:51
举报
文章被收录于专栏:Devops专栏Devops专栏

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);
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 海洋的渔夫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 12-SpringBoot整合mybatis
    • SpringBoot整合mybatis
      • 实现步骤
      • 实现案例
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档