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

myBatis的基本操作(持续更新中。。。)

目录

  • 1. 简介
  • 2. 简单使用
  • 3. 代理开发
  • 4. 小技巧
  • 5. 动态查询
  • 6. 注解(待更新)
  • 底部

1. 简介

mybatis是一款优秀的持久层框架,用来简化JDBC开发

持久层:负责将数据保存到数据库的那一层代码

2. 简单使用

依赖

<dependencies>  <dependency>            <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>3.5.14</version>  </dependency><!--        mysql的驱动-->  <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.26</version>  </dependency>        <dependency>            <groupId>junit</groupId>  <artifactId>junit</artifactId>  <scope>test</scope>  <version>4.13.2</version>  </dependency>    </dependencies>
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE configuration  PUBLIC "-//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.jdbc.Driver"/>  <property name="url" value="${url}"/>  <property name="username" value="${username}"/>  <property name="password" value="${password}"/>  </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="org/mybatis/example/BlogMapper.xml"/>  </mappers></configuration>

![[Pasted image 20240719201225.png]]

Userx

package com.yxz.pojo;  import java.util.Date;  public class Userx {  private Integer id;  private String username;  private Date birthday;  private Character sex;  private String address;  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 Date getBirthday() {  return birthday;  }  public void setBirthday(Date birthday) {  this.birthday = birthday;  }  public Character getSex() {  return sex;  }  public void setSex(Character sex) {  this.sex = sex;  }  public String getAddress() {  return address;  }  public void setAddress(String address) {  this.address = address;  }  @Override  public String toString() {  return "Userx{" +  "id=" + id +  ", username='" + username + '\'' +  ", birthday=" + birthday +  ", sex=" + sex +  ", address='" + address + '\'' +  '}';  }  
}

UserMapper.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="test">  <select id="selectAll" resultType="com.yxz.pojo.Userx">  select * from userx;  </select>  
</mapper>

进行测试:

package com.yxz;  import com.yxz.pojo.Userx;  
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 java.io.IOException;  
import java.io.InputStream;  
import java.util.List;  public class MybatisDemo {  public static void main(String[] args) throws IOException {  // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory  String resource = "mybatis-config.xml";  InputStream inputStream = Resources.getResourceAsStream(resource);  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  // 2. 获取对应的SqlSession对象,用它执行sql  SqlSession sqlSession = sqlSessionFactory.openSession();  // 3. 执行sql  List<Userx> userxs =  sqlSession.selectList("test.selectAll");  System.out.println(userxs);  sqlSession.close();  }  
}

建好数据库
在这里插入图片描述

目录:
在这里插入图片描述

3. 代理开发

在这里插入图片描述

创建一个接口,作为代理

package com.yxz.dao;  import com.yxz.pojo.Userx;  import java.util.List;  public interface UserMapper {  List<Userx> selectAll();  
}

命名空间修改:

<?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.yxz.dao.UserMapper">  <select id="selectAll" resultType="com.yxz.pojo.Userx">  select * from userx;  </select>  
</mapper>

进行测试

package com.yxz;  import com.yxz.dao.UserMapper;  
import com.yxz.pojo.Userx;  
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 java.io.IOException;  
import java.io.InputStream;  
import java.util.List;  public class MybatisDemo {  public static void main(String[] args) throws IOException {  // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory  String resource = "mybatis-config.xml";  InputStream inputStream = Resources.getResourceAsStream(resource);  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  // 2. 获取对应的SqlSession对象,用它执行sql  SqlSession sqlSession = sqlSessionFactory.openSession();  // 3. 执行sql  
//        List<Userx> userxs =  sqlSession.selectList("test.selectAll");  UserMapper userMapper = sqlSession.getMapper(UserMapper.class);  List<Userx> userxs =  userMapper.selectAll();  System.out.println(userxs);  sqlSession.close();  }  
}

4. 小技巧

起别名

<typeAliases>  <package name="com.yxz.pojo"/>  
</typeAliases>
<select id="selectAll" resultType="userx">  select  <include refid="brand_column"/>  from userx;  
</select>  
<sql id="brand_column">  id,username, birthday, sex, address  
</sql>
<select id="selectById" parameterType="int" resultType="userx">  select  <include refid="brand_column"/>  from userx where id  <![CDATA[        <     ]]>     #{id};</select>

5. 动态查询

choose when otherwise
set if

delete from user where id 
in(<foreach collction="ids" item="id" separator=",">#{id} </foreach>) ;

在这里插入图片描述

6. 注解(待更新)

@Select
@Insert
@Update
@Delete

底部

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Windows配置Qt+VLC
  • 使用 node --inspect 命令调试js文件执行
  • 【LeetCode】day18:530 - 二叉搜索树的最小绝对差, 501 - 二叉搜索树中的众数, 236 - 二叉树的最近公共祖先
  • python爬虫Selenium模块及测试案例详解
  • 大语言模型-Transformer-Attention Is All You Need
  • 如何发一篇顶会论文? 涉及3D高斯,slam,自动驾驶,三维点云等等
  • 学生管理系统(C语言)(Easy-x)
  • [python]pycharm设置清华源
  • WEB前端09-前端服务器搭建(Node.js/nvm/npm)
  • 【引领未来智造新纪元:量化机器人的革命性应用】
  • npm 或者yarn下载依赖卡顿报错 sill idealTree buildDeps
  • [Redis]典型应用——分布式锁
  • BGP笔记的基本概要
  • 开源模型应用落地-FastAPI-助力模型交互-进阶篇-RequestDataclasses(三)
  • HTML5应用的安全防护策略与实践
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • co.js - 让异步代码同步化
  • CSS中外联样式表代表的含义
  • Fundebug计费标准解释:事件数是如何定义的?
  • Git同步原始仓库到Fork仓库中
  • Phpstorm怎样批量删除空行?
  • scrapy学习之路4(itemloder的使用)
  • SpiderData 2019年2月13日 DApp数据排行榜
  • Spring-boot 启动时碰到的错误
  • WebSocket使用
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 给初学者:JavaScript 中数组操作注意点
  • 如何用vue打造一个移动端音乐播放器
  • 十年未变!安全,谁之责?(下)
  • 我感觉这是史上最牛的防sql注入方法类
  • 新版博客前端前瞻
  • 最简单的无缝轮播
  • PostgreSQL之连接数修改
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #FPGA(基础知识)
  • (+4)2.2UML建模图
  • (2024,LoRA,全量微调,低秩,强正则化,缓解遗忘,多样性)LoRA 学习更少,遗忘更少
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (黑马点评)二、短信登录功能实现
  • (九十四)函数和二维数组
  • (十八)Flink CEP 详解
  • (四)linux文件内容查看
  • (转)大型网站的系统架构
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • ./configure,make,make install的作用
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作