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

MyBatis框架(编写代码部分1)

目录

四、Mapper.xml映射文件

4.1 创建Mapper.xml映射文件

Dept 实体类:

DeptMapper 接口:

4.2 编写DeptMapper.xml 映射文件

查询使用

  • 标签
  • 插入 使用 标签
  • 占位符号:#{ }   ${ }
  • 修改 使用 标签
  • 删除 使用 标签
  • 4.3 编写测试类
  • 本次新加的代码
  • Dept 实体类
  • DeptMapper 接口
  • DeptMapper.xml 映射文件
  • DeptTest 测试类

  • 需要的插件:MyBatisX,Lombok

    点击 file — Settings — Plugins — 搜索 — install — Apply — OK

    四、Mapper.xml映射文件

    SQL 映射文件只有很少的几个顶级元素(按照应被定义的顺序列出):

    • cache – 该命名空间的缓存配置。 ----缓存处理

    • cache-ref – 引用其它命名空间的缓存配置。 -----缓存处理

    • resultMap – 描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素。 ----关联关系映射

    • parameterMap – 老式风格的参数映射。此元素已被废弃,并可能在将来被移除!

    • sql – 可被其它语句引用的可重用语句块。 ----动态sql语句

    • insert – 映射插入语句。

    • update – 映射更新语句。

    • delete – 映射删除语句。

    • select – 映射查询语句。

    4.1 创建Mapper.xml映射文件

    先创建一个实体类 Dept,再创建接口 DeptMapper 和 DeptMapper.xml 映射文件

    Dept 实体类:

    使用注解自动添加构造器,get/set方法 以及 toString方法

    package com.jr.pojo;
    import lombok.*;@AllArgsConstructor //自动添加 全参构造
    @NoArgsConstructor  //自动添加 无参构造
    @Data               //自动添加 get/set方法
    @ToString
    public class Dept {private Integer deptno;private String dname;private String loc;
    }

    DeptMapper 接口:

    package com.jr.mapper;
    import com.jr.pojo.Dept;
    import java.util.ArrayList;public interface DeptMapper {ArrayList<Dept> selectAll();int insertDept(Dept record);int updateDept(Dept record);int deleteDeptById(Integer id);
    }

    DeptMapper.xml 映射文件:
    先添加一下代码,设置二级缓存的范围(一般会定位到接口)

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.jr.mapper.DeptMapper"><!-- 设置二级缓存的范围(一般会定位到接口) --></mapper>

    会有小鸟图标出现

    4.2 编写DeptMapper.xml 映射文件

    在接口:ArrayList<Dept> selectAll(); 

    查询使用 <select> 标签

    id 是方法名,resultType 是返回值类型,parameterType 是参数类型,标签里写 sql 语句

    如果在核心配置文件里取别名了,这里的 resultType 和 parameterType 可以直接使用别名(参考上一篇 MyBatis框架(概念,环境搭建,核心配置文件部分)-CSDN博客 的3.3 typeAliases 类型别名)

        <select id="selectAll" resultType="com.jr.pojo.Dept">select * from dept</select>

    插入 使用 <insert> 标签

        <insert id="insertDept" parameterType="com.jr.pojo.Dept">insert into dept values(#{deptno},#{dname},#{loc})</insert>

    占位符号:#{ }   ${ }

    #{ } 预编译处理$ { } 是字符串替换

    mybatis在处理 #{ } 时,会将sql中的 #{ } 替换为 ? 号,调用 PreparedStatement 的 set 方法来赋值;

    mybatis 在处理 $ { } 时,就是把 ${ } 替换成变量的值。使用 #{ } 可以有效的防止SQL注入,提高系统安全性。但是表名用参数传递进来的时候,只能使用 ${}

    修改 使用 <update> 标签

        <update id="updateDept" parameterType="com.jr.pojo.Dept">update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}</update>

    删除 使用 <delete> 标签

        <delete id="deleteDeptById" parameterType="integer">delete from dept where deptno=#{deptno}</delete>

    4.3 编写测试类

    public class DeptTest {SqlSessionFactory sqlSessionFactory=null;@Beforepublic void before() throws IOException {InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");//1.加载 mybatis 核心配置文件sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//2.获得连接对象:session 会话对象}@Testpublic void test01() throws IOException {//3.通过session对象.方法();SqlSession sqlSession = sqlSessionFactory.openSession();List<Dept> list = sqlSession.selectList("selectAll");for(Dept dept:list){System.out.println(dept);}//4.关闭会话对象sqlSession.close();}
    }
    

    查询结果:

    添加:

        @Testpublic void test02() throws IOException {//通过session对象.方法SqlSession sqlSession = sqlSessionFactory.openSession();Dept dept = new Dept(55,"后勤部门","沈阳");int i = sqlSession.insert("insertDept",dept);if(i>0){System.out.println("添加成功");}else{System.out.println("添加失败");}sqlSession.commit();sqlSession.close();}

    注意:添加、删除、修改操作后 记得提交事务— sqlSession.commit(); — 后再关闭

    本次新加的代码

    Dept 实体类

    DeptMapper 接口

    DeptMapper.xml 映射文件

    DeptTest 测试类

    package com.jr.test;import com.jr.pojo.Dept;
    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.junit.Before;
    import org.junit.Test;import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;public class DeptTest {SqlSessionFactory sqlSessionFactory=null;@Beforepublic void before() throws IOException {InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");//1.加载 mybatis 核心配置文件sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//2.获得连接对象:session 会话对象}@Testpublic void test01() throws IOException {//3.通过session对象.方法();SqlSession sqlSession = sqlSessionFactory.openSession();List<Dept>  list = sqlSession.selectList("selectAll");for(Dept dept:list){System.out.println(dept);}//4.关闭会话对象sqlSession.close();}@Testpublic void test02() throws IOException {//通过session对象.方法SqlSession sqlSession = sqlSessionFactory.openSession();Dept dept = new Dept(55,"后勤部门","沈阳");int i = sqlSession.insert("insertDept",dept);if(i>0){System.out.println("添加成功");}else{System.out.println("添加失败");}sqlSession.commit();sqlSession.close();}@Testpublic void test03() throws IOException {//通过session对象.方法SqlSession sqlSession = sqlSessionFactory.openSession();int i = sqlSession.delete("deleteDeptById",66);if(i>0){System.out.println("删除成功");}else{System.out.println("删除失败");}sqlSession.commit();sqlSession.close();}@Testpublic void test04() throws IOException {//通过session对象.方法SqlSession sqlSession = sqlSessionFactory.openSession();Dept dept = new Dept(40,"后勤部门","沈阳");int i = sqlSession.update("updateDept",dept);if(i>0){System.out.println("修改成功");}else{System.out.println("修改失败");}sqlSession.commit();sqlSession.close();}
    }
    


    文章转载自:

    http://BudL45fm.sqgqh.cn
    http://xXeNZiyV.sqgqh.cn
    http://mFeOBKjC.sqgqh.cn
    http://7rDIi6gc.sqgqh.cn
    http://xqItYD7m.sqgqh.cn
    http://Y14QdxTq.sqgqh.cn
    http://zQOVJgn3.sqgqh.cn
    http://yb1KuYiA.sqgqh.cn
    http://b9F9h8F2.sqgqh.cn
    http://0HECb0ha.sqgqh.cn
    http://R0ppfMn0.sqgqh.cn
    http://bMfn9GCk.sqgqh.cn
    http://1zvmhVco.sqgqh.cn
    http://qwPJwfqL.sqgqh.cn
    http://cA2Du6nG.sqgqh.cn
    http://IFmNHXJV.sqgqh.cn
    http://XqA35dct.sqgqh.cn
    http://Ykryrm0F.sqgqh.cn
    http://G9Xujxso.sqgqh.cn
    http://zlDg45bv.sqgqh.cn
    http://KRzXg9ok.sqgqh.cn
    http://ipUgHcMK.sqgqh.cn
    http://QGxpYlrz.sqgqh.cn
    http://yX037F0r.sqgqh.cn
    http://80ibFbP6.sqgqh.cn
    http://Z8zYdKXK.sqgqh.cn
    http://nG23Ev1W.sqgqh.cn
    http://O4eGLaEx.sqgqh.cn
    http://tnArcg0n.sqgqh.cn
    http://L1f1TouN.sqgqh.cn
    http://www.dtcms.com/a/379143.html

    相关文章:

  • mes之工序管理
  • P4053 [JSOI2007] 建筑抢修
  • Unity Embedded Browser文档翻译
  • 阻容感专题学习笔记
  • ARM指令集(Instruction Set)细节
  • 28.线程互斥与同步(二)
  • 批量修改图片尺寸大小的免费工具
  • 【vscode】如何离线下载vsxi插件,且在无网环境下离线安装插件-2026最新实验教程
  • 基于浏览器运行的本地大模型语音助手
  • 动态热机械分析测试(DMA):解析材料的粘弹性能
  • 【龙智Atlassian插件】Confluence周报插件上线AI智能总结,一键生成专业报告
  • 因表并行引发的血案【故障处理案例】
  • 实现双向循环链表
  • Flutter Riverpod 3.0 发布,大规模重构下的全新状态管理框架
  • This is Game
  • Git分支管理:从创建到合并冲突解决(二)
  • Elasticsearch 7.15 存储类型详解
  • 深入解析数据结构之栈及其应用
  • (一)昇腾AI处理器技术
  • BUUCTF刷题十一道(14)
  • Linux防火墙-Iptables
  • python访问基于docker搭建的elasticsearch
  • logback-spring.xml文件说明
  • 【PyTorch训练】为什么要有 loss.backward() 和 optimizer.step()?
  • 抖音大数据开发一面(0905)
  • 原生js的轮播图
  • 连接池项目考点
  • ruoyi-flowable-plus框架节点表单的理解
  • js.228汇总区间
  • BERT中文预训练模型介绍