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

mybatis的简单教程

整体就是mysql里存了一张表,然后在java程序里用mybatis把数据读出来的一个简单示例。

库 blog里有一张表 article

整个项目就是增加了这3个文件

首先是mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/blog"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!--写好的sql映射文件(EmployeeMapper.xml)注册到全局配置文件(mybatis-config.xml)中--><mappers><mapper resource="ArticleMapper.xml"/></mappers>
</configuration>

然后是ArticleMapper.XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper"><select id="selectArticle" resultType="org.example.po.Article">select * from article where id = #{id}</select>
</mapper>

然后是和数据库存储模型对应的对象Article类

package org.example.po;import java.util.Date;public class Article {private Integer id;private String title;private String desc;private Integer cate;private Date createdAt;private Date updatedAt;private String content;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title == null ? null : title.trim();}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc == null ? null : desc.trim();}public Integer getCate() {return cate;}public void setCate(Integer cate) {this.cate = cate;}public Date getCreatedAt() {return createdAt;}public void setCreatedAt(Date createdAt) {this.createdAt = createdAt;}public Date getUpdatedAt() {return updatedAt;}public void setUpdatedAt(Date updatedAt) {this.updatedAt = updatedAt;}public String getContent() {return content;}public void setContent(String content) {this.content = content == null ? null : content.trim();}
}

然后就是运行类,main方法

package org.example;import com.google.gson.Gson;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.po.Article;import java.io.InputStream;public class Main {public static void main(String[] args) throws Exception {String resource="mybatis-config.xml";InputStream inputStream= Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession=sqlSessionFactory.openSession();Article article=sqlSession.selectOne("selectArticle",1);Gson gson=new Gson();System.out.println("Hello world!");System.out.println(gson.toJson(article));}
}

最后把pom.XML也贴一下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>mybatis-study</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>14</maven.compiler.source><maven.compiler.target>14</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency></dependencies></project>

其他问题可以参这个官方教程

mybatis – MyBatis 3 | XML 映射器

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

相关文章:

  • 数据结构 队列(C语言实现)
  • Android---屏幕适配的处理技巧
  • vmware workstation 与 device/credential guard 不兼容
  • 第7章-使用统计方法进行变量有效性测试-7.2.1-单因素方差分析
  • 黑客技术-小白学习手册
  • 用示波器测量高压电
  • AQS中Node状态
  • STM32 寄存器配置笔记——GPIO配置输出
  • Spring boot 整合grpc 运用
  • C++ 模板保姆级详解——template<class T>(什么是模板?模板分哪几类?模板如何应用?)
  • uni.getLocation() 微信小程序 线上获取失败
  • Pytorch损失函数、反向传播和优化器、Sequential使用
  • css:两个行内块元素和图片垂直居中对齐
  • 从0开始python学习-34.pytest常用插件
  • 初始MySQL(二)(表的增删查改)
  • SLAM从入门到精通(SLAM落地的难点)
  • 通过内网穿透快速搭建公网可访问的Spring Boot接口调试环境
  • 职业迷茫,我该如何做好职业规划
  • 数据结构----顺序栈的操作
  • web3 React Dapp书写订单 买入/取消操作
  • C++学习---信号处理机制、中断、异步环境
  • 机器学习——奇异值分解案例(图片压缩-代码简洁版)
  • 【Go入门】面向对象
  • Asp.Net Core 中使用配置文件
  • 深入理解JVM虚拟机第二十四篇:详解JVM当中的动态链接和常量池的作用
  • QGridLayout
  • 万能在线预约小程序系统源码 适合任何行业在线预约小程序+预约到店模式 带完整的搭建教程
  • Leetcode 2935. Maximum Strong Pair XOR II
  • [直播自学]-[汇川easy320]搞起来(4)看文档 查找设备(续)
  • WebSphere Liberty 8.5.5.9 (四)