当前位置: 首页 > news >正文

MyBatis查询数据库

目录

一、什么是MyBatis

二、搭建MyBatis开发环境

🍅添加MyBatis依赖

🍅在数据库添加数据  

🍅设置MyBatis配置

         🎈数据库的相关连接信息🎈xml的保存和设置路径

三、使用MyBatis模式和语法操作数据库

        🍅接口:方法声明(给service层调用)

        🍅XML:实现接口

         🍅生成测试类

 四、相关插件下载


一、什么是MyBatis

MyBatis是一款持久层框架,支持自定义SQL、存储过程以及高级映射。可以嗲提JDBC代码以及设置参数和获取结果集的工作。MyBatis可以通过简单的XML或者注解来配置和映射原始类型、接口和Java POJO为数据库中的记录。

它是更见但完成程序和数据库交互的工具,使得能够简单操作和读取数据库工具。

二、搭建MyBatis开发环境

🍅添加MyBatis依赖

创建Spring boot 时添加MyBatis的相关依赖

🍅在数据库添加数据  

drop database if exists mycnblog;create database myblog DEFAULT CHARACTER SET utf8mb4;-- 创建表[⽤户表]drop table if exists userinfo;create table userinfo(id int primary key auto_increment,username varchar(100) not null,password varchar(32) not null,photo varchar(500) default '',createtime datetime default now(),updatetime datetime default now(),`state` int default 1) default charset 'utf8mb4';-- 在表中添加数据
INSERT INTO `userinfo` (`id`, `username`, `password`, `photo`, 
`createtime`, `updatetime`, `state`) VALUES 
(1, 'admin', 'admin', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1)
;

🍅设置MyBatis配置

         🎈数据库的相关连接信息🎈xml的保存和设置路径

# 设置数据库的相关连接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306?myblog?characterEncoding=utf8
spring.datasource.username=root
# 自己的mysql密码,不要写我的
spring.datasource.password=111111
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# MyBatis配置信息
# myBatis的实现类
# 设置MyBatis XML存放路径和命名格式,
# 路径就是冒号后面的斜杠前面的,我这里时和.properties同级的mybatis包
mybatis.mapper-locations=classpath:mybatis/*Mapper.xml# 以上是必须要设置的,
# 以下是可选配置
# 配置MyBatis执行时打印SQL
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 设置日志级别
logging.level.com.example.demo=debug

        

        

三、使用MyBatis模式和语法操作数据库

 

首先先在数据库添加数据

常规写法包含了两个文件:

        🍅接口:方法声明(给service层调用)

定义实体类

@Data
public class UserInfo {private int id;private String username;private String password;private String photo;private LocalDateTime updatatime;private LocalDateTime createtime;private int state;}

在接口中进行方法声明 

@Mapper  //数据持久层的标志
public interface UserMapper {
//UserInfo是一个自定义的实体类List<UserInfo> getAll();
}

        🍅XML:实现接口

固定格式:

<?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="com.example.demo.dao.UserMapper">
<!--    id是UserMapper的方法名-->
-- 查询操作,其他操作同理<select id="getAll" resultType="com.example.demo.model.UserInfo">
--         不写分号select * from userinfo</select>
</mapper>

         🍅生成测试类

 

 

@SpringBootTest     //一定要加该注解,目的是为了告诉当前的测试程序是运行在Springboot容器中的
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid getAll() {List<UserInfo> list = userMapper.getAll();System.out.println(list);}
}

 查询结果:

 四、相关插件下载

做完上述步骤以后,可以发现,UserMapper.xml实现了UserMapper接口,但是如果在接口和xml文件不叫多的情况下要对文件进行修改,这样找起来很麻烦,我们就可以下载一个mybatisx的插件。

 

 点击图标就可以直接跳转到对应的xml文件或者接口文件中。

http://www.lryc.cn/news/98021.html

相关文章:

  • RVM问题记录 - Error running ‘__rvm_make -j10‘
  • VIS for AI :ConvNetJS
  • 【Python入门系列】第二十篇:Python区块链和加密货币
  • MySQL 服务器的调优策略
  • Educational Codeforces Round 152 (Rated for Div. 2)
  • CSPM难度大吗?对比pmp怎么样?
  • Android.mk中的LOCAL_OVERRIDES_PACKAGES用法
  • Matlab遍历文件及直方图统计
  • 为什么要格式化硬盘?硬盘格式化了数据怎么恢复
  • PHP注册、登陆、6套主页-带Thinkphp目录解析-【白嫖项目】
  • antDesignMobile中Switch配合Form使用无效解决方案
  • 记录springboot在k8s下无法读取文件问题
  • 数据湖如何为企业带来9%的高增长?可否取代数据仓库?
  • P2669 [NOIP2015 普及组] 金币
  • 【2023】华为OD机试真题Java CC++ Python JS Go-题目0250-选修课
  • lama cleaner
  • 制作一个简易的计算器app
  • 48. 旋转图像
  • “深入解析Spring Boot:从入门到精通的完整指南“
  • 【C++】C++11——包装器
  • 插件使用权限管理软件(三)WebAPI项目IIS部署
  • [算法很美打卡] 多维数组篇 (打卡第二天)
  • K8S初级入门系列之十一-安全
  • 【雕爷学编程】MicroPython动手做(02)——尝试搭建K210开发板的IDE环境6
  • “深入解析Spring Boot:从入门到精通“
  • [自然语言处理] 自然语言处理库spaCy使用指北
  • 【新日语(2)】第6課 拓哉もさしみを食べたがってします
  • uni-app 经验分享,从入门到离职(一)——初始 uni-app,快速上手(文末送书福利1.0)
  • Python爬虫实例之淘宝商品页面爬取(api接口)
  • 并发编程 | CompletionService - 如何优雅地处理批量异步任务