笔记源码: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);
}
}

最新文章

  1. PetaPojo —— JAVA版的PetaPoco
  2. cocos2d-x 的两大基类
  3. PHP正则表达式提取超链接及其标题
  4. WebRTC手记Channel概念
  5. Tkinter颜色方案举例
  6. java初学的几个问题
  7. ASP.NET Boilerplate 邮件类使用
  8. 瑞柏匡丞_免费app开发是否可行
  9. IIS日志路径,修改存放位置,清除日志方法
  10. Java经典编程题50道之十八
  11. Redis集群方案怎么做?大牛给你介绍五种方案!
  12. 3步实现ssh面密码登录
  13. Android ListView在增加HeaderView之后使用getLocationInWindow和getLocationOnScreen获得值不正确的解决方法
  14. HDU 3761 炸碉堡【半平面交(nlogn)】+【二分】
  15. 1207. [HNOI2004]打鼹鼠【线性DP】
  16. koa 核心源码介绍
  17. Cookie文件格式解析
  18. windows下测试flask的例子tuorial报错flask KeyError: &#39;DATABASE&#39;
  19. HRBUST1310 火影忍者之~鸣人 2017-03-06 16:01 104人阅读 评论(0) 收藏
  20. easyui 设置一加载,搜索框立即弹出的效果

热门文章

  1. SpringBoot与单元测试JUnit的结合
  2. python模块一键安装
  3. MySQL基础知识和常用命令总结
  4. 有关HTTP协议
  5. python学习笔记(六)---文件操作与异常处理机制
  6. application/x-www-form-urlencoded ,multipart/form-data, text/plain
  7. tx-Lcn 分布式事务
  8. 怎么在java中创建一个自定义的collector
  9. 【Linux网络基础】网络子网划分基础知识(IP地址,子网)
  10. RHCS图形界面建立GFS共享下