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 ...
随机推荐
- phpstudy xdebug 配置
来源:https://baijiahao.baidu.com/s?id=1607680791440431678&wfr=spider&for=pc https://www.cnblog ...
- 第八次-非确定的自动机NFA确定化为DFA
提交作业 NFA 确定化为 DFA 子集法: f(q,a)={q1,q2,…,qn},状态集的子集 将{q1,q2,…,qn}看做一个状态A,去记录NFA读入输入符号之后可能达到的所有状态的集合. ...
- java 8 Stream中操作类型和peek的使用
目录 简介 中间操作和终止操作 peek 结论 java 8 Stream中操作类型和peek的使用 简介 java 8 stream作为流式操作有两种操作类型,中间操作和终止操作.这两种有什么区别呢 ...
- 【高并发】由InterruptedException异常引发的思考
写在前面 InterruptedException异常可能没你想的那么简单! 前言 当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedExc ...
- 如何理解Java的值传递
结论 为了加深印象,先把结论放在文章开头. ++Java中只有值传递++. 形参与实参 在理解Java的值传递 实参Argument 实际参数,主调用函数传递给调用函数的参数 形参Parameter ...
- 2016年全球IC设计大厂营收排名:高通稳居龙头
TrendForce旗下拓墣产业研究所最新研究统计,2016年全球前十大无晶圆IC设计业者营收中,高通(QCT)仍然稳居龙头宝座.而前三大业者高通.新博通(Broadcom)与联发科合计营收占前十名营 ...
- CF1288C-Two Arrays (DP)
You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that: the len ...
- CodeForces - 1047A
A. Little C Loves 3 I time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- ACM一年记,总结报告(希望自己可以走得很远)
一. 知识点梳理 (一) 先从工具STL说起: 容器学习了:stack,queue,priority_queue,set/multiset,map/multimap,vector. 1.stack: ...
- LeetCode 25. K 个一组翻转链表 | Python
25. K 个一组翻转链表 题目来源:https://leetcode-cn.com/problems/reverse-nodes-in-k-group 题目 给你一个链表,每 k 个节点一组进行翻转 ...