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 ...
随机推荐
- Git错误merge怎么办?
Git怎样撤销一次分支的合并Merge git merge了错误分支,如何优雅的回退到merge前的状态? 版本回退
- php函数 array_diff
array_diff ( array $array1 , array $array2 [, array $... ] ) : array 对比 array1 和其他一个或者多个数组,返回在 array ...
- 一招明白URL和URI的区别
URL和URI的区别(示例): URL[统一资源定位器]: http://localhost:8080/api/account/queryAccountInfo URI[统一资源定位符]: /api/ ...
- MyBatis基础:MyBatis缓存(5)
1. MyBatis缓存简介 MyBatis提供支持一级缓存及二级缓存. 一级缓存: 2.MyBatis一级缓存
- 在本机使用虚拟机安装一个linux系统,并搭建ftp服务器
一.Linux基础使用:linux服务器环境搭建(FTP服务器), 在本机使用虚拟机安装一个linux系统,并搭建ftp服务器,要求能使用ftp服务将本机文件到保存linux虚拟机上 资料: VMwa ...
- 读取Excel2003、Excel2007或更高级的兼容性问题 workbook 下载中文名称文件
xls 使用HSSFWorkbook xlsx使用XSSFWorkbook 但是我使用XSSFWorkbook时没找到nuget包,引用不了,只能重新找办法,幸好workbook解决了我这个问题 // ...
- MT【322】绝对值不等式
已知 $a,b,c\in\mathbb R$,求证:$|a|+|b|+|c|+|a+b+c|\geqslant |a+b|+|b+c|+|c+a|$ 分析:不妨设$c=\max\{a,b,c\},\d ...
- 帝国CMS Table '***.phome_ecms_news_data_' doesn't exist
帝国CMS刷新内容页出现以下错误 1 Table 'www.536831.com.phome_ecms_news_data_' doesn't exist select keyid,dokey,n ...
- COGS 2353 2355 2356 2358 有标号的DAG计数
不用连通 枚举入度为0的一层 卷积 发现有式子: 由$n^2-i^2-(n-i)^2=2*i*(n-i)$ 可得$2^{i*(n-i)}=\frac{{\sqrt 2}^{(n^2)}}{{\sqrt ...
- SQL server 统计数据库表数量和列出所有表名称
统计表数量 SELECT count(*) FROM sys.objects WHERE type='U' 列出表名称 SELECT NAME FROM sys.objects WHERE typ ...