springBoot整合Mybatis,Junit
笔记源码:https://gitee.com/ytfs-dtx/SpringBoot
整合Mybatis
SpringBoot的版本:2.2.5.RELEASE Mybatis版本:mybatis-spring-boot-starter 2.1.2
添加Mybatis的起步依赖
<!-- Mybatis的起步依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
添加数据库的驱动坐标
springBoot自动管理了mysql驱动的坐标,不用自己导入版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加数据库连接信息,并使用druid连接池
# DBconfiguration:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/test
type: com.alibaba.druid.pool.DruidDataSource
创建User表
在test数据库中创建表
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('', 'zhangsan', '', '张三');
INSERT INTO `user` VALUES ('', 'lisi', '', '李四');
创建实体Bean
package xyz.ytfs.entity; /**
* @author by 雨听风说
* @Classname User
* @Description TODO(用户的实体)
* @Date 2020/5/13 15:45
*/ public class User { private Integer id;
private String username;
private String password;
private String name; public Integer getId() {
return id;
} public void setId(Integer 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;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
}
编写Mapper
package xyz.ytfs.mapper; import xyz.ytfs.entity.User; import java.util.List; /**
* @author by 雨听风说
* @Classname UserMapper
* @Description TODO(用户的mapper接口)
* @Date 2020/5/13 15:49
*/ public interface UserMapper { List<User> findUserAll();
}
配置Mapper配置文件
<?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="xyz.ytfs.entity.User">
<select id="findUserAll" resultType="xyz.ytfs.entity.User">
select * from user;
</select>
</mapper>
在application.yml中 添加mybatis信息
#mybatis信息
mybatis:
#pojo别名扫描包
type-aliases-package: xyz.ytfs.entity
#加载Mybatis映射文件
mapper-locations: classpath:xyz/ytf/mapper/*Mapper.xml
编写测试的controller
package xyz.ytfs.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.ytfs.entity.User;
import xyz.ytfs.mapper.UserMapper; import java.util.List; /**
* @author by 雨听风说
* @Classname MybatisController
* @Description TODO(mybatis 的控制层)
* @Date 2020/5/13 15:58
*/ @Controller
public class MybatisController { @Autowired
private UserMapper userMapper; @RequestMapping("query")
@ResponseBody
public List<User> findUserALl() {
return this.userMapper.findUserAll();
}
}
测试

整合Junit
添加起步依赖
<!-- springBoot整合junit测试的起起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
测试
package xyz.ytfs.test; 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 xyz.ytfs.MybatisApplication;
import xyz.ytfs.mapper.UserMapper; /**
* @author by 雨听风说
* @Classname JunitTest
* @Description TODO(Juint整合测试)
* @Date 2020/5/13 16:40
*/ @RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisApplication.class)
public class JunitTest { @Autowired
private UserMapper userMapper; @Test
public void testFindAll() {
//查询并且循环输出
this.userMapper.findUserAll().forEach(System.out::println);
}
}


springBoot整合Mybatis,Junit的更多相关文章
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- springboot整合mybatis(SSM开发环境搭建)
0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...
- 学习springboot整合mybatis并编写测试类
报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...
- springboot整合mybatis的时候报错Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
今天闲来无事,学习springboot整合mybatis,在bilibili看视频学的,视频中在dao层的interface上面加上org.apache.ibatis.annotations.Mapp ...
- SpringBoot整合Mybatis,并实现事务控制
SpringBoot整合Mybatis,并实现事务控制 1. 在pom文件里添加相关maven文件 <parent> <groupId>org.springframework. ...
- SpringBoot数据访问(一) SpringBoot整合Mybatis
前言 SpringData是Spring提供的一个用于简化数据库访问.支持云服务的开源框架.它是一个伞形项目,包含了大量关系型数据库及非关系型数据库的数据访问解决方案,其设计目的是为了使我们可以快速且 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
随机推荐
- sftp的用法
linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...
- pytorch seq2seq闲聊机器人beam search返回结果
decoder.py """ 实现解码器 """ import heapq import torch.nn as nn import con ...
- CSS躬行记(7)——合成
在图形编辑软件中,可以按特定地方式处理不同图层的合成,最新的CSS规范也引入了该功能,并提供了mix-blend-mode和background-blend-mode两个属性.混合模式(blendin ...
- markdownPad常用功能示例
1.列表 无序列表 姓名 张三 李四 王五 有序列表 张三 李四 王五 2.超链接 百度 3.引用 锄禾日当午,汗滴禾下土.谁知盘中餐,粒粒皆辛苦. -- 李绅<古风二首> 4.简要修饰文 ...
- Python学习15之python内置六大标准类型
1.六大标准类型:数值型,str,list,set,tuple,dic 2.数值型:int,float,bool,complex 3.区别: 1)数值型和str,tuple都是不可变类型 而list, ...
- weblogic补丁升级详细步骤,18.7.17补丁更新
weblogic打补丁 到weblogic官网下载补丁包 对应的补丁包 如: p22248372_1036012_Generic.zip 一 安装补丁步骤 1.登录linux的weblogic用户 ...
- Scala教程之:Option-Some-None
文章目录 Option和Some Option和None Option和模式匹配 在java 8中,为了避免NullPointerException,引入了Option,在Scala中也有同样的用法. ...
- http请求返回的数字代表的含义
一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务器超时 下面提供 HTTP 状态码的完整列表.点击链接可了解详情.您也可以访问 HTTP 状态码上的 ...
- POJ1077 八数码问题
题目:八数码 网址:http://poj.org/problem?id=1077 在一个3×3的网格中,1~8这8个数字和一个"X"恰好不重不漏地分布在这3×3的网格中. 例如: ...
- 《Microduino实战》——2.3 Microduino STM32核心系列
本节书摘来自华章出版社<Microduino实战>一 书中的第2章,第2.3节,作者:姚琪 杨立斌,更多章节内容可以访问云栖社区"华章计算机"公众号查看. 2.3 Mi ...