springboot集成mybatis源码分析(一)

时间:2022-08-29 19:48:09

本篇文章只是简单接受使用,具体源码解析请看后续文章

1、新建springboot项目,并导入mybatis的pom配置

配置数据库驱动和mybatis dependency
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
application.yml配置

spring :
datasource :
driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username : root
password : 123456

2、基础类(使用lombok自动生成get/set方法)

package com.example.demo.domain;

import lombok.Data;

@Data
public class User {
private Integer id;
private String username;
private Integer age;
}

3、测试dao(mybatis使用注解开发)

package com.example.demo.dao;

import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; @Mapper
public interface UserDao { @Select("SELECT * FROM USER")
List<User> getUser();
}

4、测试service

package com.example.demo.service;

import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
@Slf4j
public class UserService { @Autowired
private UserDao userDao; public List<User> getUser(){
List<User> userList = userDao.getUser();
log.info("查询出来的用户信息,{}",userList.toString());
return userList;
}
}

5、service对应的test类(该测试类继承主测试类(主测试类直接在启动文件上goto test即可自动生成))

package com.example.demo.service;

import com.example.demo.DemoApplicationTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import static org.junit.Assert.*; public class UserServiceTest extends DemoApplicationTests { @Autowired
private UserService userService; @Test
public void getUser() {
userService.getUser();
} }
package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { @Test
public void contextLoads() {
} }

6、运行测试类输出结果

  查询出来的用户信息,[User(id=1, username=test, age=11)]

  

springboot集成mybatis源码分析(一)的更多相关文章

  1. springboot集成mybatis源码分析-mybatis的mapper执行查询时的流程(三)

    例: package com.example.demo.service; import com.example.demo.dao.UserDao; import com.example.demo.do ...

  2. springboot集成mybatis源码分析-启动加载mybatis过程(二)

    1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...

  3. springboot整合mybatis源码分析

    springboot整合mybatis源码分析 本文主要讲述mybatis在springboot中是如何被加载执行的,由于涉及的内容会比较多,所以这次只会对调用关系及关键代码点进行讲解,为了避免文章太 ...

  4. MyBatis源码分析(5)——内置DataSource实现

    @(MyBatis)[DataSource] MyBatis源码分析(5)--内置DataSource实现 MyBatis内置了两个DataSource的实现:UnpooledDataSource,该 ...

  5. MyBatis源码分析(3)—— Cache接口以及实现

    @(MyBatis)[Cache] MyBatis源码分析--Cache接口以及实现 Cache接口 MyBatis中的Cache以SPI实现,给需要集成其它Cache或者自定义Cache提供了接口. ...

  6. MyBatis 源码分析-项目总览

    MyBatis 源码分析-项目总览 1.概述 本文主要大致介绍一下MyBatis的项目结构.引用参考资料<MyBatis技术内幕> 此外,https://mybatis.org/mybat ...

  7. 精尽 MyBatis 源码分析 - 整体架构

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  8. 精尽 MyBatis 源码分析 - SqlSession 会话与 SQL 执行入口

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  9. 精尽MyBatis源码分析 - MyBatis-Spring 源码分析

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

随机推荐

  1. 大量数据快速导出的解决方案-Kettle

    1.开发背景 在web项目中,经常会需要查询数据导出excel,以前比较常见的就是用poi.使用poi的时候也有两种方式,一种就是直接将集合一次性导出为excel,还有一种是分批次追加的方式适合数据量 ...

  2. Dell xps 13 9350待机时总是关机的处理方法

    现象: 年初买的dell xps13 9350,最近可能是由于win10做了一些更新,每次睡眠就自动关机了,重启很多次,修改电源选项都没用, 原因分析: 在网上搜了一下,有人发现xps15 9350也 ...

  3. protobuf初体验

    概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...

  4. react学习笔记1

    # 1.hello world 学习一个语言,最好的方式,我们需要去官网去查看文档(https://facebook.github.io/react),通过JSFiddle,便可以看到最简单的demo ...

  5. iOS 测试驱动开发

    测试驱动开发是敏捷开发的一部分,它一般有“red-green- refactor”步骤 iOS测试驱动开发的工具 一. OCUnit 是Xcode自带的测试工具 其使用步骤分为 1 建立测试的Targ ...

  6. jQuery-ui datepicker的使用演示代码

    这两天使用jquery做一个web端展示的工具,遇到了不少问题也学到了不少知识.其中有一个就是在页面中显示日期选择器的功能,通过百度直接使用的是jquery datepicker 看到一篇使用说明很不 ...

  7. Hibernate基础学习&lpar;五&rpar;&mdash&semi;对象-关系映射&lpar;下&rpar;

    一.单向n-1 单向n-1关联只需从n的一端可以访问1的一端. 域模型: 从Order到Customer的多对一单向关联.Order类中定义一个Customer属性,而在Customer类不用存放Or ...

  8. Intellij Idea 无法启动项目的配置坑

    1. run/debug configuration里面,tomcat的deployment点击添加不能自动创建war-explorded包: 方案:删除project libraries,重新mav ...

  9. debian 系统安装配置apache

    安装sshapt-get install ssh-server  (安装失败请插入镜像)service ssh start Apache 服务安装apt-get install apache2 apa ...

  10. Mybatis trim标签

      trim代替where/set标签 trim 是更灵活用来去处多余关键字的标签,它可以用来实现 where 和 set 的效果. <!-- 使用 if/trim 代替 where(判断参数) ...