springboot集成mybatis源码分析(一)
本篇文章只是简单接受使用,具体源码解析请看后续文章
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源码分析(一)的更多相关文章
- springboot集成mybatis源码分析-mybatis的mapper执行查询时的流程(三)
例: package com.example.demo.service; import com.example.demo.dao.UserDao; import com.example.demo.do ...
- springboot集成mybatis源码分析-启动加载mybatis过程(二)
1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...
- springboot整合mybatis源码分析
springboot整合mybatis源码分析 本文主要讲述mybatis在springboot中是如何被加载执行的,由于涉及的内容会比较多,所以这次只会对调用关系及关键代码点进行讲解,为了避免文章太 ...
- MyBatis源码分析(5)——内置DataSource实现
@(MyBatis)[DataSource] MyBatis源码分析(5)--内置DataSource实现 MyBatis内置了两个DataSource的实现:UnpooledDataSource,该 ...
- MyBatis源码分析(3)—— Cache接口以及实现
@(MyBatis)[Cache] MyBatis源码分析--Cache接口以及实现 Cache接口 MyBatis中的Cache以SPI实现,给需要集成其它Cache或者自定义Cache提供了接口. ...
- MyBatis 源码分析-项目总览
MyBatis 源码分析-项目总览 1.概述 本文主要大致介绍一下MyBatis的项目结构.引用参考资料<MyBatis技术内幕> 此外,https://mybatis.org/mybat ...
- 精尽 MyBatis 源码分析 - 整体架构
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
- 精尽 MyBatis 源码分析 - SqlSession 会话与 SQL 执行入口
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
- 精尽MyBatis源码分析 - MyBatis-Spring 源码分析
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
随机推荐
- 兼容IphoneX
兼容IphoneX的显示,一般有两种方法,一种通过css的media来做兼容,一种是通过js来做兼容. 一.我们先讲CSS的方法,我们要做两步即可: 1. 全屏覆盖,html使用 <meta n ...
- USB安装centos6系统(centos7需要换软件)
一.下载系统镜像 二.下载安装软碟通软件UltraISO 三.插入U盘制作启动盘 1.用软碟通打开镜像文件:文件-->打开 2.写入映像:启动-->写入硬盘映像 3.等待写入完成 四.系统 ...
- Bugku 杂项 啊哒
有趣的表情包来源:第七届山东省大学生网络安全技能大赛 下载下来安装包后可以得到一张图片,010发现jpg后面还夹带着一些东西,用binwalk提取后得到一个压缩包,但是需要密码. 我卡在这里了,尝试了 ...
- 2019/04/06 BJ省选模拟DAY1
今天好惨啊 \(n*m\)的图手抖打成\(n*n\)的板子 挂了70分 否则并列rk20?[雾] hyc好厉害啊阿克 省队预订了啊 T1 众所周知向量a,b相乘形成一个矩陣 已知一个矩阵 问至少要多少 ...
- yii2 redirect重定向
redirect使用方法 $this->redirect(array('/site/contact','id'=>12)); //http://www.kuitao8.com/testwe ...
- ubuntu16.04+cuda9+cudnn7+tensorflow+pycharm环境搭建
安装环境:ubuntu16.04+cuda9+cudnn7+tensorflow+pycharm 1)前期搭建过程主要是按照这篇博文,对于版本选择,安装步骤都讲得很详细,亲测有效! https://b ...
- Gym - 101350F Monkeying Around(线段树+树状数组)
When the monkey professor leaves his class for a short time, all the monkeys go bananas. N monkeys a ...
- Mysql加锁过程详解(9)-innodb下的记录锁,间隙锁,next-key锁
Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...
- oldboy s21day10
#!/usr/bin/env python # -*- coding:utf-8 -*- # 1.写函数,函数可以支持接收任意数字(位置传参)并将所有数据相加并返回. ''' def func(* ...
- SpringBoot系列: Pebble模板引擎语法介绍
本文基于Pebble官方文档, 对pebble的语法做一些介绍. ===============================Pebble 官方资料========================= ...